Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)

时间:2023-03-08 21:23:45
Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)

贪吃的小老鼠又回来了,这次有什么新的办法吃到奶酪呢?

规则不变,只能上下左右在格子内移动。

Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)

因为上次的深度优先算法让老鼠走了不少冤枉路,这次老鼠带来了帮手探路鼠。探路鼠的使用规则如下:

小老鼠按右、下、左、上的顺序向身边四个格子尝试放出探路鼠,如果遇到猫、出边界、已经有探路鼠存在的格子则放弃。

每只探路鼠都有唯一的顺序号,第一只从1开始,每放成功一只序号递增1。

老鼠探路完成后,找出当前未行动过的顺序号最小的探路鼠重复老鼠的工作,即尝试向右、下、左、上四个格子放出探路鼠。

用图来解释一下,第一步,小老鼠放出两只探路鼠,如下:

Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)

老鼠行动完成,按规则是1号探路鼠行动。由于地形所限,1号尝试了右、下、左、上四个方向后,只成功放出了3号。

Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)

1号完成后,轮到2号行动,也只成功放出一只,即4号

Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)

据此规则不难推算出,接下来依次是:

3号放出5号

4号放出6号

5号放出7号

6号放出8号

7号放出9、10号

8号放出11号

9号放出12号

如下图:

Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)

注意12号探路鼠首先发现了奶酪,这时它向上一级即9号汇报,9号向7号汇报。。。,12->9->7->5->3->1->老鼠,可以计算出最少的步数是6。

上面的探路过程即广度优先搜索(Breadth First Search, BFS),与深度优先搜索的一条路走到黑不同,每到一个新的位置都向四个方向分别探索,找出每一个分支,并对每一个分支继续探索。

用程序来描绘这一过程,首先需要把迷宫“数字化“,如下图:

Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)

这样就可以用一个二维数组存储迷宫:

  1. int width = 5;  //迷宫宽度
  2. int height = 4;  //迷宫高度
  3. int[][] maze = new int[width][height];
  4. maze[2][0] = 1;
  5. maze[1][2] = 1;
  6. maze[2][2] = 1;
  7. maze[4][1] = 1;

用一个同样大小的二维数组标记已经放了探路鼠的点

  1. int[][] mark = new int[width][height];
  2. mark[0][0] = 1;

每个“探路鼠”需要知道自己所在位置(坐标),自己的上一级是谁。为了方便,还用它记录了到达该位置需要的步数。用一个类来表示:

  1. static class Trace {
  2. public Trace(int x, int y, int father, int step) {
  3. this.x = x;
  4. this.y = y;
  5. this.father = father;
  6. this.step = step;
  7. }
  8. private int x;
  9. private int y;
  10. private int father;
  11. private int step;
  12. public int getX() {
  13. return x;
  14. }
  15. public void setX(int x) {
  16. this.x = x;
  17. }
  18. public int getY() {
  19. return y;
  20. }
  21. public void setY(int y) {
  22. this.y = y;
  23. }
  24. public int getFather() {
  25. return father;
  26. }
  27. public void setFather(int father) {
  28. this.father = father;
  29. }
  30. public int getStep() {
  31. return step;
  32. }
  33. public void setStep(int step) {
  34. this.step = step;
  35. }
  36. @Override
  37. public String toString() {
  38. return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
  39. }
  40. }

