1. /**
    2. * 跳马问题:
    3. * 把象棋棋盘放在第一象限位置,棋盘的最左下角位置是(0,0),那么整个棋盘就是横坐标上九条线,纵坐标上十条线的区域
    4. * 给定三个参数,x,y,k
    5. * 返回马从(0,0)位置出发,必须走K步,最后落在(x,y)上的方法数有多少个。
    6. */
    7. public class Code02_HorseJump {
    8. // #############################方式一,暴力递归#####################################################
    9. // 10 * 9
    10. // 主函数
    11. public static int jump(int a, int b, int k) {
    12. return process(0, 0, k, a, b);
    13. }
    14. // 当前来到的位置是(x,y)
    15. // 还剩下rest步需要跳
    16. // 跳完rest步,正好跳到a,b的方法数是多少?
    17. public static int process(int x, int y, int rest, int a, int b) {
    18. // 跳出棋盘返回0
    19. if (x < 0 || x > 9 || y < 0 || y > 8) {
    20. return 0;
    21. }
    22. if (rest == 0) {
    23. return (x == a && y == b) ? 1 : 0;
    24. }
    25. // x,y位置下一步八个方向上跳法的方法数总和。
    26. int ways = process(x + 2, y + 1, rest - 1, a, b);
    27. ways += process(x + 1, y + 2, rest - 1, a, b);
    28. ways += process(x - 1, y + 2, rest - 1, a, b);
    29. ways += process(x - 2, y + 1, rest - 1, a, b);
    30. ways += process(x - 2, y - 1, rest - 1, a, b);
    31. ways += process(x - 1, y - 2, rest - 1, a, b);
    32. ways += process(x + 1, y - 2, rest - 1, a, b);
    33. ways += process(x + 2, y - 1, rest - 1, a, b);
    34. return ways;
    35. }
    36. // #############################方式二,动态规划#####################################################
    37. // *Note*:对于这种三维,有三个动态参数的。如果关系依赖简单,则可以进行优化,改成动态规划的形式。如果关系依赖复杂,直接使用记忆化搜索(缓存法)无需修改。
    38. // dp解决 三维, xy为底,rest为高。通过第0层,可以推出第一层,第i层的位置依赖的是i-1层的。同一层没有依赖关系。rest==0即第0层的时候
    39. // 只有 a,b位置是1,其他都为0,则第一层所有数据确定,然后可以得到第二层的数据,依次向上。
    40. public static int dp(int a, int b, int k) {
    41. int[][][] dp = new int[10][9][k + 1];
    42. // 第0层
    43. dp[a][b][0] = 1;
    44. // 1-K层
    45. for (int rest = 1; rest <= k; rest++) { // 最外层for循环表示 有多少层。
    46. for (int x = 0; x < 10; x++) { // 里面两个for循环,穷举,x,y的值。
    47. for (int y = 0; y < 9; y++) {
    48. int ways = pick(dp, x + 2, y + 1, rest - 1);
    49. ways += pick(dp, x + 1, y + 2, rest - 1);
    50. ways += pick(dp, x - 1, y + 2, rest - 1);
    51. ways += pick(dp, x - 2, y + 1, rest - 1);
    52. ways += pick(dp, x - 2, y - 1, rest - 1);
    53. ways += pick(dp, x - 1, y - 2, rest - 1);
    54. ways += pick(dp, x + 1, y - 2, rest - 1);
    55. ways += pick(dp, x + 2, y - 1, rest - 1);
    56. dp[x][y][rest] = ways;
    57. }
    58. }
    59. }
    60. return dp[0][0][k];
    61. }
    62. // 获取x,y.rest位置的值,越界返回0.
    63. public static int pick(int[][][] dp, int x, int y, int rest) {
    64. if (x < 0 || x > 9 || y < 0 || y > 8) {
    65. return 0;
    66. }
    67. return dp[x][y][rest];
    68. }
    69. // #################################################################################
    70. public static int ways(int a, int b, int step) {
    71. return f(0, 0, step, a, b);
    72. }
    73. public static int f(int i, int j, int step, int a, int b) {
    74. if (i < 0 || i > 9 || j < 0 || j > 8) {
    75. return 0;
    76. }
    77. if (step == 0) {
    78. return (i == a && j == b) ? 1 : 0;
    79. }
    80. 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)
    81. + 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)
    82. + f(i - 1, j - 2, step - 1, a, b) + f(i - 2, j - 1, step - 1, a, b);
    83. }
    84. public static int waysdp(int a, int b, int s) {
    85. int[][][] dp = new int[10][9][s + 1];
    86. dp[a][b][0] = 1;
    87. for (int step = 1; step <= s; step++) { // 按层来
    88. for (int i = 0; i < 10; i++) {
    89. for (int j = 0; j < 9; j++) {
    90. dp[i][j][step] = getValue(dp, i - 2, j + 1, step - 1) + getValue(dp, i - 1, j + 2, step - 1)
    91. + getValue(dp, i + 1, j + 2, step - 1) + getValue(dp, i + 2, j + 1, step - 1)
    92. + getValue(dp, i + 2, j - 1, step - 1) + getValue(dp, i + 1, j - 2, step - 1)
    93. + getValue(dp, i - 1, j - 2, step - 1) + getValue(dp, i - 2, j - 1, step - 1);
    94. }
    95. }
    96. }
    97. return dp[0][0][s];
    98. }
    99. // 在dp表中,得到dp[i][j][step]的值,但如果(i,j)位置越界的话,返回0;
    100. public static int getValue(int[][][] dp, int i, int j, int step) {
    101. if (i < 0 || i > 9 || j < 0 || j > 8) {
    102. return 0;
    103. }
    104. return dp[i][j][step];
    105. }
    106. public static void test(int x){
    107. x++;
    108. }
    109. public static void main(String[] args) {
    110. Integer x = 7;
    111. int y = 7;
    112. test(x);
    113. System.out.println(x);
    114. int step = 10;
    115. // System.out.println(ways(x, y, step));
    116. // System.out.println(dp(x, y, step));
    117. // System.out.println(jump(x, y, step));
    118. }
    119. }