HDOJ 1010 Tempter of the Bone (JAVA)

参考原文:http://www.cnblogs.com/zhourongqing/archive/2012/04/28/2475684.html

原题和思考过程请参考以上连接,包括关键的DFS和一些剪枝技巧。

一下是自己写的Java代码,在以上的参考连接中优化了一点(原文用到了两个二维数组,这里简化为了一个)。

废话不多说,上代码!(如有不足,欢迎指正,共同学习!)

import java.util.Scanner;

public class Main {


    static int N, M, T, endI, endJ;
    static boolean[][] visited;
    static boolean ans;

    static void memset(boolean[][] arr, boolean bo) {
        int height = arr.length;
        int width = arr[0].length;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                arr[i][j] = bo;
            }
        }
    }

    static void DFS(int i, int j, int count) {
        if (ans)
            return;
        if (count > T)
            return;
        if (i < 0 || j < 0 || i >= N || j >= M)
            return;
        if (i == endI && j == endJ && count == T) {
            ans = true;
            return;
        }

        // 奇偶剪枝
        int temp = Math.abs(endI - i) + Math.abs(endJ - j);
        temp = T - count - temp;
        if ((temp & 1) == 1)
            return;

        if ((i - 1 >= 0) && !visited[i - 1][j]) {
            visited[i - 1][j] = true;
            DFS(i - 1, j, count + 1);
            visited[i - 1][j] = false;
        }
        if ((i + 1 < N) && !visited[i + 1][j]) {
            visited[i + 1][j] = true;
            DFS(i + 1, j, count + 1);
            visited[i + 1][j] = false;
        }
        if ((j - 1 >= 0) && !visited[i][j - 1]) {
            visited[i][j - 1] = true;
            DFS(i, j - 1, count + 1);
            visited[i][j - 1] = false;
        }
        if ((j + 1 < M) && !visited[i][j + 1]) {
            visited[i][j + 1] = true;
            DFS(i, j + 1, count + 1);
            visited[i][j + 1] = false;
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        int startX = 0, startY = 0;
        while (cin.hasNext()) {
            N = cin.nextInt();
            M = cin.nextInt();
            T = cin.nextInt();
            if (N == 0 || M == 0 || T == 0)
                return;
            ans = false;
            visited = new boolean[N][M];
            memset(visited, false);

            int k = 0;

            for (int i = 0; i < N; i++) {
                String str;
                str = cin.next();
                for (int j = 0; j < M; j++) {
                    char ch = str.charAt(j);
                    if (ch == 'X') {
                        visited[i][j] = true;
                        k++;
                    } else if (ch == 'D') {
                        endI = i;
                        endJ = j;
                    } else if (ch == 'S') {
                        startX = i;
                        startY = j;
                        visited[i][j] = true;
                    }
                }
            }

            visited[startX][startY] = true;

            if (N * M - k - 1 >= T)
                DFS(startX, startY, 0);
            if (ans) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }

        }
    }
}

 

你可能感兴趣的:(java)