注意马的拦截点逻辑判断,注意数组边界是end + 1。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] horse = new int[2], end = new int[2];
end[0] = in.nextInt();
end[1] = in.nextInt();
horse[0] = in.nextInt();
horse[1] = in.nextInt();
long[][] dp = new long[end[0] + 1][end[1] + 1];
dp[0][0] = 1;
for (int i = 1; i <= end[1]; i++) {
if (ifReach(0, i, horse)) {
dp[0][i] = dp[0][i - 1];
}
}
for (int r = 1; r <= end[0]; r++) {
for (int c = 0; c <= end[1]; c++) {
if (c == 0) {
if (ifReach(r, c, horse)) {
dp[r][c] = dp[r - 1][c];
}
continue;
}
if (ifReach(r, c, horse)) {
dp[r][c] = dp[r - 1][c] + dp[r][c - 1];
}
}
}
System.out.println(dp[end[0]][end[1]]);
}
public static boolean ifReach(int x, int y, int[] horse) {
if (x == horse[0]) {
return y != horse[1];
}
if (y == horse[1]) {
return x != horse[0];
}
return Math.abs(x - horse[0]) + Math.abs(y - horse[1]) != 3;
}
}

京公网安备 11010502036488号