Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 98875 Accepted Submission(s): 26807
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
题目大意就是从S出发能否在t时刻内恰好到达D点...这个题做得很失败,我开始是用bfs去搜索,后来一直WA,看题解发现用dfs,恍然大悟。
发现这题不能用bfs,因为bfs总是搜的最短路,标记后没办法回溯。而恰好在t时刻到达的这个条件必须要用dfs去搜索才能出来(其实感觉bfs搜的话可能
可以加个时间标记什么的,只要能重走的话能想到还是可以的)而且这题还有一个重要的奇偶剪枝。
//分析:题意是要恰好在t时间内到达,所以只能用dfs进行回溯搜索(bfs搜索的是最短路)。 //若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达; package 搜索; import java.util.Scanner; public class hdu_1010 { static char[][] map; static boolean flag; static boolean[][] vis; static int[][] dir = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; static int n, m, t; static int sx, sy, ex, ey; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { n = sc.nextInt(); m = sc.nextInt(); t = sc.nextInt(); if (n == 0 && m == 0 && t == 0) break; map = new char[n][m]; vis = new boolean[n][m]; for (int i = 0; i < n; i++) { String str = sc.next(); map[i] = str.toCharArray(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (map[i][j] == 'S') { sx = i; sy = j; } if (map[i][j] == 'D') { ex = i; ey = j; } } } if (t < Math.abs(ex - sx) + Math.abs(ey - sy)) { System.out.println("NO"); continue; } if ((t - Math.abs(ex - sx) + Math.abs(ey - sy)) % 2 != 0) { System.out.println("NO"); continue; } flag = false; vis[sx][sy]=true; //第一个点没标记WA了半天 dfs(sx, sy, 0); if (!flag) System.out.println("NO"); else System.out.println("YES"); } } private static void dfs(int x, int y, int time) { if (x == ex && y == ey && time == t) { //System.out.println(x+" "+y + " "+time); flag = true; return; } if (time >= t) //时间过了 return; for (int i = 0; i < 4; i++) { int next_x = x + dir[i][0]; int next_y = y + dir[i][1]; if (next_x < 0 || next_x >= n || next_y < 0 || next_y >= m || vis[next_x][next_y] == true || map[next_x][next_y] == 'X') { continue; } vis[next_x][next_y] = true; dfs(next_x, next_y, time + 1); vis[next_x][next_y] = false; // 回溯 if (flag) return; // 找到了直接返回 } } }