zoj-2110-Tempter of the Bone-DFS-剪枝-java

来源:互联网 发布:天猫抢购软件 编辑:程序博客网 时间:2024/05/16 00:37
Tempter of the Bone

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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


          解题思路: 题意是一只小狗从s到d 到达d点时 时间刚好过了t  纯dfs搜索题  我第一遍超时 之后才意识到要剪枝

                              我发现我并没有养成良好的意识 哪怕这题简单 如果可以优化 那么一定要优化  这样以后再思考每一

                              件事情都会按照最大的效率来 

                              两处剪枝

                                          1.搜索前剪枝 因为到达d点时 时间要正好为 t 所以必须走t个.才行 如果图里的点小于t那么肯定是no了

                                           2.搜索时剪枝 到达某点用了还剩t2个单位时间 再计算一下到d点的最小距离 就是abs(x-dx)+abs(y-dy)

                                                                  如果小于0 那么意味着时间不够了 这点就不用再往下搜了 或者

                                                                  如果为奇数 那么说明 他得多走奇数步到达d点 你想啊 如果想多走几步就得拐弯 最终你是要回到

                                                                   d点的还得拐回来 也就是不管你怎么走 你多走出的那几步 一定是偶数 所以奇数就不用再往下搜了



import java.util.Scanner;


public class Main {


private static int n;
private static int m;
private static int t;
private static int Ydoor;
private static int Xdoor;
private static int Ydog;
private static int Xdog;
private static boolean[][] flag;
private static boolean v;
private static int wall;


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
n = scanner.nextInt();
m = scanner.nextInt();
t = scanner.nextInt();
if(n==0&&m==0&&t==0) {return;}
Xdoor = 0;
Ydoor = 0;
Xdog = 0;
Ydog = 0;
wall = 0;
flag = new boolean[n][m];
char[][] arr = new char[n][m];
for (int i = 0; i < n; i++) {
String a = scanner.next();
for (int j = 0; j < m; j++) {
char b = a.charAt(j);
arr[i][j] = b;
if (b == 'S') {
Xdog = i;
Ydog = j;
}
if (b == 'D') {
Xdoor = i;
Ydoor = j;
}
if (b == 'X') {
flag[i][j] = true;
}
}
}
if (n*m-wall<t) {
System.out.println("NO");
continue;
}
v = true;
dfs(Xdog, Ydog, t);
if (!v) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}


private static void dfs(int x, int y, int t2) {
if (x >= 0 && x < n && y >= 0 && y < m && v && !flag[x][y]) {
if (x == Xdoor && y == Ydoor && t2 == 0) {
v = false;
} else {
int temp = t2 - Math.abs(x-Xdoor)-Math.abs(y-Ydoor);
if(temp<0||temp%2!=0) {return;}
flag[x][y] = true;
dfs(x + 1, y, t2 - 1);
dfs(x - 1, y, t2 - 1);
dfs(x, y + 1, t2 - 1);
dfs(x, y - 1, t2 - 1);
flag[x][y] = false;
}
}
}


}

原创粉丝点击