Four kinds of maze generation algorithms

Source: Internet
Author: User

Introduction

The so-called Maze generation algorithm is the algorithm used to generate a random maze.

The Maze generation algorithm is in such a scenario:

    • A row row, a grid map of Col columns, the first default all the walls around the grid are closed

    • Requires a 2 wall on the edge of the grid map, which is the edge of the grid.

    • All grids are guaranteed at least one wall around the grid to get through

    • All grids can form a pathway through a wall that can be opened.

Bo Master has implemented recursivebacktracking (recursive backtracking), recursivesegmentation (recursive segmentation), random prim algorithm, kruskal+ and four kinds of maze generation algorithm, this article mainly on these four kinds of algorithms are briefly introduced

code implementation of Maze generation algorithm based on Unity

GitHub links

Recursive backtracking algorithmComplexity of
  Space: O (n), Time: O (n), n is the number of maze nodes Row*col
principle

A stack is used as an auxiliary data structure to record the order of the open areas for backtracking

Start by randomly selecting an area in the map to join the stack.

Then randomly select an open area around the previously selected area, connect the newly selected area to the wall of the previously selected area, and add the new area to the stack

If the surrounding area is open, let the stack out of the stack, will be selected by the current period of the new stack top, indicating fallback to the previous area

Then recursively follow the previous steps to select the next area until all areas are opened.

Disadvantages

This algorithm is very simple to realize the idea, but the path is too obvious, even there may appear in the maze, very embarrassed!!!

Recursive segmentation algorithmComplexity of
  Recursive space complexity: O (Row * col), best time complexity: O (Lgrow + lgcol), Worst time complexity o (row * col)
principle

Divide the rectangle map into four small matrices with a cross

A wall is randomly opened on each of the four faces adjacent to the four small rectangles, making the pathway conduction.

Then repeat the previous split operation on all small rectangles

If the matrix is already unable to be divided, that is, the number of rows or columns for a moment, the matrix inside the walls are all open

Pros and cons

The resulting maze has a distinctly rectangular composition, unnatural, suitable for simple games such as Fps,act

Stochastic prim algorithmComplexity of
  
principle

Randomly select an area as the current region

Add a wall that has not been opened in the region for four weeks to the list

While (the list is not empty)

  Randomly select a wall from the list
?
If there is an area on either side of the wall that has not been opened, open the wall and join the list of walls near the wall
?
Remove the wall from the list
Pros and cons

The maze of nature, difficult to walk, suitable for real maze games

kruskal+ and check set

Complexity of
  
principle

This algorithm skillfully uses and checks the set, uses and sets up to judge the entire maze the conduction question

Add all the walls to the wall list

while (wall list is not empty)

  Randomly select a wall from the wall list to determine whether the area on both sides of the wall is on or off
?
Get through this wall without a lead.
?
Remove the wall from the wall list
Reference Links

Three great maze generation algorithms

Random Maze Generation algorithm (and search set + spanning tree)

Four kinds of maze generation algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.