Algorithm for maze generation with no dead ends?

In the example you give there is only one actual path from start to end. If that is all that you want, I'm thinking you could use random walks!

The concept is simple: given the outer boundaries of the maze, a start point, and an end point, write a function to generate random walks from the start point that eventually end at the end point. The conditions would be that our "random walker" can only move up, down, right, or left from the previous square, and cannot come within one square of a previously traversed square (this creates walls).

As I see it, there are two algorithmic challenges here. The first is ascertaining whether we are within one square of a previously traversed square (collisions). Perhaps we could maintain a list of traversed squares (their coordinates) and maze boundaries, and for each new square evaluate distance from every square in the list. This doesn't sound very efficient though.

The other challenge is actually reaching the end point with our random walk. If collisions with previously traversed squares were not an issue, we would be bound to reach our end point eventually, but with them we have the problem that we could wall ourselves off from the end point. The way to avoid this is to check for and avoid the entering of loops. If we avoid entering loops formed by the traversed path and/or the maze boundaries, then we maintain a possible path to the end point. As far as actually figuring if we are in a loop... Meh that's kind of hard.

If you already have a maze-solving algorithm, you could run it whenever you have a possible collision to see if a path exists from your current square to the endpoint. When you run it, have it think that all previously traversed squares are walls, as well as their boundaries.


I think I've found another method, however I've not yet tested it extensively.

See https://twitter.com/tdhooper/status/340853820584230915/photo/1

From left to right:

  • Generate a non-unicursal maze as described here https://en.wikipedia.org/wiki/File:Prim_Maze.svg, I think this is Prim's algorithm

  • Seal up the exit

  • Draw a path that visits every point in the maze (ie. try to solve it)

  • Make this path a wall


I would sugest to start with completely black (full) square and try to dig the path. During the digging you easily ensure there are no dead-ends, simply keep going. Use backtracking, depth first search algorithm. Do a "random walk" - in each step, randomly decide whether to keep direction or change it. Check for the dead end condition - if you get stuck, you can either say "well, I'm done, I'm in the finish", or, if you consider the maze not yet digged enough, just backtrack. Always remember what you've done before and try randomly some other action. Probably use some heuristic to prefer certain directions, like, say, always keeping some free space before dodging the wall, try first to walk around walls etc. - this way you could find the desired solution which fills all the square much more quickly.


It sounds like you want a pseudo-random space filling curve (for example, see Context-based Space Filling Curves -EUROGRAPHICS ’2000 (PDF format, 1.1 MB))

Take a look a Space-filling curve.

I suspect you could apply some randomness to the construction of one of these to achieve what you want.

Tags:

Algorithm

Maze