杭电acm1010 Tempter of the Bone

Tempter of the Bone

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

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
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
 
Sample Output
NO
YES

Solution

典型的深度搜索问题,使用dfs算法求解

Code

package acm1010;
 
/**
 * date:2017.11.23
 * author:孟小德
 * function: 杭电acm1010
 *  Tempter of the Bone     深度搜索    bfs
 */
 
import java.util.*;
 
class Node
{
    int row,column;
 
    public Node(int row,int column)
    {
        this.row = row;
        this.column = column;
    }
 
    public Node()
    {
 
    }
}
 
public class Main
{
    public static int n,m,T;
    public static boolean visit[][];
    public static int dir[][] = {{1,0},{-1,0},{0,1},{0,-1}};
    public static boolean yes;
    public static Node start = new Node();
    public static Node end = new Node();
 
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
 
        while (input.hasNextLine())
        {
            n = input.nextInt();
            m = input.nextInt();
            T = input.nextInt();
            input.nextLine();
 
            if (n == 0 && m == 0 && T == 0)
            {
                break;
            }
 
            visit = new boolean[n][m];
            yes = false;
 
            int wall = 0;
            //  输入
            for (int i=0;in*m - wall - 1)
            {
                System.out.println("NO");
                continue;
            }
 
            visit[start.row][start.column] = true;
            dfs(start.row,start.column,0);
            if (yes)
            {
                System.out.println("YES");
            }
            else
            {
                System.out.println("NO");
            }
        }
    }
 
    public static void dfs(int row,int column,int time)
    {
        for (int i=0;iT)
            {
                continue;
            }
 
            if (a<0 || a>=n || b<0 || b>=m)
            {   //  越界
                continue;
            }
 
            if (visit[a][b] == true)
            {   // 被访问过
                continue;
            }
 
            if (a == end.row && b == end.column)
            {   // 当访问到终点时
                if (thetime == T)
                {
                    yes = true;
                    return;
                }
                else
                {
                    continue;
                }
            }
 
            //  每次递归都进行剪枝运算
            int temp = T - thetime - Math.abs(a - end.row) - Math.abs(b - end.column);
            if (temp % 2 == 1)
            {   // 当剪枝结果为奇数时,在该点无法在指定时间到达终点
                //  此时没必要在这点进行递归,当返回上层递归往另一个方向
                //  继续
                continue;
            }
            visit[a][b] = true;
            dfs(a,b,thetime);
            //  同一个点在不同路径中可能被再次访问,因此
            //  每一个路径中对应不同的visit
            visit[a][b] = false;
        }
    }
}

你可能感兴趣的:(杭电acm1010 Tempter of the Bone)