Recursive Maze Generator

Turning a depth-first search into labyrinthine art.

Perfect Mazes

A perfect maze has exactly one path between any two cells — no loops, no isolated sections. Algorithms that generate such mazes must visit every cell while carving passages.

Depth-First Search Backtracker

  1. Start at a random cell and mark it visited.
  2. Randomly pick an unvisited neighbour, remove the wall between, push current cell onto a stack, and move to the neighbour.
  3. If no unvisited neighbours remain, pop a cell off the stack (back-tracking) and continue.
  4. Repeat until every cell has been visited.

The result is a snaking, bias-free maze with long corridors punctuated by bursts of branching where the algorithm back-tracked.

Implementation Details

Controls

Press Regenerate in the gallery to seed a fresh maze with a new random starting point.

← Back to gallery