#4WA求助
查看原帖
#4WA求助
295827
红菜头糕楼主2021/3/5 14:45

用的Java,看讨论区是因为数据太大了换成了long,但是仍然不能过第4个点

import java.util.Scanner;

public class Main {

    private static int desX, desY;

    public static void main(String[] args) {
        int horseX, horseY;
        Scanner sc = new Scanner(System.in);
        desX = sc.nextInt();
        desY = sc.nextInt();
        horseX = sc.nextInt();
        horseY = sc.nextInt();
        long[][] arr = new long[desX + 1][desY + 1];
        int[][] dirt = {{-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}};
        if (horseX >= 0 && horseX <= desX && horseY >= 0 && horseY <= desY)   // 马本体落点
            arr[horseX][horseY] = -1;
        for (int i = 0; i < 8; i++) {  // 马的八个落点
            int tempX = horseX + dirt[i][0];
            int tempY = horseY + dirt[i][1];
            if (tempX > 0 && tempX <= desX && tempY >= 0 && tempY <= desY)
                arr[tempX][tempY] = -1;
        }
        countNum(arr);
    }

    private static void countNum(long[][] arr) {
        if (arr[desX][desY] == -1) {  // 终点处于马的落点
            System.out.println(0);
            return;
        }
        for (int i = 1; i < desX + 1; i++) {   // 位于棋盘左边界和上边界的点均只有一种走法
            if (arr[i][0] == -1)
                break;
            arr[i][0] = 1;
        }
        for (int i = 1; i < desY + 1; i++) {
            if (arr[0][i] == -1)
                break;
            arr[0][i] = 1;
        }
        for (int i = 1; i < desX + 1; i++) {
            for (int j = 1; j < desY + 1; j++) {
                if (arr[i][j] == -1 || (arr[i - 1][j] == -1 && arr[i][j - 1] == -1))   // 若该点为马落点或者该点的左和上方为马落点,则不可达
                    arr[i][j] = -1;
                else if (arr[i - 1][j] != -1 && arr[i][j - 1] != -1)   // 左和上方都可达
                    arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
                else  // 左和上方有一者不可达
                    arr[i][j] = Long.max(arr[i - 1][j], arr[i][j - 1]);
            }
        }
        System.out.println(arr[desX][desY]);
    }
}

2021/3/5 14:45
加载中...