题目链接: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. codevs1154 能量项链

    题目描述 Description 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子 ...

  2. PatentTips – EMC Virtual File System

    BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention generally relates to net ...

  3. VIM使用技巧14

    经常使用vim的童鞋可能会注意到,实际操作过程中,处于插入模式中是非常少的,更多的是查看和浏览,偶尔修改即可.因此,快速从插入模式退出进入普通模式,就显得非常重要.主要有以下四种方式: 一.在插入模式 ...

  4. 积累——SQLCommand命令

    SQLcommand表示要对SQL数据库运行的一个 T-SQL 语句或存储过程.以便运行大量操作或处理数据库结构. 在对数据库訪问的时候,就经经常使用到这个.看看它是怎么做到的吧! 一.属性 Comm ...

  5. console command

    routes/console.php copy一个默认的 Artisan::command('hello', function () { $this->comment('hello world' ...

  6. 条款八: 写operator new和operator delete时要遵循常规

    自己重写operator new时(条款10解释了为什么有时要重写它),很重要的一点是函数提供的行为要和系统缺省的operator new一致.实际做起来也就是:要有正确的返回值:可用内存不够时要调用 ...

  7. Samba.conf案例 Ubuntu

    # Sample configuration file for the Samba suite for Debian GNU/Linux.## This is the main Samba confi ...

  8. Yarn 的工作流-创建一个新项目

    Microsoft Windows [版本 10.0.16299.125] (c) Microsoft Corporation.保留所有权利. C:\Users\Administrator>cd ...

  9. pojWindow Pains(拓扑排序)

    题目链接: 啊哈哈,点我点我 题意: 一快屏幕分非常多区域,区域之间能够相互覆盖,要覆盖就把属于自己的地方所有覆盖. 给出这块屏幕终于的位置.看这块屏幕是对的还是错的.. 思路: 拓扑排序,这个简化点 ...

  10. mysql连接字符串,连接字段结果集

    archie2010 ${原来姹紫嫣红开遍,似这般都付与扣钉八哥} mysql连接字符串,连接字段结果集 select CONCAT('My', 'S', 'QL连接字符串') as MySql; 连 ...