What's a good algorithm to generate a maze?

后端 未结 9 1413
囚心锁ツ
囚心锁ツ 2020-11-27 09:55

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

相关标签:
9条回答
  • 2020-11-27 10:12

    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.

    0 讨论(0)
  • 2020-11-27 10:12

    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).

    0 讨论(0)
  • 2020-11-27 10:16

    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

    0 讨论(0)
  • 2020-11-27 10:19

    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

    0 讨论(0)
  • 2020-11-27 10:22

    I prefer a version of the Recursive Division algorithm. It is described in detail here.

    I will give a quick overview:
    The original recursive division algorithm works as follows. First, start with an empty area for the maze. Add one straight wall to divide the chamber in two, and put one hole in that wall somewhere. Then, recursively repeat this process on each of the two new chambers until the desired passage size is reached. This is simple and works well, but there are obvious bottlenecks which make the maze easy to solve.

    The variant solves this problem by drawing randomized, "curved" walls rather than straight ones, making the bottlenecks less obvious.

    0 讨论(0)
  • 2020-11-27 10:24

    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...)

    0 讨论(0)
提交回复
热议问题