不可预知性是所有事件之源头
——某位哲学家
什么是Prim算法可以吃吗
引入问题
小明家后面的公园准备开张啦!公园建筑方为了美观,准备修建一个纯天然不添加的迷宫。已知迷宫为15*15,四围除出入口外都围了墙(如下图)
# | E | # | # | # | # | # | # | # | # | # | # | # | # | # |
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | # | |||||||||||||
# | @ | |||||||||||||
# | # | # | # | # | # | # | # | # | # | # | # | # | # | # |
迷宫说明:
#是墙
E是入口,@是出口
Q:现在请你生成一个随机的迷宫Start your SHOW
问题求解
先给个链接自己体验一下Prim迷宫算法的美丽
下面是关于Prim算法的解释
Prim迷宫算法的文本解释
English: 1.Start with a grid full of walls. 2.Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list. 3.While there are walls in the list: 1.Pick a random wall from the list. If only one of the two cells that the wall divides is visited, then: 2.Make the wall a passage and mark the unvisited cell as part of the maze. 3.Add the neighboring walls of the cell to the wall list. 2.Remove the wall from the list. 中文: 1.让迷宫全是墙. 2.选一个单元格作为迷宫的通路,然后把它的邻墙放入列表 3.当列表里还有墙时 1.从列表里随机选一个墙,如果这面墙分隔的两个单元格只有一个单元格被访问过 1.那就从列表里移除这面墙,即把墙打通,让未访问的单元格成为迷宫的通路 2.把这个格子的墙加入列表 2.如果墙两面的单元格都已经被访问过,那就从列表里移除这面墙 ———————————————— 出自 维基百科
看不懂?可视版解释来一波我打这么多字你确定不给我一个赞和关注吗
Prim迷宫算法的可视版解释(栗子)
我们现在看一个缩水版的迷宫因为我懒
1.让迷宫全是墙(新增墙$)
# | E | # | # | # | # | # |
# | $ | $ | # | |||
# | $ | $ | $ | $ | $ | # |
# | $ | $ | # | |||
# | $ | $ | $ | $ | $ | # |
# | $ | $ | @ | |||
# | # | # | # | # | # | # |
2.随机选择一个单元格(如下图%),此时放入列表的墙为&(红色为随机选中的单元格和墙,橙色为列表中的墙,荧光色为通路)
# | E | # | # | # | # | # |
# | % | & | $ | # | ||
# | & | $ | $ | $ | $ | # |
# | $ | $ | # | |||
# | $ | $ | $ | $ | $ | # |
# | $ | $ | @ | |||
# | # | # | # | # | # | # |
3.随机选择一个墙(&),若墙对边单元格未访问,打通
# | E | # | # | # | # | # |
# | % | & | $ | # | ||
# | 通 | $ | $ | $ | $ | # |
# | $ | $ | # | |||
# | $ | $ | $ | $ | $ | # |
# | $ | $ | @ | |||
# | # | # | # | # | # | # |
4.将打通后连接的单元格从第2步开始重复
# | E | # | # | # | # | # |
# | % | & | $ | # | ||
# | $ | $ | $ | $ | # | |
# | % | & | $ | # | ||
# | & | $ | $ | $ | $ | # |
# | $ | $ | @ | |||
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | & | $ | # | ||
# | $ | & | $ | $ | # | |
# | % | % | & | # | ||
# | & | $ | & | $ | $ | # |
# | $ | $ | @ | |||
# | # | # | # | # | # | # |
5.此时选中的墙左右都访问过了(下图),将其从列表中移除
# | E | # | # | # | # | # |
# | % | & | % | & | # | |
# | $ | $ | $ | # | ||
# | % | % | & | # | ||
# | & | $ | & | $ | $ | # |
# | $ | $ | @ | |||
# | # | # | # | # | # | # |
6.重复上述过程
# | E | # | # | # | # | # |
# | % | $ | % | & | # | |
# | $ | $ | $ | # | ||
# | % | % | & | # | ||
# | & | $ | & | $ | $ | # |
# | $ | $ | @ | |||
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | $ | % | & | # | |
# | $ | $ | $ | # | ||
# | % | % | & | # | ||
# | $ | & | $ | $ | # | |
# | % | & | $ | @ | ||
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | $ | % | & | # | |
# | $ | $ | & | # | ||
# | % | % | % | # | ||
# | $ | & | $ | & | # | |
# | % | & | $ | @ | ||
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | $ | % | & | # | |
# | $ | $ | & | # | ||
# | % | % | % | # | ||
# | $ | $ | & | # | ||
# | % | & | % | & | @ | |
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | $ | % | & | # | |
# | $ | $ | & | # | ||
# | % | % | % | # | ||
# | $ | $ | & | # | ||
# | % | & | % | % | @ | |
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | $ | % | & | # | |
# | $ | $ | & | # | ||
# | % | % | % | # | ||
# | $ | $ | $ | # | ||
# | % | & | % | % | @ | |
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | $ | % | & | # | |
# | $ | $ | & | # | ||
# | % | % | % | # | ||
# | $ | $ | $ | # | ||
# | % | $ | % | % | @ | |
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | $ | % | % | # | |
# | $ | $ | & | # | ||
# | % | % | % | # | ||
# | $ | $ | $ | # | ||
# | % | $ | % | % | @ | |
# | # | # | # | # | # | # |
# | E | # | # | # | # | # |
# | % | $ | % | % | # | |
# | $ | $ | $ | # | ||
# | % | % | % | # | ||
# | $ | $ | $ | # | ||
# | % | $ | % | % | @ | |
# | # | # | # | # | # | # |
迷宫生成结束,整理一下结果:
# | E | # | # | # | # | # |
# | $ | # | ||||
# | $ | $ | $ | # | ||
# | # | |||||
# | $ | $ | $ | # | ||
# | $ | @ | ||||
# | # | # | # | # | # | # |
如果有其他不解之处或有可改进之处,欢迎通过邮箱(870585593@qq.com)联系我!
感谢阅读!
如果可以的话点个赞 分个享 关注一下 呗?:)