输入一行数字,包含3个参数x,y,k
输出一个数,代表有几种
7 7 10
515813
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
int k = scanner.nextInt();
System.out.print(waysdp(k, x, y) + "");
}
// 动态规划
public static int waysdp(int k, int x, int y){
int[][][] dp = new int[9][10][k + 1];
// 达成条件
dp[x][y][0] = 1;
for(int rest = 1; rest < k + 1; rest++){
for(int i = 0; i < 9; i++){
for(int j = 0; j < 10; j++){
dp[i][j][rest] = (getValue(dp, i - 1, j - 2, rest - 1) +
getValue(dp, i - 2, j - 1, rest - 1) +
getValue(dp, i - 1, j + 2, rest - 1) +
getValue(dp, i - 2, j + 1, rest - 1) +
getValue(dp, i + 1, j - 2, rest - 1) +
getValue(dp, i + 2, j - 1, rest - 1) +
getValue(dp, i + 1, j + 2, rest - 1) +
getValue(dp, i + 2, j + 1, rest - 1));
}
}
}
return dp[0][0][k];
}
public static int getValue(int[][][] dp, int a, int b, int rest){
if(a > 8 || a < 0 || b > 9 || b < 0) {
return 0;
}
return dp[a][b][rest];
}
public static int recursion(int a , int b, int rest, int x, int y){
if(a > 8 || a < 0 || b > 9 || b < 0) {
return 0;
}
if(rest == 0){
return a == x && b == y ? 1 : 0;
}
return recursion(a - 1, b - 2, rest -1, x, y) +
recursion(a - 2, b - 1, rest -1, x, y) +
recursion(a - 1, b + 2, rest -1, x, y) +
recursion(a - 2, b + 1, rest -1, x, y) +
recursion(a + 1, b - 2, rest -1, x, y) +
recursion(a + 2, b - 1, rest -1, x, y) +
recursion(a + 1, b + 2, rest -1, x, y) +
recursion(a + 2, b - 1, rest -1, x, y);
}
}