题目链接: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. set(集合)数据类型【七】

    一.概述:(类似于Java的Set,不允许有重复元素) 在Redis中,我们可以将Set类型看作为没有排序的字符集合,和List类型一样,我们也可以在该类型的数据值上执行添加.删除或判断某一元素是否存 ...

  2. [bzoj2561]最小生成树_网络流_最小割_最小生成树

    最小生成树 bzoj-2561 题目大意:题目链接. 注释:略. 想法: 我们发现: 如果一条权值为$L$的边想加入到最小生成树上的话,需要满足一下条件. 就是求出原图的最小生成树之后,这个边当做非树 ...

  3. python函数值传递还是引用传递

    c/c++中有值传递引用传递的区别.但是python中是值传递还是引用传递呢?首先看python中对变量的定义 "python中变量是指向某个内存的, 而内存中的内容是不可变的." ...

  4. 必测的支付漏洞(一)——使用fiddler篡改支付金额

    互联网产品中常会遇到支付功能,测试人员测试这部分功能时一定要重视,因为如果这部分出现了较严重的bug,将会给公司带来不小的经济损失!如果你测出了问题领导也一定会高兴的!因此测试优先级很高,但具有一定难 ...

  5. python:functools之partial

    示例:from operator import addimport functoolsprint add(1,2) #3add1 = functools.partial(add,1)print add ...

  6. android 深入浅出 群内“每日一问” 问答总结

    永远不变的就是变. 俗话说的好,环境改变人生. 常常面对的是一群积极奋进的人,那么你的心态和生活也会变的充满斗志.青春在于折腾,趁我们还年轻,拿出你的激情.踏着泪水载着梦,才干拥有自己的一片天空. 上 ...

  7. Codeforces 479B. Towers 暴力

    纯暴力..... B. Towers time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Linux 下添加 Eclipse 桌面图标

    1. sudo gedit  /usr/share/applications/eclipse.desktop 2. 向eclipse .desktop中添加以下内容: [Desktop Entry] ...

  9. Java对话框总结

    总结起来非常easy: 1,对话框类型:消息.确认,选项.输入 2,选择图标:错误,信息.警告.问题,无或者自己定义 3,选择消息:字符串,图标.自己定义组件或者他们的集合 4.对于确认对话框,选择选 ...

  10. MySQL-导入与导出

    CSV文件导入MySQL LOAD DATA INFILE语句允许您从文本文件读取数据,并将文件的数据快速导入数据库的表中. 导入文件操作之前,需要准备以下内容: 一.将要导入文件的数据对应的数据库表 ...