Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90751 Accepted Submission(s): 24692
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.
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
Author
ZHANG, Zheng
Source
Recommend
JGShining | We have carefully selected several similar problems for you: 1072 1026 1240 1175 1015
奇偶剪枝: (a, a)--> (b, b) 的最短逃生路径为 abs(b-a) + abs(b-a); 如果最短逃生路径途中有障碍, 绕过障碍所走步数一定为偶数。
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0}; 7 char map[10][10]; int vis[10][10]; 8 int n, m, fx, fy, t; 9 bool Judge; 10 void Dfs(int x, int y, int k, int t) 11 { 12 int xx, yy; 13 vis[x][y] = 1; 14 int dis = (t-k)-(abs(x-fx)+abs(y-fy)); //奇偶剪枝。 15 if(dis < 0 || dis%2) 16 return; 17 if(k >= t) 18 { 19 if(x == fx && y == fy) 20 Judge = true; 21 } 22 else 23 { 24 for(int i = 0; i < 4 && Judge == false/*!Judge*/; i++) //小剪枝, 有逃生路径了就不再执行 25 { 26 xx = x + ac[i][0]; 27 yy = y + ac[i][1]; 28 if(map[xx][yy] != ‘X‘ && xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy]) 29 { 30 Dfs(xx, yy, k + 1, t); 31 vis[xx][yy] = 0; //回溯; 32 } 33 } 34 } 35 } 36 int main() 37 { 38 while(~scanf("%d %d %d", &n, &m, &t), n+m+t) 39 { 40 Judge = false; 41 // memset(map, 0, sizeof(map)); 42 memset(vis, 0, sizeof(vis)); 43 int x, y; 44 for(int i = 0; i < n; i++) 45 { 46 cin >> map[i]; 47 for(int j = 0; j < m; j++) 48 { 49 if(map[i][j] == ‘S‘) 50 { 51 x = i; 52 y = j; 53 } 54 if(map[i][j] == ‘D‘) 55 { 56 fx = i; 57 fy = j; 58 } 59 } 60 } 61 Dfs(x, y, 0, t); 62 if(Judge) 63 puts("YES"); 64 else 65 puts("NO"); 66 } 67 return 0; 68 }