HDOJ 1010-Tempter of the Bone【dfs】
2016-07-27 20:13
357 查看
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 106497 Accepted Submission(s): 28960
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
[align=left]Sample Input[/align]
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
[align=left]Sample Output[/align]
NO
YES
#include<cstdio> #include<cstring> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; int dx[4]={0,0,-1,1}; int dy[4]={1,-1,0,0}; int n,m,t; char map[55][55]; bool vis[55][55]; int kx,ky,ex,ey; bool ans1; int abs(int p) { return p>=0?p:-p; } void DFS(int x,int y,int z){ if(ans1==true) return ; if(map[x][y]=='D'&&z==t){ ans1=true; return ; } int mindis=abs(x-ex)+abs(y-ey); if(mindis>t-z||(mindis+ t-z )%2!=0) return ; for(int i=0;i<4;i++){ int xx=x+dx[i]; int yy=y+dy[i]; if(map[xx][yy]!='X'&&xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]){ vis[xx][yy]=true; DFS(xx,yy,z+1); vis[xx][yy]=false; } } } int main() { while(scanf("%d%d%d",&n,&m,&t)&&(n||m||t)){ memset(vis,false,sizeof(vis)); for(int i=0;i<n;i++){ scanf("%s",map[i]); for(int j=0;j<m;j++){ if(map[i][j]=='S') kx=i,ky=j; if(map[i][j]=='D') ex=i,ey=j; } } ans1=false; vis[kx][ky]=true; DFS(kx,ky,0); if(ans1){ printf("YES\n"); } else{ printf("NO\n"); } } return 0; }
相关文章推荐
- python ldle是什么?
- Fragment的pause和resume
- 安卓aidl夸进程通信的小例子,
- hdu 5616 Jam's balance ( 动态规划 )
- linux下怎么挂载U盘
- 复习4
- LintCode111 之爬楼梯
- 实现上拉加载更多的SwipeRefreshLayout
- UVA 10566 Crossed Ladders (几何+二分)
- ACL
- poj 1426 Find The Multiple
- 【Basic computer】-----Java Spring :Injection Objects (注入对象篇)
- 2016/07/27 选择
- Request.QueryString
- Python是什么类型的语言?
- shell编程
- Property (属性动画)
- IOS中 UIFont 字体大全
- 7月份学习内容
- oracle的SQL语言