Harry And Dig Machine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 852    Accepted Submission(s): 348

Problem Description
  As
we all know, Harry Porter learns magic at Hogwarts School. However,
learning magical knowledge alone is insufficient to become a great
magician. Sometimes, Harry also has to gain knowledge from other certain
subjects, such as language, mathematics, English, and even algorithm.
  Dumbledore,
the headmaster of Hogwarts, is planning to construct a new teaching
building in his school. The area he selects can be considered as an n*m
grid, some (but no more than ten) cells of which might contain stones.
We should remove the stones there in order to save place for the
teaching building. However, the stones might be useful, so we just move
them to the top-left cell. Taking it into account that Harry learned how
to operate dig machine in Lanxiang School several years ago, Dumbledore
decides to let him do this job and wants it done as quickly as
possible. Harry needs one unit time to move his dig machine from one
cell to the adjacent one. Yet skilled as he is, it takes no time for him
to move stones into or out of the dig machine, which is big enough to
carry infinite stones. Given Harry and his dig machine at the top-left
cell in the beginning, if he wants to optimize his work, what is the
minimal time Harry needs to finish it?
 
Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m.(1≤n,m≤50).
The next n line, each line contains m integer. The j-th number of ith line a[i][j] means there are a[i][j] stones on the jth cell of the ith line.( 0≤a[i][j]≤100 , and no more than 10 of a[i][j] will be positive integer).
 
Output
For each test case, just output one line that contains an integer indicate the minimal time that Harry can finish his job.
 
Sample Input
3 3
0 0 0
0 100 0
0 0 0
2 2
1 1
1 1
 
Sample Output
4
4
 
题意:n*m的矩阵里面有k个石头(k<=10) 哈利波特从(0,0)点出发,可以沿着x轴或者y轴在矩阵里面行走,他的任务是在最短的时间内将石头全部取完,问至少需要多少时间他才能取完所有的石头然后回到原点??
题解:构图然后暴搜。。剪枝。。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int INF = ;
struct Point
{
int x,y;
} p[];
int n,m,res,cnt;
int graph[][];
bool vis[];
void dfs(int x,int dept,int ans)
{
vis[x] = true;
if(ans>res) return ; ///剪枝
if(dept==cnt-)
{
ans += graph[x][]; ///走回去
res = min(res,ans);
return ;
}
for(int i=; i<cnt; i++)
{
if(!vis[i]&&graph[x][i]!=INF&&x!=i)
{
dfs(i,dept+,ans+graph[x][i]);
vis[i] = false;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt = ;
int v;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
scanf("%d",&v);
if(i==&&j==)
{
p[cnt].x = i,p[cnt++].y=j;
}
else if(v)
{
p[cnt].x = i,p[cnt++].y=j;
}
}
}
for(int i=; i<cnt; i++)
{
for(int j=; j<cnt; j++)
{
if(i==j)graph[i][j] = ;
else graph[i][j] =INF;
}
}
for(int i=; i<cnt; i++)
{
for(int j=i+; j<cnt; j++)
{
graph[i][j] = graph[j][i] = abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y);
}
}
/*for(int i=0; i<cnt; i++)
{
for(int j=0; j<cnt; j++)
{
printf("%d ",graph[i][j]);
}
printf("\n");
}*/
memset(vis,false,sizeof(vis));
res = INF;
vis[] = true;
dfs(,,);
printf("%d\n",res);
}
return ;
}

hdu 5067(暴力搜索)的更多相关文章

  1. hdu 4740 The Donkey of Gui Zhou(暴力搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...

  2. HDU 3131 One…Two…Five! (暴力搜索)

    题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...

  3. hdu 1427 速算24点 dfs暴力搜索

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

  4. ACM 暴力搜索题 题目整理

    UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...

  5. HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)

    题目地址:pid=5067">HDU 5067 经典的TSP旅行商问题模型. 状压DP. 先分别预处理出来每两个石子堆的距离.然后将题目转化成10个城市每一个城市至少经过一次的最短时间 ...

  6. 随手练——洛谷-P1151(枚举与暴力搜索)

    枚举 #include <iostream> using namespace std; int main() { ; cin >> k; ; i < ; i++) { ) ...

  7. HDU - 5067 / HDU - 5418 TSP

    集合表示多用[0,n)表示方法 HDU - 5067 经典TSP,每个顶点恰经过一次最优 #include<bits/stdc++.h> #define rep(i,j,k) for(in ...

  8. 枚举进程——暴力搜索内存(Ring0)

    上面说过了隐藏进程,这篇博客我们就简单描述一下暴力搜索进程. 一个进程要运行,必然会加载到内存中,断链隐藏进程只是把EPROCESS从链表上摘除了,但它还是驻留在内存中的.这样我们就有了找到它的方法. ...

  9. HDU 5067 Harry And Dig Machine(状压dp)

    HDU 5067 Harry And Dig Machine 思路:因为点才10个,在加上一个起点,处理出每一个点之间的曼哈顿距离,然后用状压dp搞,状态表示为: dp[i][s],表示在i位置.走过 ...

随机推荐

  1. Altium Designer之模块复用——设备图表符与Snippets

    Altium Designer中的设备图表符(Device Sheet Symbol)和Snippets是设计中模块复用的利器,下面简单介绍下这个两个功能的使用. 一.设备图表符(Device She ...

  2. 你是怎么封装一个view的

    可以通过纯代码或者xib的方式来封装子控件 建立一个跟view相关的模型,然后将模型数据传给view,通过模型上的数据给view的子控件赋值 /** * 纯代码初始化控件时一定会走这个方法 */ - ...

  3. 《Cracking the Coding Interview》——第7章:数学和概率论——题目5

    2014-03-20 02:20 题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分. 解法:穿过对称中心的线会将面积等分,所以连接两个中心即可.如果两个中心恰好重合,那么任意穿 ...

  4. USACO Section1.5 Superprime Rib 解题报告

    sprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  5. 去掉referer信息

    <iframe src="auto-refresh.html" width=500 height=500 rel="noreferrer">< ...

  6. CSS系列(5)-如何使用Firebug查看网页的html和css

    Firebug是火狐浏览器Firefox的一个插件,专门为开发人员开发的.使用Firebug需要先在Firefox中安装这个插件,网上有很多教程,可以对照着安装一下. 不同的火狐浏览器版本中的Fire ...

  7. Cannot create a secure XMLInputFactory --CXF调用出错

    在调用方法前加上下面三句即可调用成功: Properties props = System.getProperties(); props.setProperty("org.apache.cx ...

  8. ironic rescue standard rescue and unrescue process

    翻译官网救援/取消救援标准流程 1.用户在节点上调用Nova rescue 2.Nova ComputeManager调用virt驱动程序的rescue()方法,传入rescue_password作为 ...

  9. tarjan算法求LCA

    tarjan算法求LCA LCA(Least Common Ancestors)的意思是最近公共祖先,即在一棵树中,找出两节点最近的公共祖先. 这里我们使用tarjan算法离线算法解决这个问题. 离线 ...

  10. Android记事本09

    昨天: Activity的数据传递. 今天: 从Activity中返回数据 请求码和返回码的作用 遇到的问题: 无.