当前位置:网站首页>Violence recursion to dynamic programming 08 (pony go chess)
Violence recursion to dynamic programming 08 (pony go chess)
2022-08-03 02:33:00 【Taotao can't learn English】
递归思路
Consider all travel points for the horse,Out of bounds are not counted.
public int ways(int a, int b, int step) {
System.out.println(jump(a, b, 0, 0, step));
return process(a, b, 0, 0, step);
}
/** * @param a 目标位置x * @param b 目标位置y * @param i 当前位置i * @param j 当前位置j * @param step 剩余步数 step * @return 横9线,纵10线 * 9行10列 * [0-8] [0-9] */
private int jump(int a, int b, int i, int j, int step) {
if (step == 0) {
//Currently is a method
return a == i && b == j ? 1 : 0;
}
// i j jump situation:i+1,j+2 i+2,j+1 i-1,j+2 i-2,j+1 i+1,j-2 i+2,j-1
int upRightDown = i + 1 <= 9 && j + 2 <= 8 ? jump(a, b, i + 1, j + 2, step - 1) : 0;
int upRightUp = i + 2 <= 9 && j + 1 <= 8 ? jump(a, b, i + 2, j + 1, step - 1) : 0;
// i-2,j+1 i-2,j-1
int upLeftDown = i - 2 >= 0 && j + 1 <= 8 ? jump(a, b, i - 2, j + 1, step - 1) : 0;
int upLeftUp = i - 1 >= 0 && j + 2 <= 8 ? jump(a, b, i - 1, j + 2, step - 1) : 0;
// i-1,j-2 i-2,j-1
int downLeftDown = i - 1 >= 0 && j - 2 >= 0 ? jump(a, b, i - 1, j - 2, step - 1) : 0;
int downLeftUp = i - 2 >= 0 && j - 1 >= 0 ? jump(a, b, i - 2, j - 1, step - 1) : 0;
// i+1,j-2 i+2,j-1
int downRightDown = i + 1 <= 9 && j - 2 >= 0 ? jump(a, b, i + 1, j - 2, step - 1) : 0;
int downRightUp = i + 2 <= 9 && j - 1 >= 0 ? jump(a, b, i + 2, j - 1, step - 1) : 0;
return upRightDown + upRightUp + upLeftDown + upLeftUp + downLeftDown + downLeftUp + downRightDown + downRightUp;
}
简化
private int process(int a, int b, int i, int j, int step) {
if (i > 9 || j > 8 || i < 0 || j < 0) {
return 0;
}
if (step == 0) {
//Currently is a method
return a == i && b == j ? 1 : 0;
}
// i j jump situation:i+1,j+2 i+2,j+1 i-1,j+2 i-2,j+1 i+1,j-2 i+2,j-1
int ways = process(a, b, i + 1, j + 2, step - 1);
ways += process(a, b, i + 2, j + 1, step - 1);
// i-2,j+1 i-2,j-1
ways += process(a, b, i - 2, j + 1, step - 1);
ways += process(a, b, i - 1, j + 2, step - 1);
// i-1,j-2 i-2,j-1
ways += process(a, b, i - 1, j - 2, step - 1);
ways += process(a, b, i - 2, j - 1, step - 1);
// i+1,j-2 i+2,j-1
ways += process(a, b, i + 1, j - 2, step - 1);
ways += process(a, b, i + 2, j - 1, step - 1);
return ways;
}
动态规划
public int dp(int a, int b, int k) {
int[][][] dp = new int[10][9][k + 1];
//The first layer is filled
dp[a][b][0] = 1;
for (int step = 1; step <=k; step++) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 9; j++) {
// i j jump situation:i+1,j+2 i+2,j+1 i-1,j+2 i-2,j+1 i+1,j-2 i+2,j-1
int upRightDown = i + 1 <= 9 && j + 2 <= 8 ? dp[i + 1][j + 2][step - 1] : 0;
int upRightUp = i + 2 <= 9 && j + 1 <= 8 ? dp[i + 2][j + 1][step - 1] : 0;
// i-2,j+1 i-2,j-1
int upLeftDown = i - 2 >= 0 && j + 1 <= 8 ? dp[i - 2][j + 1][step - 1] : 0;
int upLeftUp = i - 1 >= 0 && j + 2 <= 8 ? dp[i - 1][j + 2][step - 1] : 0;
// i-1,j-2 i-2,j-1
int downLeftDown = i - 1 >= 0 && j - 2 >= 0 ? dp[i - 1][j - 2][step - 1] : 0;
int downLeftUp = i - 2 >= 0 && j - 1 >= 0 ? dp[i - 2][j - 1][step - 1] : 0;
// i+1,j-2 i+2,j-1
int downRightDown = i + 1 <= 9 && j - 2 >= 0 ? dp[i + 1][j - 2][step - 1] : 0;
int downRightUp = i + 2 <= 9 && j - 1 >= 0 ? dp[i + 2][j - 1][step - 1] : 0;
dp[i][j][step] = upRightDown + upRightUp + upLeftDown + upLeftUp + downLeftDown + downLeftUp + downRightDown + downRightUp;
}
}
}
return dp[0][0][k];
}
边栏推荐
猜你喜欢
随机推荐
Vite教程 安装
npm运行项目dependencies were not found: core-js/modules/es6.array.fill
.NET深入解析LINQ框架(四:IQueryable、IQueryProvider接口详解)
nmap: Bad CPU type in executable
电信业务分类
C语言:链表
【遥控器开发基础教程5】疯壳·开源编队无人机-SPI(2.4G 双机通信)
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
可编程逻辑控制器(PLC) : 基础、类型和应用
聊聊 Nacos
NVM和NRM
全栈---Proxy
2022 开放原子全球开源峰会 | 麒麟信安携手openEuler助力开源产业繁荣发展
高并发基石:多线程、守护线程、线程安全、线程同步、互斥锁,一文扫尽!...
担心的事情
优秀论文以及思路分析01
升级版的冒泡排序:鸡尾酒排序(快乐小时排序)
线性DP
Understand the next hop address in the network topology in seconds
Jenkins汉化设置