我对如何使用队列解决迷宫非常困惑。我已经提供了一些javadoc和一些我教授给我们的伪代码。尽可能帮助。我已经查看了其他主题,但我无法理解,希望有人可以帮我解决我的解决方法。感谢
public class QueueMazeSolver implements MazeSolver {
private MazeGUI gui;
public static class Cell {
private int r;
private int c;
public Cell(int row, int col){
r = row;
c = col;
}
}
public QueueMazeSolver(){
gui = new MazeGUI( this );
}
/**
* This method is called when the start button is
* clicked in the MazeGUI. This method should solve the maze.
* This method may call MazeGUI.drawMaze(...) whenever the
* GUI display should be updated (after each step of the solution).
*
* The maze is provided as the first parameter. It is a 2D array containing
* characters that represent the spaces in the maze. The following
* characters will be found in the array:
* '#' - This represents a wall.
* ' ' - This represents an open space (corridor)
*
* When calling MazeGUI.drawMaze(...) to update the display, the GUI
* will recognize the '#' and ' ' characters as well as the following:
* '@' - Means the cell is a space that has been explored
* '%' - Means that the cell is part of the best path to the goal.
*
* @param maze the maze (see above).
* @param startR the row of the start cell.
* @param startC the column of the start cell.
* @param endR the row of the end (goal) cell.
* @param endC the column of the end (goal) cell.
*/
@Override
public void solve(char[][] maze, int startR, int startC, int endR, int endC) {
maze[startR][startC] = '@';
ArrayQueue<Cell> agenda = new ArrayQueue<Cell>();
Cell temp = new Cell(startR, startC);
agenda.offer(temp);
// while agenda is not empty and red not found
while(!agenda.isEmpty() && maze[endR][endC] != '@' ){
Cell current = agenda.poll(); //remove front square from queue
/*
if front square is red
found it
else
mark amaze all unexplored neighbors of front
square and add them to the square
*/
if(current == new Cell(endR, endC) ){
break;
}
else{
=
}
}
/** Notes
maze[r][c] = '@' //marking cell seen
up = r-1, c
down = r+1, c
left = r, c-1
right = r, c+1
*/
}
if (!agenda.isEmpty())
gui.setStatusText("Maze is solvable");
else
gui.setStatusText("Maze is unsolvable");
gui.drawMaze(maze);
try {Thread.sleep(150);}
catch (InterruptedException e){
System.err.println("Thread interrupted");
}
}
public static void main(String[] args){
QueueMazeSolver solver = new QueueMazeSolver();
}
}
答案 0 :(得分:2)
似乎你正试图获得可能在迷宫中移动并到达red
单元格的路径,这些单元格有墙(无法交叉)或开放空间。
此代码基本上是应用breadth first search
我们从queue
移除了一个单元格,如果未访问周围的单元格[at distance 1 unit]
,请将其添加到queue
并访问它们。
伪代码(来自维基百科):
1 procedure BFS(G,v) is
2 create a queue Q
3 create a vector set V
4 enqueue v onto Q
5 add v to V
6 while Q is not empty loop
7 t ← Q.dequeue()
8 if t is what we are looking for then
9 return t
10 end if
11 for all edges e in G.adjacentEdges(t) loop
12 u ← G.adjacentVertex(t,e)
13 if u is not in V then
14 add u to V
15 enqueue u onto Q
16 end if
17 end loop
18 end loop
19 return none
20 end BFS
假设您位于小区(i,j)
,因此t = (i,j)
且adjacentEdges(t)为(i+1,j)
,(i,j+1)
,(i-1,j)
。 (i,j-1)
如果以前没有访问(i+1,j)
,请将其添加到队列中(因此,下次从队列中弹出时,您将获得它)否则如果访问它(即在V
中)则我们完成了它。对其他三个细胞重复相同的操作
通过这种方式,您可以执行O(m*n)
次操作,并且只需访问每个单元格一次。