/** * 跳马问题: * 把象棋棋盘放在第一象限位置,棋盘的最左下角位置是(0,0),那么整个棋盘就是横坐标上九条线,纵坐标上十条线的区域 * 给定三个参数,x,y,k * 返回马从(0,0)位置出发,必须走K步,最后落在(x,y)上的方法数有多少个。 */public class Code02_HorseJump {// #############################方式一,暴力递归##################################################### // 10 * 9 // 主函数 public static int jump(int a, int b, int k) { return process(0, 0, k, a, b); } // 当前来到的位置是(x,y) // 还剩下rest步需要跳 // 跳完rest步,正好跳到a,b的方法数是多少? public static int process(int x, int y, int rest, int a, int b) { // 跳出棋盘返回0 if (x < 0 || x > 9 || y < 0 || y > 8) { return 0; } if (rest == 0) { return (x == a && y == b) ? 1 : 0; } // x,y位置下一步八个方向上跳法的方法数总和。 int ways = process(x + 2, y + 1, rest - 1, a, b); ways += process(x + 1, y + 2, rest - 1, a, b); ways += process(x - 1, y + 2, rest - 1, a, b); ways += process(x - 2, y + 1, rest - 1, a, b); ways += process(x - 2, y - 1, rest - 1, a, b); ways += process(x - 1, y - 2, rest - 1, a, b); ways += process(x + 1, y - 2, rest - 1, a, b); ways += process(x + 2, y - 1, rest - 1, a, b); return ways; }// #############################方式二,动态规划##################################################### // *Note*:对于这种三维,有三个动态参数的。如果关系依赖简单,则可以进行优化,改成动态规划的形式。如果关系依赖复杂,直接使用记忆化搜索(缓存法)无需修改。 // dp解决 三维, xy为底,rest为高。通过第0层,可以推出第一层,第i层的位置依赖的是i-1层的。同一层没有依赖关系。rest==0即第0层的时候 // 只有 a,b位置是1,其他都为0,则第一层所有数据确定,然后可以得到第二层的数据,依次向上。 public static int dp(int a, int b, int k) { int[][][] dp = new int[10][9][k + 1]; // 第0层 dp[a][b][0] = 1; // 1-K层 for (int rest = 1; rest <= k; rest++) { // 最外层for循环表示 有多少层。 for (int x = 0; x < 10; x++) { // 里面两个for循环,穷举,x,y的值。 for (int y = 0; y < 9; y++) { int ways = pick(dp, x + 2, y + 1, rest - 1); ways += pick(dp, x + 1, y + 2, rest - 1); ways += pick(dp, x - 1, y + 2, rest - 1); ways += pick(dp, x - 2, y + 1, rest - 1); ways += pick(dp, x - 2, y - 1, rest - 1); ways += pick(dp, x - 1, y - 2, rest - 1); ways += pick(dp, x + 1, y - 2, rest - 1); ways += pick(dp, x + 2, y - 1, rest - 1); dp[x][y][rest] = ways; } } } return dp[0][0][k]; } // 获取x,y.rest位置的值,越界返回0. public static int pick(int[][][] dp, int x, int y, int rest) { if (x < 0 || x > 9 || y < 0 || y > 8) { return 0; } return dp[x][y][rest]; }// ################################################################################# 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]的值,但如果(i,j)位置越界的话,返回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 test(int x){ x++; } public static void main(String[] args) { Integer x = 7; int y = 7; test(x); System.out.println(x); int step = 10;// System.out.println(ways(x, y, step));// System.out.println(dp(x, y, step));// System.out.println(jump(x, y, step)); }}