Question

Say you want a simple maze on an N by M grid, with one path through, and a good number of dead ends, but that looks "right" (i.e. like someone made it by hand without too many little tiny dead ends and all that). Is there a known way to do this?

Was it helpful?

Solution

From http://www.astrolog.org/labyrnth/algrithm.htm

Recursive backtracker: This is somewhat related to the recursive backtracker solving method described below, and requires stack up to the size of the Maze. When carving, be as greedy as possible, and always carve into an unmade section if one is next to the current cell. Each time you move to a new cell, push the former cell on the stack. If there are no unmade cells next to the current position, pop the stack to the previous position. The Maze is done when you pop everything off the stack. This algorithm results in Mazes with about as high a "river" factor as possible, with fewer but longer dead ends, and usually a very long and twisty solution. It runs quite fast, although Prim's algorithm is a bit faster. Recursive backtracking doesn't work as a wall adder, because doing so tends to result in a solution path that follows the outside edge, where the entire interior of the Maze is attached to the boundary by a single stem.

They produce only 10% dead ends

is an example of a maze generated by that method.

OTHER TIPS

It turns out there are 12 classic algorithms to generate "perfect" mazes. A maze is perfect if it has one, and only one, solution. Here are some links to each algorithm, in rough order of my preference.

  1. Kruskal's
  2. Prim's
  3. Recursive Backtracker
  4. Aldous-Broder
  5. Growing Tree
  6. Hunt-and-Kill
  7. Wilson's
  8. Eller's
  9. Cellular Automaton (Easy)
  10. Recursive Division (Very Easy)
  11. Sidewinder (Predictable)
  12. Binary Tree (Flawed)

For more info, check out mazelib on GitHub, a Python library implementing all the standard maze generating/solving algorithms.

A pretty straightforward solution could be to assign random weights to the graph edges and apply Kruskal's algorithm to find a minimum spanning tree.

Best discussion ever on maze generation algorithms: http://www.jamisbuck.org/presentations/rubyconf2011/index.html (was on HN a couple days ago).

Strangely enough, by slightly changing the 'canonical' rules and starting from a random configuration, Conway's Game of Life seems to generate pretty nice mazes!

(I don't remember the exact rule, but it's a very simple modification that tends to 'densify' the population of cells...)

My favorite way is to use Kruskal's algorithm, but when randomly choosing and edge to remove, weight the choice based on the types of edges it's connected to.

By varying the weights for different edge types, you can generate mazes with lots of distinct characteristics or "personalities". See my example here:

https://mtimmerm.github.io/webStuff/maze.html

One of the methods to generate a maze is the randomized version of Prim's algorithm.

Start with a grid full of walls. Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list. While there are walls in the list:

Pick a random wall from the list. If the cell on the opposite side isn't in the maze yet:

(i) Make the wall a passage and mark the cell on the opposite side as part of the maze.

(ii) Add the neighboring walls of the cell to the wall list.

If the cell on the opposite side already was in the maze, remove the wall from the list.

For more understanding click here

Here's the DFS algorithm written as pseudocode:

create a CellStack (LIFO) to hold a list of cell locations
set TotalCells = number of cells in grid
choose a cell at random and call it CurrentCell
set VisitedCells = 1

while VisitedCells < TotalCells find all neighbors of CurrentCell with all walls intact
if one or more found choose one at random
knock down the wall between it and CurrentCell
push CurrentCell location on the CellStack
make the new cell CurrentCell
add 1 to VisitedCells else pop the most recent cell entry off the CellStack
make it CurrentCell endIf endWhile

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top