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
 
题目大意:还是迷宫的题目,S是起点,D是终点,X是墙,每秒一个位置,当好T秒走到
该题不再要求最优解,而是判定符合条件的路径,因此用深度优先搜索。
里面有关于剪枝的运用(因为如果不剪枝可能会超时),在54行,即判断起始点和时间的关系。
 #include <iostream>
#include<cstdio>
using namespace std;
char maze[][];//保存地图信息
int n,m,t;//地图大小n*m,起点到终点能否恰好为t
bool success;//是否找到所需状态标记
int go[][]={//s四个方向行走坐标差
-,,
,,
,,
,-
}; void DFS(int x,int y,int time){
for(int i=;i<;i++){
int nx=x+go[i][];//枚举四个相邻位置
int ny=y+go[][i];
if(nx< || nx>n || ny< || ny>m)//地图外
continue;
if(maze[nx][ny]=='X')//碰墙
continue;
if(maze[nx][ny]=='D'){//到终点
if(time+==t){//所用时间恰好为t
success=true;//搜索成功
return;
}
else
continue;
}
maze[nx][ny]='X';//该点设为墙
DFS(nx,ny,time+);//递归扩展该状态
maze[nx][ny]='.';//把原来的路改回来
if(success)
return;
}
} int main(){
while(scanf("%d %d %d",&n,&m,&t)!=EOF){
if(n== && m== && t==)
break;
for(int i=;i<=n;i++)
scanf("%s",maze[i]+);
success=false;
int sx,sy;
for(int i=;i<=n;i++){//寻找D的坐标
for(int j=;j<=m;j++){
if(maze[i][j]=='D'){
sx=i;
sy=j;
}
}
}
for(int i=;i<=n;i++){//找到S后,判断S和D的奇偶关系是否和t相符
for(int j=;j<=m;j++){
if(maze[i][j]=='S' && (i+j)%==((sx+sy)%+t%)%){
maze[i][j]='X';//起始点设为墙
DFS(i,j,);//递归扩展初始状态
}
}
}
puts(success==true?"YES":"NO");
}
return ;
}

//说实话我真的不是很懂43行,为什么要+1。。。请在评论区告诉我,多谢!

Tempter of the Bone——DFS(王道)的更多相关文章

  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. Tempter of the Bone(dfs奇偶剪枝)

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

  4. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  5. HDOJ.1010 Tempter of the Bone (DFS)

    Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...

  6. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  7. zoj 2110 Tempter of the Bone (dfs)

    Tempter of the Bone Time Limit: 2 Seconds      Memory Limit: 65536 KB The doggie found a bone in an ...

  8. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  9. Tempter of the Bone dfs+剪枝

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

随机推荐

  1. 搜索引擎--范例:谈谈django--mysql数据库的一些常用命令

    现在基本没有什么能离得开数据库了,django我一直用的都是mysql的数据库,这次和大家说说django--mysql数据库的一些常用命令吧 1:命令行登陆mysql C:\Users\Admini ...

  2. DELPHI数组,指针,字符串转换的例子

    关于数组,指针,字符串转换的例子 var   aa:   array [0..5] of Char;   bb:Pointer;   cc:string;   dd:PChar; procedure ...

  3. delphi.指针.应用----应用重要 多看 多练

    来自:http://www.cnblogs.com/qiusl/p/4026459.html ----------------------------------------------------- ...

  4. [译]Java8的函数式接口

    Java8引入了 java.util.function 包,他包含了函数式接口,具体的描述在以下api说明文档中: 函数式接口为lambda表达式和方法引用提供目标类型.每个函数式接口有一个单独的抽象 ...

  5. k8s的持久化存储PV&&PVC

    1.PV和PVC的引入 Volume 提供了非常好的数据持久化方案,不过在可管理性上还有不足. 拿前面 AWS EBS 的例子来说,要使用 Volume,Pod 必须事先知道如下信息: 当前 Volu ...

  6. Python 进阶 之 闭包变量

    在闭包内访问外部变量的方法有两种: 1:变量前加nonlocal(仅支持Python3) 2:用列表来代替变量.即使是只有一个元素的数组,否则会报错.

  7. AppScan8.7的两个细节亮点

    1.增加了对红极一时的Struts2的远程代码执行漏洞的检测 2.增加了对篡改价格这类应用逻辑缺陷的检测

  8. PreparedStatement 和 Statement 实现基本的批处理

    批处理:若需要对数据库进行多步操作,则就没必要每次都和数据库进行一次通信,这样很消耗资源和时间.则需要将操作进行批处理:    Statement方式来实现批处理        优点:         ...

  9. SQL 插入多行数据语句整理

     参考别人的,希望对大家有用. 1.只是插入简单的有限行数据时用: insert 要插入的表名(列名1,列名2,....) select '列名1需要的数据','列名2需要的数据',... union ...

  10. Codeforces #432 Div2 D

    #432 Div2 D 题意 给出一些数字,如果这些数字的的 \(gcd\) 不为1则称这些数字 \(good\). 可以有两种操作: 花费 x 删掉一个数 花费 y 将一个数加 1 问使这些数 \( ...