迷宫问题和递归backtracker算法

编程入门 行业动态 更新时间:2024-10-11 23:27:23
本文介绍了迷宫问题和递归backtracker算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要实现的递归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.

    更多推荐

    迷宫问题和递归backtracker算法

    本文发布于:2023-11-30 14:29:38,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1650328.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:递归   迷宫   算法   backtracker

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!