骑士周游回溯算法 - 图1
    骑士周游回溯算法 - 图2

    1. /**
    2. * 《骑士周游回溯算法》
    3. *
    4. * 马踏棋盘算法也被称为骑士周游问题,将马随机放在国际象棋的8X8棋盘,Board[0~7][0~7]的某个方格中,马按走棋规则(马走日字)进行移动。要求每个方格只进入一次,走遍棋盘上全部64个方格
    5. *
    6. * 骑士周游问题的解决步骤和思路
    7. * 1.创建棋盘chessBoard,是一个二维数组
    8. * 2.将当前位置设置为已经访问,然后根据当前位置,计算马儿还能走哪些位置,
    9. * 并放入到一个集合中(ArrayList),最多有8个位置,每走一步,就使用step+1
    10. * 3.遍历ArrayList中存放的所有位置,看看哪个可以走通,如果走通,就继续,走不通,就回溯
    11. * 4.判断马儿是否完成了任务,使用step 和应该走的步数比较,如果没有达到数量,则表示没有完成任务,将整个棋盘置0
    12. * 注意:马儿不同的走法(策略) ,会得到不同的结果,效率也会有影响(优化)
    13. */
    14. public class HorseChessboard {
    15. /**
    16. * 棋盘的行数
    17. */
    18. private int x;
    19. /**
    20. * 棋盘的列数
    21. */
    22. private int y;
    23. /**
    24. * 创建二维数组,表示棋盘
    25. */
    26. private int[][] chessboard;
    27. /**
    28. * 创建一个数组,标记棋盘的各个位置是否被访问过
    29. */
    30. private boolean visited[];
    31. /**
    32. * 使用一个属性,标记是否棋盘的所有位置都被访问过,如果是true,表示成功
    33. */
    34. private boolean finished;
    35. /**
    36. * 马儿的位置
    37. */
    38. private class Point{
    39. /**
    40. * 行数
    41. */
    42. private int x;
    43. /**
    44. * 列数
    45. */
    46. private int y;
    47. public Point() {
    48. }
    49. public Point(int x, int y) {
    50. this.x = x;
    51. this.y = y;
    52. }
    53. public Point(Point p) {
    54. this.x = p.x;
    55. this.y = p.y;
    56. }
    57. }
    58. public HorseChessboard(int x, int y) {
    59. this.x = x;
    60. this.y = y;
    61. this.chessboard = new int[x][y];
    62. this.visited = new boolean[x * y];
    63. this.finished = false;
    64. }
    65. /**
    66. * 从指定位置开始
    67. */
    68. public void start(int row, int column) {
    69. traversalChessboard(chessboard, row, column, 1);
    70. //输出棋盘的最后情况
    71. for (int[] rows : chessboard) {
    72. for (int step : rows) {
    73. System.out.print(step + "\t");
    74. }
    75. System.out.println();
    76. }
    77. }
    78. /**
    79. * 完成骑士周游问题的算法
    80. *
    81. * @param chessboard 棋盘
    82. * @param row 马儿当前的位置的行 从0开始
    83. * @param column 马儿当前的位置的类 从0开始
    84. * @param step 是第几步,初始位置就是第1步
    85. */
    86. public void traversalChessboard(int[][] chessboard, int row, int column, int step) {
    87. chessboard[row][column] = step;
    88. // 标记该位置已经访问
    89. visited[row * this.x + column] = true;
    90. // 获取当前位置可以走的下一个位置的集合
    91. ArrayList<Point> ps = next(new Point(row, column));
    92. //遍历ps
    93. while (!ps.isEmpty()) {
    94. // 取出下一个可以走的位置
    95. Point p = ps.remove(0);
    96. // 判断该点是否已经访问过
    97. if (!visited[p.x * this.x + p.y]) {
    98. traversalChessboard(chessboard, p.x, p.y, step + 1);
    99. }
    100. }
    101. //判断马儿是否完成了任务,使用step 和应该走的步数比较
    102. //如果没有达到数量,刚表示没有完成任务,将整个棋盘置0
    103. //说明:step < X * Y 成立的情况有两种
    104. // 1、棋盘到目前位置,仍然没有走完
    105. //2、棋盘处于一个回溯过程
    106. if (step < this.x * this.y && !finished) {
    107. chessboard[row][column] = 0;
    108. visited[row * this.x + column] = false;
    109. } else {
    110. finished = true;
    111. }
    112. }
    113. /**
    114. * 功能:根据当前位置(Point)对象,计算马儿还能走哪些位置(Point),并放入
    115. * 到一个集合中(ArrayList),最多有8个位置
    116. * @return
    117. */
    118. public ArrayList<Point> next(Point curPoint) {
    119. //创建一个ArrayList
    120. ArrayList<Point> ps = new ArrayList<>();
    121. //创建一个Point
    122. Point p1 = new Point();
    123. //表示马儿可以走5这个位置
    124. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {
    125. ps.add(new Point(p1));
    126. }
    127. //表示马儿可以走6这个位置
    128. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {
    129. ps.add(new Point(p1));
    130. }
    131. //表示马儿可以走7这个位置
    132. if ((p1.x = curPoint.x + 1) < this.x && (p1.y = curPoint.y - 2) >= 0) {
    133. ps.add(new Point(p1));
    134. }
    135. //表示马儿可以走0这个位置
    136. if ((p1.x = curPoint.x + 2) < this.x && (p1.y = curPoint.y - 1) >= 0) {
    137. ps.add(new Point(p1));
    138. }
    139. //表示马儿可以走1这个位置
    140. if ((p1.x = curPoint.x + 2) < this.x && (p1.y = curPoint.y + 1) < this.y) {
    141. ps.add(new Point(p1));
    142. }
    143. //表示马儿可以走2这个位置
    144. if ((p1.x = curPoint.x + 1) < this.x && (p1.y = curPoint.y + 2) < this.y) {
    145. ps.add(new Point(p1));
    146. }
    147. //表示马儿可以走3这个位置
    148. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < this.y) {
    149. ps.add(new Point(p1));
    150. }
    151. //表示马儿可以走4这个位置
    152. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < this.y) {
    153. ps.add(new Point(p1));
    154. }
    155. return ps;
    156. }
    157. public static void main(String[] args) {
    158. HorseChessboard horseChessboard = new HorseChessboard(8, 8);
    159. long start = System.currentTimeMillis();
    160. horseChessboard.start(0,0);
    161. long end = System.currentTimeMillis();
    162. System.out.println("耗时:" + (end - start) + "ms");
    163. }
    164. }