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
 
题目意思:n行m列规模的迷宫,小狗要在t秒内从S点到达D点,问能否完成。
 
解题思路:搜索的一道很经典的例题。注意两点:
1.需要剪枝,这里有两处剪枝,一个是路径剪枝,一个是奇偶剪枝。路径剪枝一看就明白,关于奇偶剪枝这个给出说明https://www.cnblogs.com/wkfvawl/p/9337156.html
 
2.回退过程中现场的恢复。如果当前搜索方向行不通,该搜索过程要结束了,但并不代表其他的搜索方向也行不通,所以回退的时候必须还原到原来的状态,保证其他搜索过程不受影响。
 
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char maps[][];
int n,m,t;
int di,dj;//出口
int flag;//是否成功脱逃
int dir[][]= {{,-},{,},{,},{-,}}; //四方搜索
void DFS(int x,int y,int cnt)
{
int i,temp;
int a,b;
if (x>n || y>m || x<= || y<=)//越出边界便不搜索
{
return;
}
if (x==di && y==dj && cnt==t) //时间正好的时候才能逃生
{
flag=;
return;
}
temp=abs(t-cnt)-(abs(di-x)+abs(dj-y));
//计算当前到终点的最短路与还需要的时间差,若小于0则路径剪枝
if (temp< || temp%)//temp如果是奇数的话也要剪枝
{
return;//不符合条件
}
for (i=; i<; i++)
{
a=x+dir[i][];
b=y+dir[i][];
if (maps[a][b]!='X')
{
maps[a][b]='X';//前进方向!
DFS(a,b,cnt+);//搜索该点
if (flag)
{
return;
}
maps[a][b]='.';//后退方向!恢复现场!
}
}
return ;
}
int main()
{
int i,j;
int wall;
int si,sj;//起点
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
if(n==&&m==&&t==)
{
break;
}
getchar();
wall=;
for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
scanf("%c",&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++;
}
}
getchar();
}
if(n*m-wall<=t)//路径剪枝,当走完不含墙的迷宫都还不到t时间将不能逃生
{
printf("NO\n");
continue;
}
flag=;
maps[si][sj]='X';//刷为x
DFS(si,sj,);
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
}
 

Tempter of the Bone HDU 1010(DFS+剪枝)的更多相关文章

  1. Tempter of the Bone HDU - 1010(dfs)

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

  2. HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝

    传送门: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1010 ZOJ:http://acm.zju.edu.cn/onlinejudge/showPr ...

  3. Tempter of the Bone HDU - 1010

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

  4. hdu 1010 dfs搜索

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

  5. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

  6. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  7. 题目1461:Tempter of the bone(深度优先遍历DFS)

    题目链接:http://ac.jobdu.com/problem.php?pid=1461 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  8. hdu 4109 dfs+剪枝优化

    求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己添加的)出发,0到1~n个节点之间的距离为1.mt[i]表示从0点到第i个节点眼下所得的最长路径 #include<iost ...

  9. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

随机推荐

  1. jQuery之scroll用法实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. table(表格)中的标签和属性

    1.表格由 <table> 标签来定义.行( <tr> ),单元格(<td> ) 字母 td 指表格数据(table data),即数据单元格的内容.数据单元格可以 ...

  3. git pull 发生冲突解决办法

    冲突原因:远程仓库的同一个文件的代码,和本地的文件代码不一样 解决办法 : 1.git stash (把本地冲突的代码隐藏) 2.git pull 3.git stash pop (将隐藏的和pull ...

  4. [修正] Firemonkey SpeedButton 鼠标移开按钮后 IsPressed 为 False 的问题

    未修正: 修正代码: 请将 FMX.StdCtrls.pas 复制到自己的工程目录下,再修改如下代码: procedure TCustomButton.RestoreButtonState; begi ...

  5. 利用.htaccess文件将多个子域名解析至对应的子目录

    对于不支持子域名解析但是支持 .htaccess 的主机来说,这个功能就非常有用了 假设有主域名 ppios.com,子域名 yspx.ppios.com 和 ask.ppios.com,设置结果为访 ...

  6. vs code 写C心得

    用命令行的话可能比较简单: g++ -o [目标文件名] [原文件名] 然后在当前路径下直接执行这个文件,查看程序执行结果 例如: g++ -o a.out test.cpp ./a.out 默认是a ...

  7. 004---IO模型

    io模型 同步.异步.阻塞.非阻塞概念 同步:发出一个功能调用时,在没有得到结果之前,该调用就不会返回,原地等待 异步:相反,不需要等待 阻塞:调用结果返回之前,当前线程会被挂起,如io操作,只有在得 ...

  8. Altera三速以太网IP核使用(下篇)--- 百兆网接口设计与使用

    MAC IP核的主要作用是:实现数据链路层协议,分为TX方向与RX方向,TX方向实现的是在原包文的前面加上7个55和1个D5,RX方向则相反.在使用这个 MAC IP核之前,首先确认下自己使用的网卡是 ...

  9. tensorflow 模型权重导出

    tensorflow在保存权重模型时多使用tf.train.Saver().save 函数进行权重保存,保存的ckpt文件无法直接打开,不利于将模型权重导入到其他框架使用(如Caffe.Keras等) ...

  10. 20155325 信息安全技术 实验二 Windows口令破解

    内容一览 实验结果 遇到的问题 思考题 详细步骤与解析(为了使存做笔记资料而做) 实验结果 字典破解 暴力破解 -不同密码强度的破解时间比较 用户名 密码 破解方式 破解时间 TEST (年月日) 字 ...