Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 55812 Accepted Submission(s): 15052
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
搜索题:dfs深度优先搜索、、、、
代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 int n,m,ex,ey,t; 5 bool success; 6 char maze[10][10]; //考虑到要加边 7 /* 8 stx ---->开始x坐标 9 sty ---->开始y坐标 10 dt ---->花掉时间 11 */ 12 13 void dfs(int stx,int sty,int dt ) 14 { 15 /* 16 超过规定时间,超边都表示无法完成任务 17 */ 18 if(stx<=0||stx>n||sty<=0||sty>m) /* 可以加边处理的*/ 19 return ; 20 if(stx==ex&&sty==ey&&dt==t) 21 success=true; 22 if(success) return ; 23 int temp=(t-dt)-abs(ex-stx)-abs(ey-sty); 24 if(temp<0||temp&1) //奇偶剪枝 25 return ; 26 /* 27 0 1 0 1 0 1 0 1 0 28 1 0 1 0 1 0 1 0 1 29 0 1 0 1 0 1 0 1 0 30 1 0 1 0 1 0 1 0 1 31 0 1 0 1 0 1 0 1 0 32 1 0 1 0 1 0 1 0 1 33 0 1 0 1 0 1 0 1 0 34 1 0 1 0 1 0 1 0 1 35 无论是从o 开始还是从1开始, 36 都是 0--->1 或者1--->0 都是奇数步 37 0-->0 , 1--->1 都是偶数步 38 */ 39 //然后是对上下左右的搜索 40 if(maze[stx][sty+1]!='X') //向右搜索 41 { 42 maze[stx][sty+1]='X'; //见进入口堵上 43 dfs(stx,sty+1,dt+1); 44 maze[stx][sty+1]='.'; 45 } 46 47 if(maze[stx+1][sty]!='X') //向下搜索 48 { 49 maze[stx+1][sty]='X'; //见进入口堵上 50 dfs(stx+1,sty,dt+1); 51 maze[stx+1][sty]='.'; 52 } 53 if(maze[stx][sty-1]!='X') //向左搜索 54 { 55 maze[stx][sty-1]='X'; //见进入口堵上 56 dfs(stx,sty-1,dt+1); 57 maze[stx][sty-1]='.'; 58 } 59 if(maze[stx-1][sty]!='X') //向上搜索 60 { 61 maze[stx-1][sty]='X'; //见进入口堵上 62 dfs(stx-1,sty,dt+1); 63 maze[stx-1][sty]='.'; 64 } 65 return ; 66 } 67 68 int main() 69 { 70 int stx,sty,wall; 71 while(scanf("%d%d%d",&n,&m,&t),n+m+t) 72 { 73 getchar(); 74 wall=0; //统计障碍物的个数 每次输入清零 75 for(int i=1;i<=n;i++) 76 { 77 for(int j=1;j<=m;j++) 78 { 79 scanf("%c",&maze[i][j]); 80 if(maze[i][j]=='S') 81 { 82 stx=i; // 标注开始的x轴的位置 83 sty=j; // 标注开始的y轴的位置 84 } 85 else 86 if(maze[i][j]=='D') 87 { 88 ex=i; // 标注结束的x轴的位置 89 ey=j; // 标注结束的y轴的位置 90 } 91 else if(maze[i][j]=='X') 92 { 93 wall++; 94 } 95 } 96 getchar(); 97 } 98 success=false; 99 maze[stx][sty]='X'; //堵住入口 100 if( n*m-wall<=t ) //因为只有在t时刻door 才打开 101 printf("NO "); 102 else 103 { 104 dfs(stx,sty,0); 105 if(success) 106 printf("YES "); 107 else 108 printf("NO "); 109 } 110 } 111 return 0; 112 }