A C++ implementation of Wilson's algorithm for generating uniform spanning tree mazes.
Wilson's algorithm uses loop-erased random walks to generate a maze that is a uniform spanning tree — meaning every possible maze is equally likely to be generated.
- Mark a random cell as visited
- Pick any unvisited cell and start a random walk
- If the walk revisits a cell, erase the loop
- When the walk reaches a visited cell, carve the path into the maze
- Repeat until all cells are visited
mkdir build && cd build
cmake ..
make./wilson-maze # default 20x10
./wilson-maze 40 20 # custom size+---+---+---+---+---+
| | |
+ +---+---+ + +
| | | |
+ + + +---+ +
| | |
+---+---+---+---+---+
MIT