1. 程式人生 > >ZOJ - 2110 Tempter of the Bone

ZOJ - 2110 Tempter of the Bone

scan nat 可能 blocks tip clu esp second ann

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

判斷能否在恰當的時間到達出口,可有用奇偶剪枝(but 我沒用)

因為題目問得式是否存在這樣的可能性,所以應該用深搜做。

技術分享圖片
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string
.h> 4 #include<math.h> 5 #include<algorithm> 6 #include<queue> 7 #include<stack> 8 #include<deque> 9 #include<iostream> 10 using namespace std; 11 typedef long long LL; 12 const double pi=acos(-1.0); 13 const double e=exp(1); 14 struct con{ 15 int x; 16 int y; 17 }con[1000]; 18 int Map[10][10]; 19 int n,m,t; 20 int check[1000]; 21 int way[4][2]={0,1,0,-1,1,0,-1,0}; 22 int flag,step; 23 void DFS(int a,int b) 24 { 25 int i,p,j; 26 if(check[Map[a][b]]!=0||a<0||b<0||a>=n||b>=m||Map[a][b]==-1) 27 { 28 step--; 29 return ; 30 } 31 check[Map[a][b]]=1; 32 if(Map[a][b]==0) 33 { 34 if(step==t) 35 flag=1; 36 else 37 { 38 step--; 39 } 40 check[Map[a][b]]=0; 41 return ; 42 } 43 for(i=0;i<=3;i++) 44 { 45 int x,y; 46 x=a+way[i][0]; 47 y=b+way[i][1]; 48 step++; 49 DFS(x,y); 50 } 51 check[Map[a][b]]=0; 52 step--; 53 54 return ; 55 } 56 57 int main() 58 { 59 int i,p,j; 60 int a,b; 61 char c; 62 while(scanf("%d%d%d",&n,&m,&t)!=EOF) 63 { 64 flag=step=0; 65 p=1; 66 memset(check,0,sizeof(check)); 67 memset(con,0,sizeof(con)); 68 if(n==0&&m==0&&t==0) 69 break; 70 for(i=0;i<n;i++) 71 for(j=0;j<m;j++) 72 { 73 scanf(" %c",&c); 74 if(c==S) 75 { 76 a=i; 77 b=j; 78 Map[i][j]=p++; 79 } 80 else if(c==X) 81 Map[i][j]=-1; 82 else if(c==D) 83 Map[i][j]=0; 84 else 85 Map[i][j]=p++; 86 } 87 88 DFS(a,b); 89 if(flag==1) 90 printf("YES\n"); 91 else 92 printf("NO\n"); 93 } 94 return 0; 95 } 96 /* 97 4 4 4 98 SXXX 99 XXXX 100 .... 101 ..XD 102 */
View Code

ZOJ - 2110 Tempter of the Bone