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. 打包工具 Inno Setup 5

    转载: http://www.360doc.com/content/13/0327/13/4221543_274235049.shtml

  2. 常见协议基础知识总结--DHCP协议

    DHCP动态主机配置协议,简单点说,就是提供了自动获取ip地址的功能,基于四层的UDP协议: 以下描述此协议的整个工作流程: (1) 客户端发送discovery报文,二三层广播报文,源ip地址全0: ...

  3. Google的C++开源代码项目

    Google的C++开源代码项目 http://www.open-open.com/lib/view/open1413873531356.html v8  -  V8 JavaScript Engin ...

  4. Linux指令详解useradd groupadd passwd chpasswd chage 密码修改

    Linux指令详解useradd groupadd passwd chpasswd chage 密码修改 http://speediness.blog.51cto.com/760841/1783661 ...

  5. 错误 NETSDK1068: 框架依赖型应用程序主机需要一个至少 “netcoreapp2.1” 的目标框架

    错误 NETSDK1068: 框架依赖型应用程序主机需要一个至少 “netcoreapp2.1” 的目标框架 我有一个ASP.NET Core 2网站应用程序,编译运行都没有问题,但是发布时却出了错, ...

  6. JDBC连接池(数据源)

    自定义连接池:用装饰设计模式将原连接的close方法改造成将连接还回数据源:装饰设计模式:http://www.cnblogs.com/tongxuping/p/6832518.html: 开源数据库 ...

  7. [解决] win7能上网,ubuntu14.04不行

    更新驱动 http://www.realtek.com.tw/downloads/downloadsView.aspx?Langid=1&PNid=13&PFid=5&Leve ...

  8. OA项目实战(一) 概述

    从本篇博文开始,我为大家简单介绍一下办公自动化(Office Automation,简称OA). 1.OA简介     OA是将现代办公和计算机网络的功能相结合的一种新型办公方式,是针对日常工作,改变 ...

  9. AOJ 2266 Cache Strategy(费用流)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2266 [题目大意] 有M个桶,N个球,球编号为1到N,每个球都有重量 ...

  10. 【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流

    #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using ...