动态规划做的,1 3 4 RE ,求助!
查看原帖
动态规划做的,1 3 4 RE ,求助!
931839
chenweijie楼主2023/3/10 13:21
import java.util.Scanner;

public class test {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();//B坐标( n, m )
        int m = in.nextInt();
        int horse_x = in.nextInt();//马坐标 ( horse_x,horse_y )
        int horse_y = in.nextInt();
        int[][] horse = {{-2, -1}, {-2, 1}, {2, -1}, {2, 1},
                {-1, -2}, {-1, 2}, {1, -2}, {1, 2}};//马行走方向
        int[][] dp = new int[n + 1][m + 1];
        dp[horse_x][horse_y] = -1;
        for (int i = 0; i < 8; i++) {
            dp[horse_x + horse[i][0]][horse_y + horse[i][1]] = -1;
        }
        dp[0][0] = 1;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (dp[i][j] == -1) {
                    dp[i][j] = 0;
                } else if (i == 0 || j == 0) {
                    dp[i][j] = 1;
                } else {
                    dp[i][j] = Math.max(dp[i][j], dp[i - 1][j] + dp[i][j - 1]);
                }
            }
        }
        System.out.println(dp[n][m]);
    }
}
```java
2023/3/10 13:21
加载中...