完整代码如下:

  1. import org.apache.commons.lang3.builder.ToStringBuilder;
  2. import org.apache.commons.lang3.builder.ToStringStyle;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. /**
  6. * 老鼠走迷宫 BFS算法
  7. * Created by autfish on 2016/9/5.
  8. */
  9. public class BfsRatMaze {
  10. int min = Integer.MAX_VALUE;
  11. int endX = 4;  //目标点横坐标
  12. int endY = 2;  //目标点纵坐标
  13. int width = 5;  //迷宫宽度
  14. int height = 4;  //迷宫高度
  15. int[][] maze;
  16. int[][] mark;
  17. static class Trace {
  18. public Trace(int x, int y, int father, int step) {
  19. this.x = x;
  20. this.y = y;
  21. this.father = father;
  22. this.step = step;
  23. }
  24. private int x;
  25. private int y;
  26. private int father;
  27. private int step;
  28. public int getX() {
  29. return x;
  30. }
  31. public void setX(int x) {
  32. this.x = x;
  33. }
  34. public int getY() {
  35. return y;
  36. }
  37. public void setY(int y) {
  38. this.y = y;
  39. }
  40. public int getFather() {
  41. return father;
  42. }
  43. public void setFather(int father) {
  44. this.father = father;
  45. }
  46. public int getStep() {
  47. return step;
  48. }
  49. public void setStep(int step) {
  50. this.step = step;
  51. }
  52. @Override
  53. public String toString() {
  54. return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
  55. }
  56. }
  57. public void bfs() {
  58. int[][] next = new int[][] { //按右->下->左->上的顺序尝试
  59. {1, 0},
  60. {0, 1},
  61. {-1, 0},
  62. {0, -1}
  63. };
  64. int head = 0, tail = 1;
  65. int startX = 0, startY = 0;
  66. int nextX, nextY;
  67. List<Trace> traces = new ArrayList<>();
  68. traces.add(head, new Trace(startX, startY, -1, 0));
  69. mark[startX][startY] = 1;
  70. int flag = 0;
  71. while(head < tail) {
  72. for(int k = 0; k <= 3; k++) {
  73. nextX = traces.get(head).getX() + next[k][0];
  74. nextY = traces.get(head).getY() + next[k][1];
  75. if(nextX < 0 || nextX >= width || nextY < 0 || nextY >= height) {  //超出边界
  76. continue;
  77. }
  78. //没有障碍且没有探索过, 则把当前位置标记为未探索点
  79. if(maze[nextX][nextY] == 0 && mark[nextX][nextY] == 0) {
  80. this.mark[nextX][nextY] = 1;
  81. traces.add(tail, new Trace(nextX, nextY, head, traces.get(head).getStep() + 1));
  82. tail++;
  83. }
  84. if(nextX == endX && nextY == endY) {
  85. flag = 1;
  86. break;
  87. }
  88. }
  89. if(flag == 1)
  90. break;
  91. //一个点的四个方向探索完成, 取编号最小的一个未探索点
  92. head++;
  93. }
  94. Trace end = traces.get(tail - 1);
  95. int father = end.getFather();
  96. System.out.println("共" + end.getStep() + "步");
  97. StringBuilder path = new StringBuilder();
  98. path.insert(0, "->[" + end.getX() + "," + end.getY() + "]");
  99. while(father >= 0) {
  100. Trace prev = traces.get(father);
  101. father = prev.getFather();
  102. if(father > -1)
  103. path.insert(0, "->[" + prev.getX() + "," + prev.getY() + "]");
  104. else
  105. path.insert(0, "[" + prev.getX() + "," + prev.getY() + "]");
  106. }
  107. System.out.println(path.toString());
  108. }
  109. public void initMaze() {
  110. this.maze = new int[width][height];
  111. this.mark = new int[width][height];
  112. this.maze[2][0] = 1;
  113. this.maze[1][2] = 1;
  114. this.maze[2][2] = 1;
  115. this.maze[4][1] = 1;
  116. this.mark[0][0] = 1;
  117. //打印迷宫 _表示可通行 *表示障碍 !表示目标
  118. for(int y = 0; y < height; y++) {
  119. for(int x = 0; x < width; x++) {
  120. if(x == endX && y == endY) {
  121. System.out.print("!  ");
  122. }  else if(this.maze[x][y] == 1) {
  123. System.out.print("*  ");
  124. } else {
  125. System.out.print("_  ");
  126. }
  127. }
  128. System.out.println();
  129. }
  130. System.out.println();
  131. }
  132. public static void main(String[] args) {
  133. BfsRatMaze b = new BfsRatMaze();
  134. b.initMaze();
  135. b.bfs();
  136. }
  137. }

运行结果:

  1. _  _  *  _  _
  2. _  _  _  _  *
  3. _  *  *  _  !
  4. _  _  _  _  _
  5. 共6步
  6. [0,0]->[1,0]->[1,1]->[2,1]->[3,1]->[3,2]->[4,2]

用深度优先搜索的程序见: