Skip to main content

Recursive Backtracking

<time datetime="2023-06-06 00:08:25 &#43;0530 IST">6 June 2023</time><span class="px-2 text-primary-500">&middot;</span><span>291 words</span><span class="px-2 text-primary-500">&middot;</span><span title="Reading time">2 mins</span><span class="px-2 text-primary-500">&middot;</span> <span class="mb-[2px]"> <a href="#/blog/MazeGeneration/index.md" class="text-lg hover:text-primary-500" rel="noopener noreferrer" target="_blank" title="Edit content" ><span class="relative inline-block align-text-bottom px-1 icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M490.3 40.4C512.2 62.27 512.2 97.73 490.3 119.6L460.3 149.7L362.3 51.72L392.4 21.66C414.3-.2135 449.7-.2135 471.6 21.66L490.3 40.4zM172.4 241.7L339.7 74.34L437.7 172.3L270.3 339.6C264.2 345.8 256.7 350.4 248.4 353.2L159.6 382.8C150.1 385.6 141.5 383.4 135 376.1C128.6 370.5 126.4 361 129.2 352.4L158.8 263.6C161.6 255.3 166.2 247.8 172.4 241.7V241.7zM192 63.1C209.7 63.1 224 78.33 224 95.1C224 113.7 209.7 127.1 192 127.1H96C78.33 127.1 64 142.3 64 159.1V416C64 433.7 78.33 448 96 448H352C369.7 448 384 433.7 384 416V319.1C384 302.3 398.3 287.1 416 287.1C433.7 287.1 448 302.3 448 319.1V416C448 469 405 512 352 512H96C42.98 512 0 469 0 416V159.1C0 106.1 42.98 63.1 96 63.1H192z"/></svg> </span></a > </span>

Recursive Backtracking Algorithm #

GIF

Basic Idea #

This is a simple depth-first search Algorithm, which can generate mazes by carving walls between adjacent cells randomly, and Backtracking when it reaches a dead end.

Making the grid #

The grid was the easiest part, a 1D list that contained a Cell object

grid = []
for i in range(ROWS):
    for j in range(ROWS):
        grid.append(Cell(i, j, gridSize))

The cell class just has an i and j and a size.

The Algorithm #

Maze
Recursive Backtracking Psuedocode

The Algorithm is pretty simple to implement.

For Step 2:

Step 1 #

A function is defined for checking neighbors, which just checks if the cell and exists and if it has been visited

# For the top cell
try:
    top = grid[index(i, j-1)]
except:
    top = None

# Check
if top and not top.visited:
    neighbors.append(top)

# Do this for all neighbors

# Return random neighbor
if len(neighbors) > 0:
    return random.choice(neighbors)
else:
    return None

# In the main loop
new = check_neighbors(current.i, current.j)
    if new:
    new.visited = True

Step 2 #

Define a stack at the top, and append to it (Just a list in python)

stack = []
# After setting new to visited
stack.append(new)

Step 3 #

This step removes walls from the carving direction For example, the left of current wall and right of new wall is removed, if the current is to the right of the new one.

x = current.i - new.i
if x == 1:
current.walls[3] = False
new.walls[1] = False

Step 4 #

Set this as current

current = new

If stack not empty #

elif len(stack) > 0:
    current = stack.pop(-1)

Conclusion #

This works easily to build a perfect maze, and is pretty fast

The repository is here