迷宫问题和递归backtracker算法递归、迷宫、算法、问题

2023-09-11 23:10:12 作者:小呱唧吖

我要实现的递归backtracker算法来解决迷宫问题,但是我无法理解2.3命令(删除当前单元格和所选单元格之间的墙)将任何帮助我吗?

标记为'访问'当前单元格 如果当前单元格具有尚未被访问任何邻居 选择随机的未访问过的一位邻居 在当前单元格添加到堆栈 删除当前单元格,选择的单元格之间的墙 请选定单元格当前单元格 在递归调用这个函数 其他 从堆栈中删除最后一个当前单元格 原路返回到previous执行此功能

编辑 其实我想一个算法使用栈解决迷宫问题。

解决方案

这算法是一个迷宫的发电机的不是一个迷宫的求解器的。我们的想法是要创建一个随机的迷宫。你也想​​在迷宫中所有的点是从所有其他点到达。

如果你只是随意删除墙壁很可能是你的迷宫将无法连接。递归回溯算法需要照顾的通过创建一个随机的走的,并沿着随机游走拆除围墙。递归回溯一部分,还可以步行到每一个细胞都在迷宫中,甚至当你达到一个死胡同。

算法 递归 迷宫

i want to implement the Recursive backtracker algorithm to solve maze problem, but i cant understand 2.3 Command ("remove the wall between the current cell and the chosen cell") would any help me ?

Mark the current cell as 'Visited' If the current cell has any neighbours which have not been visited

Choose randomly one of the unvisited neighbours add the current cell to the stack remove the wall between the current cell and the chosen cell Make the chosen cell the current cell Recursively call this function

else

remove the last current cell from the stack Backtrack to the previous execution of this function

Edit In fact I want an algorithm to solve maze problem by using stack.

解决方案

That algorithm is a maze generator not a maze solver. The idea is that you want to create a random maze. You also want all points in the maze to be reachable from all other points.

If you just randomly remove walls it is likely that your maze will not be connected. The recursive backtracking algorithm takes care of this by creating a random walk and removing the walls along that random walk. The recursive backtracking part allows you to walk to every cell in the maze, even when you reach a dead end.