题目大意:老鼠的起点在S,出后在D,X是墙壁不能通过,.可以通过。出口在第T时刻开启,老鼠必须在这这个时刻到达出口,中途不能停留。
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
这个题难的到不是DFS,而是剪枝的应用。刚开始总是超时,后来还是再网上看了些代码,加了一些剪枝条件才没超时。
#include <iostream> #include <cstdio> #include <cmath> using namespace std; char map[7][7]; int m, n, t; int startx, starty, endx, endy; //定义开始点,和结束点 int dir[4][2] = { { 0, -1 }, { 1, 0 }, { -1, 0 }, { 0, 1 } }; int escape; //是否逃脱标识 //求绝对值。 int myabs(int a) { return a < 0 ? -a : a; } void dfs(int x, int y, int time) { //重要的剪枝,减少搜索量。 否则超时 //相隔的距离不能太大,否则不可达。相隔的距离和剩余时间,应该同为奇数或偶数。 int temp = myabs(x - endx) + myabs(y - endy) - (t - time); if (temp > 0 || temp % 2) return; int i; for (i = 0; i < 4; i++) { int tempx = x + dir[i][0]; int tempy = y + dir[i][1]; if (map[tempx][tempy] == 'D' && time == t - 1) { escape = 1; return; } if (map[tempx][tempy] == '.' && tempx >= 0 && tempx < n && tempy >= 0 && tempx < n) { map[x][y] = 'X'; dfs(tempx, tempy, time + 1); map[x][y] = '.'; if (escape) //如果已经逃脱,就直接返回。 return; } } } int main() { while (1) { scanf("%d %d %d", &m, &n, &t); if (0 == m) break; getchar(); int wall = 0; int i,j; for ( i = 0; i < m; i++) { for ( j = 0; j < n; j++) { scanf("%c", &map[i][j]); if ('S' == map[i][j]) { startx = i; starty = j; } else if ('D' == map[i][j]) { endx = i; endy = j; }else if('X' == map[i][j]){ wall++; } } getchar(); } //第一次剪枝 if(n*m - wall <= t){ printf("NO\n"); continue; } escape = 0; dfs(startx, starty, 0); if (escape) printf("YES\n"); else printf("NO\n"); // break; } return 0; }