简体   繁体   English

如何生成具有固定入口和出口点的迷宫?

[英]How to generate mazes with fixed entry and exit points?

I have read about the Depth-First search algorithm to create and solve mazes. 我已经阅读了有关创建和解决迷宫的深度优先搜索算法的信息。 However, I have not found anything on creating mazes with fixed entry and exit. 但是,在创建具有固定入口和出口的迷宫时,我没有发现任何东西。 On each maze the entry would always be at (0, 1) and the exit at the opposite side on both axis. 在每个迷宫中,入口始终位于(0,1),出口始终位于两个轴的相对侧。

During the generation of the maze, every cell should be visited (to generate as many dead-ends as possible) but the the exit should always be at the same point. 在迷宫的生成过程中,应访问每个单元(以生成尽可能多的死角),但出口应始终位于同一点。

The solution path to the resulting mazes would look like this: 导致迷宫的解决方案路径如下所示:

在此处输入图片说明

Or this: 或这个:

在此处输入图片说明

I've made grid-based mazes before using a breadth-first search, but a similar algorithm can be devised from a depth first one. 在使用广度优先搜索之前,我已经制作了基于网格的迷宫,但是可以从深度优先的角度设计出类似的算法。

First I'd create a graph , where each node in your coordinate graph above links to the node up, down, left, and right of it. 首先,我将创建一个 ,其中坐标图上方的每个节点都链接到该节点的上,下,左和右。 For example, node (1, 1) has an edge to (0, 1), (1, 0), (2, 1), and (1, 2). 例如,节点(1,1)的边缘为(0,1),(1,0),(2,1)和(1,2)。 Edge nodes will only have 3 edges, and corner ones will only have 2, since the neighbors in the appropriate directions don't exist. 边缘节点仅具有3条边缘,而角节点仅具有2条边缘,因为在适当方向上的邻居不存在。 While you are generating these edges, assign each one a random weight. 在生成这些边缘时,为每个边缘分配一个随机权重。 When I implemented it I found that a range of [0, 100) worked well, but you can tweak that. 当我实现它时,我发现[0,100)的范围很好,但是您可以对其进行调整。 Then finally you can do a Depth First Search from your desired start node to the desired end one, and just trace out the path as you go. 最后,您可以从所需的起始节点到所需的结束节点进行深度优先搜索,然后随便查找路径。 If you recurse down an edge that would connect you to a node you've already visited, don't draw the edge there. 如果递归沿着将您连接到已经访问过的节点的边缘,请不要在此处绘制边缘。 That'll get you something that looks maze-like. 那会给你一些看起来像迷宫的东西。

When I did it, I actually set up the graph in the same way, but instead of DFS I used Prim's Algorithm to calculate the minimum spanning tree of that graph. 完成此操作后,我实际上以相同的方式设置了图形,但是我使用了Prim的算法来代替DFS来计算该图形的最小生成树。 This gave me something that looked maze-like, touched every node at least once, and contained no cycles. 这给了我一种看起来像迷宫的东西,至少触摸了每个节点一次,并且没有循环。 Then I could assign any point I wanted to be the start and end point, and the maze would contain no cycles, and exactly 1 shortest path between any 2 points. 然后,我可以指定任何我想作为起点和终点的点,迷宫将不包含循环,并且在任意2个点之间恰好有1条最短路径。 I added tools on top of that for editing the maze, removing dead ends, rendering it in 3D, etc, but those are beyond the scope of your question. 我在此之上添加了一些工具,用于编辑迷宫,消除死角,以3D渲染等,但这超出了您的问题范围。

If you want to see how I did it, check out my project on GitHub. 如果您想了解我的工作方式, 在GitHub上查看我的项目 The "Minotaur" folder contains the executable (a Frankenstein's monster of Python, C++, and C#), and the source is in there too. “ Minotaur”文件夹包含可执行文件(弗兰肯斯坦的怪兽,Python,C ++和C#),源代码也位于其中。 The maze generation part is in this file . 迷宫生成部分在此文件中

I know you asked for this in Python but I'm too busy reverse engineer my C++ code right now, I hope you still find this answer helpful. 我知道您在Python中要求这样做,但是我现在正忙于对我的C ++代码进行逆向工程,希望您对此答案仍然有所帮助。

Edit: I nearly forgot I made a fancy video showing it off, so if you want to see it in action but don't want to compile my source or don't trust my executable, you can view the project on my portfolio . 编辑:我几乎忘了我制作了一个炫目的视频,所以如果您想观看它,但又不想编译我的源代码或不信任我的可执行文件,则可以在我的投资组合中查看该项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM