Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 151147    Accepted Submission(s): 40285

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

题意:走迷宫,X表示墙,.表示通路,S起点,D终点。问是否能刚好在T秒的时候走到终点(每秒只能走一步,不能往回走),可以往上下左右四个方向走

题解:典型dfs,但要注意剪枝,看上去数据量不大,但不剪枝会T到怀疑人生。有两个地方可以剪枝,第一个是从当前点走到终点的最短距离(曼哈顿距离)比剩余时间大,那么就不可能到达终点了。第二个是剩余时间减去当前点到终点的最短距离是奇数的时候,不可能到达终点。

 #include<bits/stdc++.h>
using namespace std;
int n,m,t;
char s[][];
int dirx[]= {,-,,};
int diry[]= {,,-,};
int sx,sy,ex,ey;
bool dfs(int x,int y,int time)//t表示剩余多少时间
{
if(time==)
{
if(x==ex&&y==ey)return true;
return false;
}
//剩余时间连理想最短路径都不够走
if(time<(abs(ex-x)+abs(ey-y))||(time-abs(ex-x)-abs(ey-y))%==)
return false;
for(int i=; i<; i++)
{
int xx=x+dirx[i];
int yy=y+diry[i];
if(xx<||yy<||xx>=n||yy>=m||s[xx][yy]=='X')continue;
s[xx][yy]='X';
if(dfs(xx,yy,time-))return true;
s[xx][yy]='.'; }
return false; }
int main()
{
while(~scanf("%d%d%d",&n,&m,&t),n+m+t)
{
memset(s,'\0',sizeof(s));
for(int i=; i<n; i++)
{
getchar();
for(int j=; j<m; j++)
{
scanf(" %c",&s[i][j]);
if(s[i][j]=='S')
{
sx=i;
sy=j;
s[i][j]='.';
}
if(s[i][j]=='D')
{
ex=i;
ey=j;
s[i][j]='.';
}
}
} s[sx][sy]='X';
if(dfs(sx,sy,t))printf("YES\n");
else printf("NO\n"); }
return ;
}

hdu1010Tempter of the Bone(迷宫dfs+剪枝)的更多相关文章

  1. 【HDU - 1010】Tempter of the Bone(dfs+剪枝)

    Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱 ...

  2. HDOJ-1010 Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 给出一个n*m的迷宫 X为墙 .为空地 S为起点 D为终点 给出时间T 每走一步花费一单位的时间 走过的空 ...

  3. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

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

  4. hdu1010--Tempter of the Bone(迷宫)

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

  5. Tempter of the Bone(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  6. HDU 1010 Tempter of the Bone (DFS+剪枝)

    题意:从S走到D,能不能恰好用T时间. 析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey), 那么从(x, y)到 ...

  7. Hdu1010Tempter of the Bone 深搜+剪枝

    题意:输入x,y,t.以及一个x行y列的地图,起点‘S’终点‘D’地板‘.’墙壁‘X’:判断能否从S正好走t步到D. 题解:dfs,奇偶性减枝,剩余步数剪枝. ps:帮室友Debug的题:打错了两个字 ...

  8. HDU1010 --- Tempter of the Bone(dfs+剪枝)

    小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t ...

  9. hdu1010Tempter of the Bone(dfs+奇偶剪枝)

    题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...

随机推荐

  1. isset() 与 array_key_exists() 比较

    1.对于数组值的判断不同,对于值为null或''或false,isset返回false,array_key_exists返回true: 2. 执行效率不同,isset是内建运算符,array_key_ ...

  2. web.xml中Listener的作用

    Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次,随web应 ...

  3. Yii2 配置发送邮件

    'components' => [ 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@com ...

  4. VC++和C语言中常见数据类型转换为字符串的方法

    1.短整型(int) itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制 itoa(i,temp,2); ///按二进制方式转换 2.长整型(long) lt ...

  5. java和spring 线程池总结

    1. spring 的线程池 ThreadPoolTaskExecutor @Configuration public class ThreadPoolConfig { @Bean("thr ...

  6. Reading Notes : 180214 计算机的总线结构

    读书<计算机组成原理>,百度百科 基本上接触过计算机的人,都多少知道计算机的具体构成,但是真正能讲明白的却说了很多,本节将讲解一下计算机的基本硬件构成和一些基本信息,简单认识,以后再深入了 ...

  7. PHP中使用foreach时加&符号的用法

    foreach时加&符号:遍历的同时改变原数组即修改数据或者增加数据. $arr = ['a', 'b', 'c']; foreach ($arr as $key => &$va ...

  8. php比较两个数组的差异array_diff()函数

    下面简单介绍php比较两个数组的差异array_diff()函数. 原文地址:小时刻个人技术博客 > http://small.aiweimeng.top/index.php/archives/ ...

  9. mysql 操作表结构

    整理一下对mysql表结构的简单操作,dos窗口模式的,现在基本上都是些图形化操作mysql,像这种命令形式的少了很多,暂时记忆点,以防以后忘记. 从数据库的创建开始: show databases; ...

  10. day 19 反射

    1.isinstance, type, issubclass 的含义 isinstance:  判断你给对象时候是xxx类型的.(向上判断) type: 返回xxx对象的数据类型 issubclass ...