经典搜索(深搜+剪枝+Java细节注意)

来源:互联网 发布:数控车床有编程软件吗 编辑:程序博客网 时间:2024/05/30 20:08

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 98309 Accepted Submission(s): 26658

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

3 4 5
S.X.
..X.
…D
0 0 0

Sample Output
NO
YES

  1. 刚开始看这道题的时候,乍一看像是迷宫求最短路径的题,直接用广搜做了,然而持续wa。后来仔细读题发现,题目要求doggie在第Tth second恰好到达Door才能获救,并不是最短路径。所以一定要仔细读题,尤其是遇到英文时,明确题意,否则就只能呵呵!
  2. 然而当我撸了一个深搜以后,oj却是TLD,设计了一个测试样例,居然发现跑到了三十几万毫秒,然而oj却要求2000ms
  3. 当我参考了网上的代码,进行了如下剪枝:判断输入的地图中能走的步数是否有可能为T的剪枝(如果地图中的最大步数小于T,肯定要剪掉)、判断出发点和目的地的最短距离是否小于T的剪枝(如果最短距离都大于T了,当然要剪枝了)、奇偶性剪枝。
  4. 做完上述这些以后,我信心勃勃的去提交,仍然为wa(而且是多次)。万般无奈之下,我一步一步的对着参考代码调试,终于让我发现了两个bug:一是Java中的foreach循环只能遍历集合或数组中的元素,并不能进行改动。二是不满足图中能走步数可能为T的剪枝时,continue后,没有了输入!!所以,设计测试样例,真是个技术活!

题目要求不能走重复的点,所以开一个数组vis进行标记,注意搜索过程中要恢复现场!
Whatever,通过这道题,学到了不少东西。这实际是一道司空见惯的深搜题,最为关键的是剪枝的技巧学习!!

> import java.util.Arrays;import java.util.Scanner;public class Main {    static int N,M,T;    static int sx,sy,dx,dy;    static char[][] maze = new char[10][10];    static boolean[][] vis = new boolean[10][10];    static int[] dirx={1,0,-1,0},diry={0,1,0,-1};    static boolean flag;    static void Debug(){        System.out.println("Test");    }    static boolean Judge(){        if(N==0&&M==0&&T==0) return false;        return true;    }    static void dfs(int x,int y,int s){        if(flag||s>T) return ;        if(s==T){            if(x==dx&&y==dy) flag = true;            return ;        }        // even-odd prune        int l = Math.abs(dx-x)+Math.abs(dy-y);        if((l>T-s)||((T-s-l)%2!=0)) return ;        for(int k=0;k<4;k++){            int nx = x+dirx[k];            int ny = y+diry[k];            if(nx>=0&&nx<N&&ny>=0&&ny<M&&maze[nx][ny]!='X'&&!vis[nx][ny]){                vis[nx][ny] = true;                dfs(nx, ny,s+1);                vis[nx][ny] = false;            }        }    }    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in = new Scanner(System.in);        N = in.nextInt();        M = in.nextInt();        T = in.nextInt();        String line = new String();        while(Judge()){            for(int i=0;i<N;i++){                line = in.next();                maze[i]=line.toCharArray();            }            int num1 = 0;            for(int i=0;i<N;i++){                for(int j=0;j<M;j++){                    if(maze[i][j]=='S'){                        sx = i;                        sy = j;                    }                    if(maze[i][j]=='D'){                        dx = i;                        dy = j;                    }                    if(maze[i][j]=='X') num1++;                }            }            if(N*M-num1-1<T||(T-Math.abs(sx-dx)-Math.abs(sy-dy))%2!=0){                System.out.println("NO");                N = in.nextInt();                M = in.nextInt();                T = in.nextInt();                continue;            }            flag = false;            for(int i=0;i<10;i++){                for(int j=0;j<10;j++) vis[i][j] = false;            }            vis[sx][sy] = true;            dfs(sx, sy,0);            if(flag) System.out.println("YES");            else System.out.println("NO");            N = in.nextInt();            M = in.nextInt();            T = in.nextInt();        }    }}

奇偶剪枝
奇偶剪枝:
起点S(sx,sy),终点D(ex,ey),给定t步恰好走到终点。
定理:S、D这两点之间的最短距离是step1=abs(ex-sx)+abs(ey-sy)!(走法:(sx,sy)->(sx,ey)->(ex,ey) 等等
对于一般非最短路径下的走法其距离记为step2,则step2-step1一定为偶数
推广:若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;

1 0