题目链接: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. 使用谷歌Z生成条形码以及二维码

    下载地址:http://zxingnet.codeplex.com/ zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 首先下载二进制dll文件,引入工程: using Sy ...

  2. android 实现照相功能 照片存放在SID卡中,将照片显示在Image中

    protected static final int CAMERA_RESULT = 0; private String fileName; private Button takePhotoBn; p ...

  3. java 判断一个字符串是否为纯数字

    if (getUid().matches("[0-9]+")) { Log.v("纯数字");} else { Log.v("非纯数字"); ...

  4. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  5. [bzoj3712][PA2014]Fiolki_倍增LCA

    Fiolki bzoj-3712 PA-2014 题目大意:题目链接. 注释:略. 想法: 神题! 我们建树:对于一次倾倒操作,我们弄一个新的大瓶子作为两个合并瓶子的父亲节点,与两个瓶子相连. 对于一 ...

  6. POJ2632 Crashing Robots 解题报告

    Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...

  7. MySQL查询去重语句

    1.distinct select count(distinct CName) from Course select count(CName) from (select distinct CName ...

  8. cache and database

    This article referenced from http://coolshell.cn/articles/17416.html We all know that high concurren ...

  9. EC2的维护更新-总结篇及有效经验分享

    2014年10月11日 号,我们对不到10%的EC2实例的完毕了重新启动.来预防不论什么与Xen安全通报(XSA-108)相关的安全风险. 日之前都有义务遵守相关问题的保密要求.直到它被向公众公布. ...

  10. Django打造大型企业官网(八)

    4.16.侧边栏标题和广告位布局完成 templates/news/index.html <div class="sidebar-wrapper"> <div c ...