Harry And Dig Machine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 435    Accepted Submission(s): 153

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
BC的b题。一開始写的dfs后来挂掉了。后来才知道自己傻逼了居然妄图想搜全图。 。题解给的是状压DP,蒟蒻还不会。 。由于题意是要让清掉非0的数字且步数最小,我们能够把图中非0的数字的坐标取出来爆搜这些点就能够了,注意用最优性剪一下。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int INF=1<<27;
const int maxn=1010;
int ans,num,n,m,a[55][55];
bool vis[12];
int tx[12],ty[12];
void dfs(int x,int y,int goal,int step)
{
//最优性剪枝。这个比較好想
if(step>ans)return ;
if(goal==num)
{
ans=min(ans,step+x-1+y-1);
return ;
}
for(int i=0;i<num;i++)
{
if(!vis[i])
{
vis[i]=1;
int st=step+abs(x-tx[i])+abs(y-ty[i]);
dfs(tx[i],ty[i],goal+1,st);
vis[i]=0;
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
num=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(i==1&&j==1)continue;
if(a[i][j])
{
tx[num]=i;
ty[num++]=j;
}
}
ans=INF;
dfs(1,1,0,0);
printf("%d\n",ans);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

HDU 5067-Harry And Dig Machine(DFS)的更多相关文章

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

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

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

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

  3. hdu 5067 Harry And Dig Machine (状态压缩dp)

    题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意 ...

  4. HDU 5067 Harry And Dig Machine:TSP(旅行商)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题意: 给你一个n*m的地图,地图上标着对应位置的石子数.你从左上角出发,每次可以向上下左右四个 ...

  5. hdu 5067 Harry And Dig Machine

    http://acm.hdu.edu.cn/showproblem.php?pid=5067 思路:问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少.这就是经典 ...

  6. hdu 1010 Tempter of the Bone(dfs)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. HDU 5113:Black And White(DFS)

    题目链接 题意 给出一个n*m的图,现在有k种颜色让你对这个图每个格子染色,每种颜色最多可以使用col[i]次,问是否存在一种染色方案使得相邻格子的颜色不同. 思路 以为是构造题,结果是爆搜.对于每一 ...

  8. HDU 6060:RXD and dividing(DFS)

    题目链接 题意 给出n个点,要把除1以外的点分成k个集合,然后对于每个集合要和1这个点一起求一个最小生成树,然后问这k个最小生成树的最大总和是多少. 思路 因为每个集合都包含1这个点,因此对于每个点都 ...

  9. HDU 2181 哈密顿绕行世界问题(DFS)

    Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数, ...

随机推荐

  1. CactiEZ 中文版V10.1安装使用以及139邮箱短信报警设置

    说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合Cacti等相关软件,重新编译而成的一个操作系统!   说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合 ...

  2. 使用nw.js将html项目打包为桌面程序

    首先需要确保电脑已经布置好node.js环境 1.下载并全局安装nw.js npm install nw -g 2.安装nw-builder模块 npm install nw-builder -g 3 ...

  3. java日期处理总结(二)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzUAAAG1CAIAAABPoU1KAAAgAElEQVR4nOy9e1xU1d747znP9/V9nu

  4. java事件处理4(焦点,键盘

    FocusEvent焦点事件 接口 addFocusListener(FocusListener listener) 有两个方法 public void focusGains(FocusEvent e ...

  5. DSP TMS320C6000基础学习(6)—— gel文件

    什么是gel文件?gel文件能干什么? gel全称General Extended Language,即通用扩展语言文件,gel文件中由类似C语言的代码构成,gel语言是一种解释性语言,gel文件扩展 ...

  6. jquery实现定时调度(倒计时)

    工作需要实现了倒计时的脚本,代码如下: /** * 倒计时 * @param infoId :信息显示的id 最好是用span包裹 * @param callback:倒计时结算后的回调 */ fun ...

  7. phpstudy虚拟主机配置

    <新手篇,开发者直接配置服务器配置文件即可> 很多时候我们从网下或通过其他途径下载源代码到本地Web目录下出现无法访问的案例: 具体的问题是程序路由路径方面做了手脚,把localhost当 ...

  8. smarty 中时间格式化的用法

    大家都知道PHP中输出时间和日期可以用 date("Y-m-d H:i:s",时间戳)  , 但是在smarty模板中,$time|date_format:'%Y-%m-%d %H ...

  9. jQuery 鼠标拖拽排序

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  10. 信息安全实验三:privilege-separation

    title: privilege-separation date: 2016-01-12 14:40:04 categories: tags: --- Exercise1 In order to ga ...