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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <iomanip> 13 using namespace std; 14 const int INF=0x5fffffff; 15 const int MS=8; 16 const double EXP=1e-8; 17 18 int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0} }; 19 20 int N,M,T; 21 bool escape; 22 char plat[MS][MS]; 23 int vis[MS][MS]; //可以用X来代替vis 24 int sx,sy,ex,ey; 25 int wall; 26 bool input() 27 { 28 scanf("%d %d %d",&N,&M,&T); 29 if(N==0) 30 return false; 31 getchar(); 32 wall=0; 33 for(int i=0;i<N;i++) 34 { 35 for(int j=0;j<M;j++) 36 { 37 scanf("%c",&plat[i][j]); 38 if(plat[i][j]=='S') 39 { 40 sx=i; 41 sy=j; 42 } 43 if(plat[i][j]=='D') 44 { 45 ex=i; 46 ey=j; 47 } 48 if(plat[i][j]=='X') 49 wall++; 50 } 51 getchar(); 52 } 53 memset(vis,0,sizeof(vis)); 54 escape=false; 55 return true; 56 } 57 58 void dfs(int x,int y,int cur) 59 { 60 vis[x][y]=1; 61 if(cur==T) 62 { 63 if(plat[x][y]=='D') 64 escape=true; 65 return ; 66 } 67 if(cur>T) 68 return ; 69 int r=(T-cur)-abs(x-ex)-abs(y-ey); // 剪枝 70 if(r<0||r%2) 71 return ; 72 int xx,yy; 73 for(int i=0;i<4;i++) 74 { 75 xx=x+dir[i][0]; 76 yy=y+dir[i][1]; 77 if((xx>=0&&xx<N&&yy>=0&&yy<M&&plat[xx][yy]=='.'&&vis[xx][yy]==0)||plat[xx][yy]=='D') 78 { 79 dfs(xx,yy,cur+1); 80 if(escape) 81 return ; //剪枝 82 vis[xx][yy]=0; 83 84 } 85 } 86 return ; 87 } 88 89 int main() 90 { 91 while(input()) 92 { 93 if(N*M-wall<=T) //搜索前剪枝 94 { 95 printf("NO "); 96 continue; 97 } 98 dfs(sx,sy,0); 99 if(escape) 100 printf("YES "); 101 else 102 printf("NO "); 103 } 104 return 0; 105 }