当前位置:网站首页>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];
}
边栏推荐
猜你喜欢
随机推荐
random.nextint()详解
236. 二叉树的最近公共祖先
【系统架构设计师】第三章 数据库系统
【SQL】—数据库操作、表操作
minio 单机版安装
买了一瓶饮料
js显示隐藏手机号
alibaba数据同步组件canal的实践整理
Rasa 3.x study series - Rasa - Issues 4792 socket debug logs clog up debug feed study notes
微信小程序--》条件与列表渲染以及WXSS模板样式
【MySQL —— 数据库约束】
从一文中了解SSRF的各种绕过姿势及攻击思路
SAP ABAP Gateway Client 里 OData 测试的 PUT, PATCH, MERGE 请求有什么区别
北路智控上市首日破发:公司市值59亿 募资15.6亿
投资的思考
2022 Shandong International Youth Eye Health Industry Exhibition, Vision Health Exhibition, Optometry Exhibition
在表格数据上,为什么基于树的模型仍然优于深度学习?
向往的生活
如何突破测试/开发程序员思维?一种不一样的感觉......
阿南的对话