java實現過河卒

這篇blog算是自己記錄一下,沒什麼很新的東西。共勉~~

因爲我原來並沒有研究過算法,所以一看到過河卒這個題目直接想都沒想 ,深搜遍歷被。

public class guohezu {
    static int[][] grips;
    static int[] back = new int[40];
    static int cur_x = 1, cur_y = 1;
    static Integer number = 0;
    static Integer retricted_x = 0;
    static Integer retricted_y = 0;
    /**
     * Braylon
     * 20191124
     * */
    //@Test
    public static void main(String args[])/*guohezu1124() */{

        //獲取數據
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String array[] = input.split(" ");
        int m = Integer.parseInt(array[0]) + 3;
        int n = Integer.parseInt(array[1]) + 3;
        int h_x = Integer.parseInt(array[2]) + 1;
        int h_y = Integer.parseInt(array[3]) + 1;
        grips = new int[m][n];
        for (int i = 0; i < m ; i++) {
            for (int j = 0; j < n ; j++) {
                if (i == 0 || j == 0 || i == m -1 || j == n -1 ||
                        (i == h_x && j == h_y) ||
                        (i != h_x && j != h_y && ((Math.abs(i - h_x) + Math.abs(j - h_y)) == 3))) {
                    grips[i][j] = 1;
                } else {
                    grips[i][j] = 0;
                }
            }
        }
        for (int i = 0; i < 40; i++) {
            back[i] = -1;
        }

        //以上初始化結束

        //遍歷
        while (cur_x != 0) {
            if (grips[cur_x][cur_y + 1] == 0 && retricted_x != cur_x && retricted_y != cur_y) {
                if (grips[cur_x + 1][cur_y] == 0) {
                    push(cur_x, cur_y);
                    walk(cur_x, cur_y + 1);//先走右邊
                } else {
                    walk(cur_x, cur_y + 1);
                }
            } else {
                if (retricted_x == cur_x && retricted_y == cur_y) {
                    retricted_x = 0;
                    retricted_y = 0;
                }
                if (grips[cur_x + 1][cur_y] == 0) {
                    walk(cur_x + 1, cur_y);
                } else {
                    gotosavepoint();
                }
            }
            if (cur_y == n - 2 && cur_x == m - 2) {
                number++;
                gotosavepoint();
            }
        }
        System.out.println(number);
    }

    public static int walk(int i, int j) {
        //grips[i][j] = 1;
        cur_x = i;
        cur_y = j;
        return 0;
    }
    public static void gotosavepoint() {
        int get_x = 0;
        int get_y = 0;
        for (int i = 39; i >= 1; i=i-2) {
            if (back[i] != -1) {
                get_y = back[i];
                get_x = back[i - 1];
                back[i] = -1;
                back[i - 1] = -1;
                break;
            }
        }
        retricted_y = get_y;
        retricted_x = get_x;
        cur_y = get_y;
        cur_x = get_x;
    }

    public static int push(int x, int y) {
        for (int i = 0; i <= 38; i = i + 2) {
            if (back[i] == -1) {
                back[i] = x;
                back[i + 1] = y;
                return 0;
            }
        }
        return 1;
    }
}

這樣寫肯定就基本上超時了,後來仔細研究,再加上參考一些資料和方法,確實dp纔是王道。

思路很簡單
大家可以參考這個文章看具體算法思路
點擊跳轉
這裏我放一個java實現

/**
 * Braylon
 * 20191125
 * */
public class guohezu_DP {

    @Test
    public void guohezu_dp() {
//    public static void main(String args[]){
        //聲明全局變量
        long[][] dp_map;//表示走到當前空格
        int[][] restricted_matric;//1:禁止通過,0:正常
        long number = 0;//最終數量
        int[] h_arr_x = {1, 1, -1, -1, 2, 2, -2, -2};
        int[] h_arr_y = {2, -2, 2, -2, -1, 1, 1, -1};

        //獲取輸入
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt() + 1;//輸入6但是有7挑路徑
        int n = scanner.nextInt() + 1;
        int h_x = scanner.nextInt();
        int h_y = scanner.nextInt();
//        String input = scanner.nextLine();
//        String input="20 3 19 2";
//        String[] arr = input.split(" ");
//        int m = Integer.parseInt(arr[0]) + 1;//輸入6但是有7挑路徑
//        int n = Integer.parseInt(arr[1]) + 1;
//        int h_x = Integer.parseInt(arr[2]);
//        int h_y = Integer.parseInt(arr[3]);

        //初始化全局變量
        dp_map = new long[m][n];
        restricted_matric = new int[m][n];
        restricted_matric[h_x][h_y] = -1;
        for (int i = 0; i < 8; i++) {
            if (((h_x + h_arr_x[i]) < m) && ((h_x + h_arr_x[i]) >= 0) && ((h_y + h_arr_y[i]) >= 0) && ((h_y + h_arr_y[i]) < n)) {
                restricted_matric[h_x + h_arr_x[i]][h_y + h_arr_y[i]] = -1;
            }
        }
        for (int i = 0; i < n; i++) {
            if (restricted_matric[0][i] == -1) {
                dp_map[0][i] = 0;
                continue;
            }
            dp_map[0][i] = 1;
        }
        for (int i = 0; i < m; i++) {
            if (restricted_matric[i][0] == -1) {
                dp_map[i][0] = 0;
                continue;
            }
            dp_map[i][0] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (restricted_matric[i][j] == -1) {
                    dp_map[i][j] = 0;
                } else {
                    dp_map[i][j] = dp_map[i - 1][j] + dp_map[i][j - 1];
                }
            }
        }
        number = dp_map[m - 1][n - 1];
        System.out.println(number);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章