# 迷宫生成算法

迷宫生成算法是一种在由墙壁围成的单元格网格中开辟通道的流程，使得每个单元格都相互连通、不存在环路，且任意两个单元格之间恰好有一条路径相连。用图论的术语来说，其结果就是网格图的一棵生成树，而每种算法都是采样或构造这样一棵树的不同方式，并在最终生成的迷宫中留下可辨识的纹理特征。<sup>[1](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)</sup><sup> • </sup><sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup>

| 关键事实 | 详情 |
|---|---|
| 目标结构 | 完美迷宫是网格图的一棵生成树：连通、无环，开凿的通道数恰好比单元格数少一<sup>[1](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)</sup> |
| 递归回溯法 | 深度优先行走，边走边开凿，遇到死胡同时回溯；走廊长、分支少、解法路径长而曲折<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup> |
| 随机化普里姆算法 | 通过随机打开边界墙来扩展一个区域；短分支和死胡同多，解法路径短<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup> |
| 随机化克鲁斯卡尔算法 | 打乱所有墙壁的顺序，打开那些连接两个独立区域的墙，用并查集追踪；纹理均匀，没有原点感<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup><sup> • </sup><sup>[1](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)</sup> |
| 均匀采样器 | Aldous-Broder 算法和威尔逊算法通过随机游走生成均匀生成树；威尔逊方法（1996年）通常比 Aldous-Broder 快得多<sup>[3](https://www.cs.cmu.edu/~15859n/RelatedWork/RandomTrees-Wilson.pdf)</sup> |
| 速度 | 常见的树生长算法以单元格数量的线性时间运行；150×150 的迷宫在不到一秒内即可生成<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup> |

## 迷宫作为生成树

所有方法都从相同的设置开始：一个网格，其中每个单元格都被墙壁围住，将其视为一个图，节点是单元格，边是相邻单元格之间的共享墙壁。开凿通道就是删除一条边墙；当开放的通道形成一棵生成树时，迷宫被称为完美迷宫，即连通、无环、可解，任意两个单元格之间恰好有一条路径。<sup>[1](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)</sup><sup> • </sup><sup>[4](https://www.teachme.sh/pathfinding/maze-generation)</sup> n 个单元格上的生成树总是恰好有 n − 1 条边，因此完美迷宫保留 n − 1 条通道，其余所有潜在连接都被墙壁封死。<sup>[5](https://www.mysimulator.uk/articles/maze-algorithms-spanning-trees/)</sup>

这种等价关系将迷宫设计转化为一个定义明确的算法问题：构建网格的一棵生成树，而算法的选择决定了倾向于产生哪棵生成树，从而决定哪种视觉纹理。<sup>[4](https://www.teachme.sh/pathfinding/maze-generation)</sup><sup> • </sup><sup>[5](https://www.mysimulator.uk/articles/maze-algorithms-spanning-trees/)</sup>

## 树生长算法

**递归回溯法。** 这是一种深度优先行走。从随机单元格开始，算法随机走向一个未访问的邻居，边走边开凿，当遇到死胡同时沿路径回退，从最后一个仍有未访问邻居的单元格继续。结果具有长而不间断的走廊、相对较少的分叉点，以及一条长而曲折的解法路径；这是经典的手绘迷宫外观。<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup><sup> • </sup><sup>[1](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)</sup>

**随机化普里姆算法。** 从一个单元格开始，维护一个与生长区域相邻的墙壁列表。从列表中随机选择一堵墙；如果墙另一侧的单元格尚未被访问，则移除该墙，然后将新到达的单元格的墙壁追加到列表中。由于生长发生在边界的任意位置，迷宫从原点向各个方向展开，产生许多短分支、许多死胡同和较短的解法路径。<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup><sup> • </sup><sup>[1](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)</sup>

**随机化克鲁斯卡尔算法。** 编制网格中所有墙壁的枚举列表，随机化其顺序，然后依次处理，推倒每一堵连接两个尚未处于同一区域的单元格的墙，同时用并查集结构追踪每个单元格属于哪个区域。这是经典的[最小生成树](https://www.edgepedia.org/zh/minimum-spanning-tree)算法应用于带随机权重的网格。克鲁斯卡尔迷宫具有均匀的纹理，没有原点感，分支长度适中，死胡同常见；由于洗牌是全局的，其结果在常见方法中最为均匀随机。<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup><sup> • </sup><sup>[1](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)</sup>

**猎杀法。** 与回溯法类似，它从一个单元格走到另一个单元格，直到卡住。它不回退，而是扫描网格，找到第一个与已访问单元格相邻的未访问单元格，将其连接，然后从那里开始新的行走。这种扫描给迷宫带来一种淡淡的纹理，因为新走廊倾向于从网格的某个区域开始；与回溯法一样，它产生较长的直线段，但改变方向的频率更高。<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup>

**二叉树法和响尾蛇法。** 这些是最简单的遍历方法。[二叉树](https://www.edgepedia.org/zh/binary-tree)法访问每个单元格并随机向北或向东开凿，留下两条完全开放的边界边。响尾蛇法逐行行走，每段以单个向北的门结束。两者都很快，且偏向顶部和右侧，这种可见的偏差使它们易于教学。<sup>[1](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)</sup> 一项比较研究同样指出，二叉树法和响尾蛇法产生沿边缘延伸的细长走廊。<sup>[6](https://www.mdpi.com/1999-4893/17/9/404)</sup>

## 均匀生成树算法

上述算法产生的迷宫具有特征性偏差。另一类算法从所有可能的生成树中均匀采样，因此不偏向任何纹理。

**Aldous-Broder 算法。** 选择一个任意顶点作为起点，在图中进行无限制随机游走；每当一个顶点首次被访问时，记录游走到达它的那条边。当所有顶点都被发现时，标记的边形成一棵具有正确均匀概率的随机生成树。该方法易于编码，运行时间常数小，但游走必须覆盖整个图，因此其运行时间与图的覆盖时间相关。<sup>[3](https://www.cs.cmu.edu/~15859n/RelatedWork/RandomTrees-Wilson.pdf)</sup>

**威尔逊算法。** [大卫·布鲁斯·威尔逊](https://www.edgepedia.org/zh/david-bruce-wilson)在 1996 年 STOC 论文中介绍了一种基于环路消除随机游走的方法，同样产生均匀生成树，但可以在覆盖时间之前完成；其期望运行时间与图的平均命中时间成正比。在两种算法都适用的图上，威尔逊算法的运行时间绝不会超过 Aldous-Broder 的两倍，而且通常快得多。<sup>[3](https://www.cs.cmu.edu/~15859n/RelatedWork/RandomTrees-Wilson.pdf)</sup><sup> • </sup><sup>[7](https://dblp.dagstuhl.de/rec/conf/stoc/Wilson96.html)</sup> 两种算法的行为互补：Aldous-Broder 快速添加最初的顶点，但在覆盖最后几个顶点之前会游荡；而威尔逊快速添加最后的顶点，但在找到第一个锚点之前可能会游荡。<sup>[8](https://arxiv.org/abs/2206.12378)</sup>

**混合方法。** 努内斯、亚科贝利和菲格雷多在 2022 年证明，在完全图上，两种算法在特定停止时间构建的部分树在统计上是等价的，这允许一种从 Aldous-Broder 开始然后切换到威尔逊的混合方法。他们的两阶段框架生成传递图的均匀生成树所需时间比单独使用威尔逊方法少约 25%。<sup>[8](https://arxiv.org/abs/2206.12378)</sup>

## 纹理、偏差与算法选择

算法的选择主要是纹理的选择。四种常见方法的并排比较总结了差异：<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup>

| 算法 | 走廊 | 死胡同 | 解法路径 | 感觉 |
|---|---|---|---|---|
| 回溯法 | 长 | 少 | 长 | 经典、曲折 |
| 普里姆 | 短 | 多 | 短 | 分支多、繁忙 |
| 克鲁斯卡尔 | 中 | 多 | 中 | 均匀、随机 |
| 猎杀法 | 长 | 一些 | 中 | 有反向的连续段 |

偏差是一个反复出现的主题。应用于迷宫的最小生成树算法偏向于短解法路径和许多死胡同，因为连接两点的第一条无环路径会胜出，而更长的路径需要更多步骤才能出现。<sup>[9](https://courses.grainger.illinois.edu/cs340/fa2025/mp/mazegen.html)</sup> 一篇俄亥俄州立大学的论文分析了由递归回溯法、[普里姆算法](https://www.edgepedia.org/zh/prims-algorithm)和[克鲁斯卡尔算法](https://www.edgepedia.org/zh/kruskals-algorithm)采样的迷宫，发现每种方法都存在某些方面的偏差。<sup>[10](https://etd.ohiolink.edu/acprod/odb_etd/ws/send_file/send?accession=osu1563286393237089&disposition=inline)</sup> 只有均匀采样器完全避免了这种结构。

2025 年一项面向游戏开发的分类研究将迷宫生成算法分为四个拓扑家族：理想型（完美）、非理想型、洞穴型和混合型，并将每个家族与类型需求、技术限制和玩家期望相匹配。<sup>[11](https://dspace.onua.edu.ua/items/07507b27-0229-4220-95e5-f23cb0c226f0)</sup> 这属于更广泛的过程化内容生成领域，即使用算法自动创建游戏内容，自 1980 年代 Roguelike 游戏以来，迷宫一直是其中的主要内容。<sup>[12](https://arxiv.org/abs/2410.15644)</sup>

## 性能

所有这些树生长方法所需时间与单元格数量成正比，因此 150×150 单元格的迷宫几乎可以瞬间生成。<sup>[2](https://www.mazegenerator.org/guides/maze-algorithms-compared)</sup> 均匀采样器是例外：其成本取决于随机游走量，如图的覆盖时间和平均命中时间，这就是为什么更快的生成树采样一直是一个活跃的研究方向，已有算法能在期望时间 \( \tilde{O}(m^{4/3}) \) 内从具有 m 条边的图中采样生成树。<sup>[3](https://www.cs.cmu.edu/~15859n/RelatedWork/RandomTrees-Wilson.pdf)</sup><sup> • </sup><sup>[13](https://arxiv.org/abs/1501.00267)</sup>

## References

1. [Maze generation algorithms, explained, Boneyard Tools](https://boneyardtools.com/math/maze-generator/maze-generation-algorithms-explained)
2. [Backtracker, Prim, Kruskal or hunt-and-kill: which maze algorithm is best?, Maze Generator](https://www.mazegenerator.org/guides/maze-algorithms-compared)
3. [David Bruce Wilson (1996). Generating Random Spanning Trees More Quickly than the Cover Time](https://www.cs.cmu.edu/~15859n/RelatedWork/RandomTrees-Wilson.pdf)
4. [Generating Mazes, Teach Me Pathfinding](https://www.teachme.sh/pathfinding/maze-generation)
5. [Maze Generation Algorithms: Spanning Trees and Graph Theory, mysimulator](https://www.mysimulator.uk/articles/maze-algorithms-spanning-trees/)
6. [A Comparative Study of Maze Generation Algorithms in a Game-Based Mobile Learning Application, Algorithms (MDPI)](https://www.mdpi.com/1999-4893/17/9/404)
7. [dblp record: Generating Random Spanning Trees More Quickly than the Cover Time, STOC 1996](https://dblp.dagstuhl.de/rec/conf/stoc/Wilson96.html)
8. [Nunes, Iacobelli, Figueiredo (2022). A transient equivalence between Aldous-Broder and Wilson's algorithms, arXiv](https://arxiv.org/abs/2206.12378)
9. [Maze Creation, CS 340, University of Illinois](https://courses.grainger.illinois.edu/cs340/fa2025/mp/mazegen.html)
10. [Chapter 12: Maze Generation Method with Choices of Algorithm, Ohio State University thesis](https://etd.ohiolink.edu/acprod/odb_etd/ws/send_file/send?accession=osu1563286393237089&disposition=inline)
11. [Trofymenko et al. (2025). Classification of algorithms for procedural generation of labyrinths for game development](https://dspace.onua.edu.ua/items/07507b27-0229-4220-95e5-f23cb0c226f0)
12. [Procedural Content Generation in Games: A Survey with Insights on Emerging LLM Integration, arXiv](https://arxiv.org/abs/2410.15644)
13. [Fast Generation of Random Spanning Trees and the Effective Resistance Metric, arXiv](https://arxiv.org/abs/1501.00267)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Graph and network algorithms › Spanning trees and graph connectivity structures

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/maze-generation-algorithm

English original: https://www.edgepedia.org/maze-generation-algorithm
