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. PHP中__get()和__set()的用法实例详

    刚刚看到一个对我有用的文章,我就把它摘抄下来了.                                                                        php面 ...

  2. Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思维】

    传送门:http://codeforces.com/contest/1092/problem/D1 D1. Great Vova Wall (Version 1) time limit per tes ...

  3. 移动端 Touch 事件

    在移动端页面开发时,常常会用到touch事件,比如左滑右滑的轮播等.常用的触摸事件有touchstart,touchmove,touchend. 每个事件包含下面三个用于跟踪虎摸的属性: touche ...

  4. cascade rcnn

    在region proposal阶段采用不同的iou. 第一幅图,不同颜色的线是用不同的region proposal的iou阈值,横坐标是region proposal生成的框与gt的原始iou,纵 ...

  5. mybatis逆向工程mbg

    mbg:mybatis generator=mybatis代码生成器 1.看一下项目结构 其中bean文件,mapper接口文件和mapper.xml文件是代码生成器自动生成的. 使用generato ...

  6. 报错Caused by: org.hibernate.AnnotationException: No identifier specified for entity:

    Caused by: org.hibernate.AnnotationException: No identifier specified for entity:. 原因: 1.没有给实体类ID 解决 ...

  7. Impossible WHERE noticed after reading const tables

    阿里云反馈的慢SQL,执行计划返回如下:Impossible WHERE noticed after reading const tables sql很简单: SELECT * FROM deposi ...

  8. JAVA格式化解析日期

  9. 『ACM C++』 Codeforces | 1003C - Intense Heat

    今日兴趣新闻: NASA 研制最强推进器,加速度可达每秒 40 公里,飞火星全靠它 链接:https://mbd.baidu.com/newspage/data/landingsuper?contex ...

  10. chromium之task

    // A task is a generic runnable thingy, usually used for running code on a // different thread or fo ...