当前位置:   article > 正文

【算法】马踏棋盘(骑士周游)问题回溯算法实现以及使用贪心算法优化

【算法】马踏棋盘(骑士周游)问题回溯算法实现以及使用贪心算法优化

目录

1.游戏规则

2.算法分析

3.解决步骤和思路

4.马踏棋盘算法的代码实现

4.1计算马儿还能走哪些位置

4.2马踏棋盘的核心代码

4.3马踏棋盘算法完整代码

4.4使用贪心算法进行优化

4.4.1思路

4.4.2代码实现 


1.游戏规则

将马儿随机放在国际象棋的 8*8 棋盘的某个方格中,马儿按照“马走日”进行移动,要求每个方格只进入一次,走遍棋盘上全部 64 个方格。

2.算法分析

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

3.解决步骤和思路

  1. 创建棋盘 chessBoard,是一个二维数组
  2. 将当前位置设置为已经访问,然后根据当前位置,计算马儿还能走哪些位置,并放入到一个集合(ArrayList)中,最多有 8 个位置,每走一步,就让 step + 1
  3. 遍历 ArrayList 中存放的所有位置,看看哪个可以走通,如果走通就继续,走不通就回溯
  4. 判断马儿是否完成了任务,使用 step 和应该走的步数比较,如果没有达到数量,则表示没有完成任务,将整个棋盘置 0

注意:马儿不同的走法会得到不同的结果,效率也会有影响

4.马踏棋盘算法的代码实现

4.1计算马儿还能走哪些位置

  1. public static ArrayList<Point> next(Point curPoint) {
  2. //创建一个 ArrayList
  3. ArrayList<Point> ps = new ArrayList<>();
  4. //创建一个 Point
  5. Point p1 = new Point();
  6. //左偏上
  7. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {
  8. ps.add(new Point(p1));
  9. }
  10. //上偏左
  11. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {
  12. ps.add(new Point(p1));
  13. }
  14. //上偏右
  15. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
  16. ps.add(new Point(p1));
  17. }
  18. //右偏上
  19. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
  20. ps.add(new Point(p1));
  21. }
  22. //右偏下
  23. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
  24. ps.add(new Point(p1));
  25. }
  26. //下偏右
  27. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
  28. ps.add(new Point(p1));
  29. }
  30. //下偏左
  31. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
  32. ps.add(new Point(p1));
  33. }
  34. //左偏下
  35. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
  36. ps.add(new Point(p1));
  37. }
  38. return ps;
  39. }

4.2马踏棋盘的核心代码

  1. /**
  2. * 完成马踏棋盘问题的算法
  3. * @param chessboard 棋盘
  4. * @param row 马儿当前位置的行 从0开始
  5. * @param column 马儿当前位置的列 从0开始
  6. * @param step 是第几步,初始位置就是第1步
  7. */
  8. public static void traversalChessboard(int[][] chessboard, int row, int column, int step) {
  9. chessboard[row][column] = step;
  10. visited[row * X + column] = true; //标记该位置已经访问
  11. //获取当前位置可以走的下一个位置的集合
  12. ArrayList<Point> ps = next(new Point(column, row));
  13. //遍历 ps
  14. while (!ps.isEmpty()) {
  15. Point p = ps.remove(0);//取出下一个可以走的位置
  16. //判断该点是否已经访问过
  17. if (!visited[p.y * X + p.x]) {//说明还没有访问过
  18. traversalChessboard(chessboard, p.y, p.x, step + 1);
  19. }
  20. }
  21. /*
  22. 判断马儿是否完成任务,使用step和应该走的步数比较
  23. 如果没有达到数量,则表示没有完成任务,将整个棋盘置0
  24. 说明:step < X * Y 成立的条件有两种
  25. 1.棋盘到目前为止,仍然没有走完
  26. 2.棋盘处于一个回溯过程
  27. */
  28. if (step < X * Y && !finished) {
  29. chessboard[row][column] = 0;
  30. visited[row * X + column] = false;
  31. } else {
  32. finished = true;
  33. }
  34. }

