迷宫问题和递归回溯算法

发布于 2024-10-06 21:43:39 字数 362 浏览 5 评论 0原文

我想实现递归回溯算法来解决迷宫问题,但我无法理解 2.3 命令(“移除当前单元格和所选单元格之间的墙”)对我有帮助吗?

  1. 则将当前单元格标记为“已访问”
  2. 如果当前单元格有任何未访问过的邻居,
    1. 随机选择一个未访问过的邻居
    2. 将当前单元格添加到堆栈
    3. 移除当前单元格和所选单元格之间的墙壁
    4. 将所选单元格设为当前单元格
    5. 递归调用此函数
  3. else
    1. 从堆栈中删除最后一个当前单元
    2. 回溯到该函数的上一次执行

编辑 事实上,我想要一种使用堆栈来解决迷宫问题的算法。

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 ?

  1. Mark the current cell as 'Visited'
  2. If the current cell has any neighbours which have not been visited
    1. Choose randomly one of the unvisited neighbours
    2. add the current cell to the stack
    3. remove the wall between the current cell and the chosen cell
    4. Make the chosen cell the current cell
    5. Recursively call this function
  3. else
    1. remove the last current cell from the stack
    2. Backtrack to the previous execution of this function

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很快妥协 2024-10-13 21:43:39

该算法是迷宫生成器,而不是迷宫求解器。这个想法是你想创建一个随机迷宫。您还希望迷宫中的所有点都可以从所有其他点到达。

如果你只是随机移除墙壁,你的迷宫很可能不会连接起来。递归回溯算法通过创建随机行走并沿着该随机行走移除墙壁来解决这个问题。递归回溯部分允许您走到迷宫中的每个单元格,即使您到达死胡同。

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.

你好,陌生人 2024-10-13 21:43:39

你的算法适用于god模式。通常你应该这样做

  1. 如果当前单元格是出口,则完成
  2. 如果当前单元格有任何未访问过的邻居不是墙壁
    1. 随机选择一个未访问过的非墙邻居
    2. 将当前单元格添加到堆栈中
    3. 什么都没有
    4. 使所选单元格成为当前单元格
    5. 递归调用该函数
  3. else
    1. 从堆栈中删除最后一个当前单元
    2. 回溯到该函数的上一次执行

Your algorithm is for god mode. Normally you should do

  1. If the current cell is the exit, then finished
  2. If the current cell has any neighbours which have not been visited that are not walls
    1. Choose randomly one of the unvisited non-wall neighbours
    2. add the current cell to the stack
    3. nothing
    4. Make the chosen cell the current cell
    5. Recursively call this function
  3. else
    1. remove the last current cell from the stack
    2. Backtrack to the previous execution of this function
一念一轮回 2024-10-13 21:43:39

拆墙简单来说就是拆墙!您从一个单元网格开始,每个单元都完全被 4 堵墙包围。当你在(2.1)周围随机移动时,你移除了连接细胞的壁。

Removing the wall simply means removing the wall! You start with a grid of cells, each of which is totally surrounded by 4 walls. As you move randomly around (2.1) you remove the wall joining the cells.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文