一、马踏棋盘算法介绍和游戏演示

  1. 马踏棋盘算法也被称为骑士周游问题
  2. 将马随机放在国际象棋的 8×8 棋盘 Board0~7的某个方格中,马按走棋规则(马走日字)进行移动。要求 每个方格只进入一次,走遍棋盘上全部 64 个方格
  3. 游戏演示: http://www.4399.com/flash/146267_2.htm

马踏棋盘算法 - 图1

二、马踏棋盘游戏代码实现

  1. 马踏棋盘问题(骑士周游问题)实际上是图的深度优先搜索(DFS)的应用。
  2. 如果使用回溯(就是深度优先搜索)来解决,假如马儿踏了 53 个点,如图:走到了第 53 个,坐标(1,0),发 现已经走到尽头,没办法,那就只能回退了,查看其他的路径,就在棋盘上不停的回溯…… ,思路分析+代码 实现
  • 思路图解马踏棋盘算法 - 图2
  • 并使用贪心算法(greedyalgorithm)进行优化。解决马踏棋盘问题马踏棋盘算法 - 图3

代码实现:

  1. public class HorseChessboard {
  2. private static int X; // 棋盘的列数
  3. private static int Y; // 棋盘的行数
  4. //创建一个数组,标记棋盘的各个位置是否被访问过
  5. private static boolean visited[];
  6. //使用一个属性,标记是否棋盘的所有位置都被访问
  7. private static boolean finished; // 如果为 true,表示成功
  8. public static void main(String[] args) {
  9. System.out.println("骑士周游算法,开始运行~~");
  10. //测试骑士周游算法是否正确
  11. X = 8;
  12. Y = 8;
  13. int row = 1; //马儿初始位置的行,从 1 开始编号
  14. int column = 1; //马儿初始位置的列,从 1 开始编号
  15. //创建棋盘
  16. int[][] chessboard = new int[X][Y];
  17. visited = new boolean[X * Y];//初始值都是 false
  18. //测试一下耗时
  19. long start = System.currentTimeMillis();
  20. traversalChessboard(chessboard, row - 1, column - 1, 1);
  21. long end = System.currentTimeMillis();
  22. System.out.println("共耗时: " + (end - start) + " 毫秒");
  23. //输出棋盘的最后情况
  24. for(int[] rows : chessboard) {
  25. for(int step: rows) {
  26. System.out.print(step + "\t");
  27. }
  28. System.out.println();
  29. }
  30. }
  31. /**
  32. * 完成骑士周游问题的算法
  33. * @param chessboard 棋盘
  34. * @param row 马儿当前的位置的行 从 0 开始
  35. * @param column 马儿当前的位置的列 从 0 开始
  36. * @param step 是第几步 ,初始位置就是第 1 步
  37. */
  38. public static void traversalChessboard(int[][] chessboard, int row, int column, int step) {
  39. chessboard[row][column] = step;
  40. //row = 4 X = 8 column = 4 = 4 * 8 + 4 = 36
  41. visited[row * X + column] = true; //标记该位置已经访问
  42. //获取当前位置可以走的下一个位置的集合
  43. ArrayList<Point> ps = next(new Point(column, row));
  44. //对 ps 进行排序,排序的规则就是对 ps 的所有的 Point 对象的下一步的位置的数目,进行非递减排序
  45. sort(ps);
  46. //遍历 ps
  47. while(!ps.isEmpty()) {
  48. Point p = ps.remove(0);//取出下一个可以走的位置
  49. //判断该点是否已经访问过
  50. if(!visited[p.y * X + p.x]) {//说明还没有访问过
  51. traversalChessboard(chessboard, p.y, p.x, step + 1);
  52. }
  53. }
  54. //判断马儿是否完成了任务,使用 step 和应该走的步数比较 ,
  55. //如果没有达到数量,则表示没有完成任务,将整个棋盘置 0
  56. //说明: step < X * Y 成立的情况有两种
  57. //1. 棋盘到目前位置,仍然没有走完
  58. //2. 棋盘处于一个回溯过程
  59. if(step < X * Y && !finished ) {
  60. chessboard[row][column] = 0;
  61. visited[row * X + column] = false;
  62. } else {
  63. finished = true;
  64. }
  65. }
  66. /**
  67. * 功能: 根据当前位置(Point 对象),计算马儿还能走哪些位置(Point),并放入到一个集合中(ArrayList), 最多
  68. 有 8 个位置
  69. * @param curPoint
  70. * @return
  71. */
  72. public static ArrayList<Point> next(Point curPoint) {
  73. //创建一个 ArrayList
  74. ArrayList<Point> ps = new ArrayList<Point>();
  75. //创建一个 Point
  76. Point p1 = new Point();
  77. //表示马儿可以走 5 这个位置
  78. if((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y -1) >= 0) {
  79. ps.add(new Point(p1));
  80. }
  81. //判断马儿可以走 6 这个位置
  82. if((p1.x = curPoint.x - 1) >=0 && (p1.y=curPoint.y-2)>=0) {
  83. ps.add(new Point(p1));
  84. }
  85. //判断马儿可以走 7 这个位置
  86. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
  87. ps.add(new Point(p1));
  88. }
  89. //判断马儿可以走 0 这个位置
  90. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
  91. ps.add(new Point(p1));
  92. }
  93. //判断马儿可以走 1 这个位置
  94. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
  95. ps.add(new Point(p1));
  96. }
  97. //判断马儿可以走 2 这个位置
  98. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
  99. ps.add(new Point(p1));
  100. }
  101. //判断马儿可以走 3 这个位置
  102. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
  103. ps.add(new Point(p1));
  104. }
  105. //判断马儿可以走 4 这个位置
  106. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
  107. ps.add(new Point(p1));
  108. }
  109. return ps;
  110. }
  111. //根据当前这个一步的所有的下一步的选择位置,进行非递减排序, 减少回溯的次数
  112. public static void sort(ArrayList<Point> ps) {
  113. ps.sort(new Comparator<Point>() {
  114. @Override
  115. public int compare(Point o1, Point o2) {
  116. // TODO Auto-generated method stub
  117. //获取到 o1 的下一步的所有位置个数
  118. int count1 = next(o1).size();
  119. //获取到 o2 的下一步的所有位置个数
  120. int count2 = next(o2).size();
  121. if(count1 < count2) {
  122. return -1;
  123. } else if (count1 == count2) {
  124. return 0;
  125. } else {
  126. return 1;
  127. }
  128. }
  129. });
  130. }
  131. }