4.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. visited[row * X + column] = true; //标记该位置已经访问
  41. //获取当前位置可以走的下一个位置的集合
  42. ArrayList<Point> ps = next(new Point(column, row));
  43. //遍历 ps
  44. while (!ps.isEmpty()) {
  45. Point p = ps.remove(0);//取出下一个可以走的位置
  46. //判断该点是否已经访问过
  47. if (!visited[p.y * X + p.x]) {//说明还没有访问过
  48. traversalChessboard(chessboard, p.y, p.x, step + 1);
  49. }
  50. }
  51. /*
  52. 判断马儿是否完成任务,使用step和应该走的步数比较
  53. 如果没有达到数量,则表示没有完成任务,将整个棋盘置0
  54. 说明:step < X * Y 成立的条件有两种
  55. 1.棋盘到目前为止,仍然没有走完
  56. 2.棋盘处于一个回溯过程
  57. */
  58. if (step < X * Y && !finished) {
  59. chessboard[row][column] = 0;
  60. visited[row * X + column] = false;
  61. } else {
  62. finished = true;
  63. }
  64. }
  65. public static ArrayList<Point> next(Point curPoint) {
  66. //创建一个 ArrayList
  67. ArrayList<Point> ps = new ArrayList<>();
  68. //创建一个 Point
  69. Point p1 = new Point();
  70. //左偏上
  71. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {
  72. ps.add(new Point(p1));
  73. }
  74. //上偏左
  75. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {
  76. ps.add(new Point(p1));
  77. }
  78. //上偏右
  79. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
  80. ps.add(new Point(p1));
  81. }
  82. //右偏上
  83. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
  84. ps.add(new Point(p1));
  85. }
  86. //右偏下
  87. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
  88. ps.add(new Point(p1));
  89. }
  90. //下偏右
  91. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
  92. ps.add(new Point(p1));
  93. }
  94. //下偏左
  95. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
  96. ps.add(new Point(p1));
  97. }
  98. //左偏下
  99. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
  100. ps.add(new Point(p1));
  101. }
  102. return ps;
  103. }
  104. }

4.4使用贪心算法进行优化

4.4.1思路

  1. 获取当前位置的可以走的下一个位置的集合
  2. 对 ps 中所有的 Point 的下一步的所有集合的数目进行非递减排序

4.4.2代码实现 

  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. visited[row * X + column] = true; //标记该位置已经访问
  41. //获取当前位置可以走的下一个位置的集合
  42. ArrayList<Point> ps = next(new Point(column, row));
  43. //对 ps 进行排序
  44. sort(ps);
  45. //遍历 ps
  46. while (!ps.isEmpty()) {
  47. Point p = ps.remove(0);//取出下一个可以走的位置
  48. //判断该点是否已经访问过
  49. if (!visited[p.y * X + p.x]) {//说明还没有访问过
  50. traversalChessboard(chessboard, p.y, p.x, step + 1);
  51. }
  52. }
  53. /*
  54. 判断马儿是否完成任务,使用step和应该走的步数比较
  55. 如果没有达到数量,则表示没有完成任务,将整个棋盘置0
  56. 说明:step < X * Y 成立的条件有两种
  57. 1.棋盘到目前为止,仍然没有走完
  58. 2.棋盘处于一个回溯过程
  59. */
  60. if (step < X * Y && !finished) {
  61. chessboard[row][column] = 0;
  62. visited[row * X + column] = false;
  63. } else {
  64. finished = true;
  65. }
  66. }
  67. public static ArrayList<Point> next(Point curPoint) {
  68. //创建一个 ArrayList
  69. ArrayList<Point> ps = new ArrayList<>();
  70. //创建一个 Point
  71. Point p1 = new Point();
  72. //左偏上
  73. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {
  74. ps.add(new Point(p1));
  75. }
  76. //上偏左
  77. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {
  78. ps.add(new Point(p1));
  79. }
  80. //上偏右
  81. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
  82. ps.add(new Point(p1));
  83. }
  84. //右偏上
  85. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
  86. ps.add(new Point(p1));
  87. }
  88. //右偏下
  89. if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
  90. ps.add(new Point(p1));
  91. }
  92. //下偏右
  93. if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
  94. ps.add(new Point(p1));
  95. }
  96. //下偏左
  97. if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
  98. ps.add(new Point(p1));
  99. }
  100. //左偏下
  101. if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
  102. ps.add(new Point(p1));
  103. }
  104. return ps;
  105. }
  106. //根据当前这一步的所有下一步的数目进行非递减排序,减少回溯的可能
  107. public static void sort(ArrayList<Point> ps) {
  108. ps.sort(new Comparator<Point>() {
  109. @Override
  110. public int compare(Point o1, Point o2) {
  111. //获取到 o1 的下一步的所有位置的个数
  112. int count1 = next(o1).size();
  113. //获取到 o2 的下一步的所有位置的个数
  114. int count2 = next(o2).size();
  115. if (count1 < count2) {
  116. return -1;
  117. }else if (count1 == count2) {
  118. return 0;
  119. } else {
  120. return 1;
  121. }
  122. }
  123. });
  124. }
  125. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/1001766
推荐阅读
相关标签
  

闽ICP备14008679号