HDU 1010 Tempter of the Bone(dfs)
Posted cake-lover-77
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1010 Tempter of the Bone(dfs)相关的知识,希望对你有一定的参考价值。
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.
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.
‘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
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 6 int n,m,t,x1,y3,x2,y2,flag,wall; 7 char Map[10][10]; 8 int vis[10][10]; 9 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; //四个方向 10 void dfs(int x,int y,int step) 11 { 12 if(flag) return; 13 if(x==x2&&y==y2&&step==t) //找到就返回 14 { 15 flag=1; 16 return; 17 } 18 int re=t-step-abs(x2-x)-abs(y2-y); //剪枝 19 if(re<0||re&1) return; 20 for(int i=0;i<4;i++) 21 { 22 int xx=x+dir[i][0]; 23 int yy=y+dir[i][1]; 24 if(xx<0||xx>=n||yy<0||yy>=m) //出边界不往下搜 25 continue; 26 if(Map[xx][yy]!=‘X‘&&vis[xx][yy]==0) 27 { 28 vis[xx][yy]=1; 29 dfs(xx,yy,step+1); 30 if(flag) return; 31 vis[xx][yy]=0; //回溯 32 } 33 } 34 return; 35 } 36 37 int main() 38 { 39 while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(n&&m&&t)) 40 { 41 wall=0,flag=0; 42 memset(vis,0,sizeof(vis)); 43 for(int i=0;i<n;i++) 44 scanf("%s",Map[i]); 45 for(int i=0;i<n;i++) 46 { 47 for(int j=0;j<m;j++) 48 { 49 if(Map[i][j]==‘S‘) x1=i,y3=j; 50 if(Map[i][j]==‘D‘) x2=i,y2=j; 51 if(Map[i][j]==‘X‘) wall++; 52 } 53 } 54 if(t>n*m-wall-1) //剪枝,如果剩下的路加起来都比时间要小,那肯定不能在规定时间正好走到 55 { 56 printf("NO "); 57 continue; 58 } 59 vis[x1][y3]=1; 60 dfs(x1,y3,0); 61 if(flag==1) 62 printf("YES "); 63 else 64 printf("NO "); 65 } 66 return 0; 67 }
以上是关于HDU 1010 Tempter of the Bone(dfs)的主要内容,如果未能解决你的问题,请参考以下文章
题解报告:hdu 1010 Tempter of the Bone
HDU 1010 Tempter of the Bone(dfs)