Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

A simple maze generation algorithm

Sharing is caring!

Knowing how to create a maze is as essential as breathing. So here is a simple algorithm to create mazes along with a link to a GitHub repository for its implementation in Swift.

maze-circular

Algorithm
1. Start at cell (1, 1).
2. Find a neighbor at random that you haven’t yet visited.
3. If you find one, move there, removing the wall between the current cell and the chosen cell. If you don’t find one, go back to the previous cell.
4. Repeat steps 2. and 3. until you’ve been to every cell in the grid.

Full code
https://github.com/lucaslouca/swift-mazes

References
http://algs4.cs.princeton.edu/41graph/