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.需要剪枝,这里有两处剪枝,一个是路径剪枝,一个是奇偶剪枝。路径剪枝一看就明白,关于奇偶剪枝这个给出说明Gate

2.回退过程中现场的恢复。如果当前搜索方向行不通,该搜索过程要结束了,但并不代表其他的搜索方向也行不通,所以回退的时候必须还原到原来的状态,保证其他搜索过程不受影响。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char maps[9][9];
int n,m,t;
int di,dj;//出口
int flag;//是否成功脱逃
int dir[4][2]= {{0,-1},{0,1},{1,0},{-1,0}}; //四方搜索
void DFS(int x,int y,int cnt)
{
int i,temp;
int a,b;
if (x>n || y>m || x<=0 || y<=0)//越出边界便不搜索
{
return;
}
if (x==di && y==dj && cnt==t) //时间正好的时候才能逃生
{
flag=1;
return;
}
temp=abs(t-cnt)-(abs(di-x)+abs(dj-y));
//计算当前到终点的最短路与还需要的时间差,若小于0则路径剪枝
if (temp<0 || temp%2)//temp如果是奇数的话也要剪枝
{
return;//不符合条件
}
for (i=0; i<4; i++)
{
a=x+dir[i][0];
b=y+dir[i][1];
if (maps[a][b]!='X')
{
maps[a][b]='X';//前进方向!
DFS(a,b,cnt+1);//搜索该点
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==0&&m==0&&t==0)
{
break;
}
getchar();
wall=0;
for(i=1; i<=n; i++)
{
for(j=1; 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=0;
maps[si][sj]='X';//刷为x
DFS(si,sj,0);
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}

#1010:Tempter of the Bone(DFS + 奇偶剪枝)的更多相关文章

  1. 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 ...

  2. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

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

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

  5. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

  6. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

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

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

  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. HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

  10. 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. 黑客玩具入门——9、Burp Suite

    Burp Suite是一款集成化的渗透测试工具,包含了很多功能,可以帮助我们快速完成对web应用程序的渗透测试和攻击.Burp Suite是由Java语言编写,因为Java是可以跨平台的,所以Burp ...

  2. CoreFlex框架发布 `0.1.1`

    CoreFlex框架发布 0.1.1 框架描述 CoreFlex是一个支持.NET 6,.NET 7,.NET 8的快速开发框架,也提供MasaFramework相关框架的集成提供更多功能模块, 集成 ...

  3. C# 从代码入门 Mysql 数据库事务

    目录 生成数据库数据 Mysql 数据库事务基础 数据库的并发一致性问题 数据库事务的隔离级别 BeginTransaction() 和 TransactionScope 的区别 BeginTrans ...

  4. 3 与HTTP相关的各种协议

    目录 1 TCP/IP 2 DNS 3 URI/URL 4 HTTPS 5 代理 1 TCP/IP TCP/IP是网络世界最常用协议,HTTP通常运行在TCP/IP提供的可靠传输基础上 IP 协议是& ...

  5. 神经网络优化篇:机器学习基础(Basic Recipe for Machine Learning)

    机器学习基础 下图就是在训练神经网络用到的基本方法:(尝试这些方法,可能有用,可能没用) 这是在训练神经网络时用到地基本方法,初始模型训练完成后,首先要知道算法的偏差高不高,如果偏差较高,试着评估训练 ...

  6. ceph集群搭建详细教程(ceph-deploy)

    ceph-deploy比较适合生产环境,不是用cephadm搭建.相对麻烦一些,但是并不难,细节把握好就行,只是命令多一些而已. 实验环境 服务器主机 public网段IP(对外服务) cluster ...

  7. JavaScript 常见错误与异常处理

    一.为什么要了解常见JS错误 1.调试和故障排除: 了解常见的JavaScript错误可以帮助你更好地调试和故障排除代码.当你遇到错误时,能够快速识别错误类型并找到解决方法,可以节省大量的时间和精力. ...

  8. android ProgressBar样式

    实现进度条由浅黄(#ffff33)到深黄色(#ff6600)的渐变样式. 与进度条自动从0加载到99,进度条每次加1 android:max:进度条的最大值. android:progressDraw ...

  9. 不是银趴~是@Import!

    首先我们要明确:@Import 注解是 Spring 提供的. 然后我们看一下该注解的官方注释: Indicates one or more component classes to import - ...

  10. 华为云MetaStudio全新升级,盘古数字人大模型助力数字人自由

    摘要:基于盘古大模型能力,华为云MetaStudio数字内容生产线全新升级,推出数字人模型生成服务和模型驱动服务. 近日,华为开发者大会2023 ( Cloud ) 在东莞拉开帷幕.基于盘古大模型能力 ...