题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;  'S': the start point of the doggie;  'D': the Door; or '.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
解题思路:掌握了两个重要的小技巧:①奇偶剪枝;②路径剪枝。注释详解在代码里。这里直接贴一篇大牛的讲解吧!很详细,也很容易懂。链接:hdu1010详细题解
AC代码:
 #include<bits/stdc++.h>
using namespace std;
//迷宫地图
//X: 墙壁,小狗不能进入
//S: 小狗所处的起始位置
//D: 迷宫的门
//. : 空的方格,表示可以经过的点
char maps[][];
int n,m,t,di,dj;//n行,m列,t是规定时间内到达,(di,dj):门的位置
bool escape;//表示是否逃脱
int dir[][]={{-,},{,},{,-},{,}};//上、下、左、右(方向数组)
void dfs(int si,int sj,int cnt)//表示起始位置为(si,sj),要求在第cnt秒到达门的位置
{
if(si>n || sj>m || si<= || sj<=)return;//处理越界情况,直接退出
if(si==di && sj==dj && cnt==t){escape=true;return;}//到达出口
int tmp=(t-cnt)-abs(si-di)-abs(sj-dj);//abs(x-ex)+abs(y-ey)表示现在所在的格子到目标格子的距离(不能走对角线)奇偶剪枝的核心代码
if(tmp< || tmp&)return;//t-cnt是实际还需要的步数,将他们做差,如果tmp<0或者tmp为奇数,那就不可能到达!
for(int i=;i<;i++){//深搜当前方向的每个方向
if(maps[si+dir[i][]][sj+dir[i][]]!='X'){
maps[si+dir[i][]][sj+dir[i][]]='X';//标记为墙壁,表示不能再走过
dfs(si+dir[i][],sj+dir[i][],cnt+);//深搜
if(escape)return;//若找到,直接返回
maps[si+dir[i][]][sj+dir[i][]]='.';//同时还原本来不是墙但被标记的墙(回溯)
}
}
return;
}
int main()
{
int si,sj;//表示起点的坐标
while(cin>>n>>m>>t && (m+n+t)){
int wall=;
for(int i=;i<=n;i++){//从1开始,因为在遍历该点的四个方向时才不会越界的危险
for(int j=;j<=m;j++){
cin>>maps[i][j];
if(maps[i][j]=='S'){si=i;sj=j;}//标记小狗的位置
else if(maps[i][j]=='D'){di=i;dj=j;}//标记出口的位置
else if(maps[i][j]=='X')wall++;//计算墙的数量
}
}//如果剩下的路径长度小于所需t步,注意n*m-wall==t表示地图上可以走的路径长度比t少1,因为此时只有n*m-wall-1条边,路径剪枝核心代码
if(n*m-wall<=t){cout<<"NO"<<endl;continue;}
escape=false;//标记为false,表示还没找到
maps[si][sj]='X';//直接标记出发点为墙,表示不能返回
dfs(si,sj,);//从起始位置深搜
if(escape)cout<<"YES"<<endl;//逃脱成功
else cout<<"NO"<<endl;
}
return ;
}

题解报告:hdu 1010 Tempter of the Bone的更多相关文章

  1. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  2. 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 ...

  3. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

  4. 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 ...

  5. 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 ...

  6. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

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

  7. hdu 1010 Tempter of the Bone 深搜+剪枝

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

  8. 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 ...

  9. hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)

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

随机推荐

  1. Linux rz的使用

    RZ是Linux提供的上传的命令,基于XMODEM/YMODEM/ZMODEM协议. 让我们来测试一下参数吧: 先准备一个文件,就叫test.txt吧,内容如下: one line rz -+  如果 ...

  2. Linux下汇编语言学习笔记11 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  3. codeforces 691E(矩阵乘法)

    E. Xor-sequences time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  4. SAP EP 设置Portal别名安全模式

    Securing the Portal Alias Cookie Context We recommend that you set the portal alias cookie to be del ...

  5. pc3-12800

    PC3-12800=DDR3 1600 PC3代表DDR3.12800是用带宽来命名,1600*64/8=12800,1600是DDR等效频率.

  6. Elasticsearch学习系列之多文档操作mget

    测试数据 GET /library/books/1 { "_index": "library", "_type": "books& ...

  7. 【转】深入理解javascript作用域——词法作用域和动态作用域

    前面的话 大多数时候,我们对作用域产生混乱的主要原因是分不清楚应该按照函数位置的嵌套顺序,还是按照函数的调用顺序进行变量查找.再加上this机制的干扰,使得变量查找极易出错.这实际上是由两种作用域工作 ...

  8. golang time.Duration()的问题解疑

    原文:  How to multiply duration by integer? 看到golang项目中的一段代码, ---------------------------------------- ...

  9. 单条insert

    ugc_l = browser.find_elements_by_class_name('ugc-item') try: myl = [{'statistics': i.text.replace('阅 ...

  10. Ubuntu 16.04下安装MacBuntu 16.04 TP 变身Mac OS X主题风格

    Ubuntu 16.04下安装MacBuntu 16.04 TP 变身Mac OS X主题风格 sudo add-apt-repository ppa:noobslab/macbuntu sudo a ...