You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class19;
public class Code03_HorseJump {
public static int ways(int a, int b, int step) {
return f(0, 0, step, a, b);
}
public static int f(int i, int j, int step, int a, int b) {
if (i < 0 || i > 9 || j < 0 || j > 8) {
return 0;
}
if (step == 0) {
return (i == a && j == b) ? 1 : 0;
}
return f(i - 2, j + 1, step - 1, a, b)
+ f(i - 1, j + 2, step - 1, a, b)
+ f(i + 1, j + 2, step - 1, a, b)
+ f(i + 2, j + 1, step - 1, a, b)
+ f(i + 2, j - 1, step - 1, a, b)
+ f(i + 1, j - 2, step - 1, a, b)
+ f(i - 1, j - 2, step - 1, a, b)
+ f(i - 2, j - 1, step - 1, a, b);
}
public static int waysdp(int a, int b, int s) {
int[][][] dp = new int[10][9][s+1];
dp[a][b][0] = 1;
for(int step = 1 ; step <= s;step++ ) { // 按层来
for(int i = 0 ; i < 10;i++) {
for(int j = 0 ; j < 9; j++) {
dp[i][j][step] = getValue(dp,i - 2, j + 1, step - 1)
+ getValue(dp,i - 1, j + 2, step - 1)
+ getValue(dp,i + 1, j + 2, step - 1)
+ getValue(dp,i + 2, j + 1, step - 1)
+ getValue(dp,i + 2, j - 1, step - 1)
+ getValue(dp,i + 1, j - 2, step - 1)
+ getValue(dp,i - 1, j - 2, step - 1)
+ getValue(dp,i - 2, j - 1, step - 1);
}
}
}
return dp[0][0][s];
}
// 在dp表中得到dp[i][j][step]的值,但如果(ij)位置越界的话返回0
public static int getValue(int[][][] dp, int i, int j, int step) {
if (i < 0 || i > 9 || j < 0 || j > 8) {
return 0;
}
return dp[i][j][step];
}
public static void main(String[] args) {
int x = 7;
int y = 7;
int step = 10;
System.out.println(ways(x, y, step));
System.out.println(waysdp(x, y, step));
}
}