首页 > 代码库 > Tempter of the Bone
Tempter of the Bone
Tempter of the Bone
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 31 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
Author
ZHANG, Zheng
Source
ZJCPC2004View Code
1 #include <stdio.h> 2 #include <stdlib.h> 3 int Len_X,Len_Y,D,SIGN; 4 int End_x,End_y; 5 char Map[100][100]; 6 int DFS(int x,int y,int d,int D) 7 { 8 if(SIGN==1)return 1; 9 if(x==End_x&&y==End_y&&d==D){SIGN+=1;return 1;} 10 if(Map[x][y]==‘X‘)return 0; 11 else Map[x][y]=‘X‘; 12 if(d>=D) {return 0;} 13 if(x+1<Len_X&&Map[x+1][y]!=‘X‘) 14 { 15 DFS(x+1,y,d+1,D); 16 Map[x+1][y]=‘.‘; 17 } 18 if(x-1>=0&&Map[x-1][y]!=‘X‘) 19 { 20 DFS(x-1,y,d+1,D); 21 Map[x-1][y]=‘.‘; 22 } 23 if(y+1<Len_Y&&Map[x][y+1]!=‘X‘) 24 { 25 DFS(x,y+1,d+1,D); 26 Map[x][y+1]=‘.‘; 27 } 28 if(y-1>=0&&Map[x][y-1]!=‘X‘) 29 { 30 DFS(x,y-1,d+1,D); 31 Map[x][y-1]=‘.‘; 32 } 33 return 0; 34 } 35 36 int main() 37 { 38 int Begin_x,Begin_y,i,j,flat,A,B; 39 while(scanf("%d%d%d",&Len_X,&Len_Y,&D)!=EOF) 40 { 41 if(Len_X==0&&Len_Y==0&&D==0)return 0; 42 getchar(); 43 for(i=0;i<Len_X;i++) 44 { 45 for(j=0;j<Len_Y;j++) 46 { 47 scanf("%c",&Map[i][j]); 48 if(Map[i][j]==‘S‘) 49 {Begin_x=i;Begin_y=j;} 50 if(Map[i][j]==‘D‘) 51 {End_x=i;End_y=j;} 52 } 53 getchar(); 54 } 55 SIGN=0; 56 A=(End_y-Begin_y)>0?(End_y-Begin_y):-(End_y-Begin_y); 57 B=(End_x-Begin_x)>0?(End_x-Begin_x):-(End_x-Begin_x); 58 if(D%2==(A+B)%2) 59 DFS(Begin_x,Begin_y,0,D); 60 if(SIGN!=0) 61 printf("YES\n"); 62 else 63 printf("NO\n"); 64 } 65 return 0; 66 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。