Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 42497 Accepted Submission(s): 11504
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
package 有点意思;
import java.util.Scanner;
public class _1010_TempterOfTheBone {
// 狗走迷宫
// 暴力递归,没有好办法,注意剪枝!
// 需要注意几个小问题
// int steps在递归中是不会改变上一步递归的值的,所以不需要改变
// char[][]在递归时是改变值的,所以每次递归完毕,走不通的话,需要将char[][]改回原值
static boolean canSurvive = false;
static int n = 0;
static int m = 0;
static int t = 0;
// 这段代码证明,在i递归时,不会影响上一步的i值
public static void testNum(int i) {
i++;
if (i < 50) {
testNum(i);
}
System.out.println(i);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// testNum(0);
while (sc.hasNext()) {
n = sc.nextInt();
m = sc.nextInt();
t = sc.nextInt();
canSurvive = false;
if (n == 0 && m == 0 && t == 0)
break;
char[][] road = new char[n][m];
int begin_x = 0;
int begin_y = 0;
for (int i = 0; i < n; i++) {
road[i] = sc.next().toCharArray();
}
label: for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (road[i][j] == 'S') {
begin_x = j;
begin_y = i;
road[i][j] = '.';
break label;
}
}
}
canSurvive(road, begin_x, begin_y, 0);
if (canSurvive) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
public static void canSurvive(char[][] road, int nowX, int nowY, int steps) {
if (!canSurvive) {
if (nowX >= 0 && nowX < m && nowY >= 0 && nowY < n) {
switch (road[nowY][nowX]) {
case 'D':
if (steps == t) {
canSurvive = true;
}
break;
case '.':
steps++;
road[nowY][nowX] = 'X';
canSurvive(road, nowX + 1, nowY, steps);
canSurvive(road, nowX - 1, nowY, steps);
canSurvive(road, nowX, nowY + 1, steps);
canSurvive(road, nowX, nowY - 1, steps);
road[nowY][nowX] = '.';
break;
}
}
}
}
}