b2KIT

Maze Generator

Generate printable mazes of various sizes and difficulty levels using recursive backtracking algorithms.

Tested tool guide Tested browser tools Checked August 16, 2026

What Maze Generator does, with a checked example

Pick a grid size and, if you want, a difficulty, and this tool draws a maze you can print and solve by hand. It carves the maze with recursive backtracking: starting from one cell, the generator randomly breaks a wall into an unvisited neighbor, backs up when it gets stuck, and repeats until every cell is reachable. The output is always a 'perfect' maze: exactly one solution path, no loops, no unreachable dead zones. The usual surprise is that harder settings do not add extra routes - the structure cannot contain them; difficulty comes from grid size and where the entrance and exit sit.

Worked example

A concrete input and expected output from the current implementation.

Input

Grid size: 5 columns by 5 rows

Expected output

A square maze of 25 cells: 24 walls carved open out of the 40 interior walls, 16 walls left standing, and exactly one solution path from the entrance to the exit.

Recursive backtracking removes exactly one wall per cell after the first, so open passages equal cells minus one (25 - 1 = 24). Because a passage never enters an already-visited cell, the routes form a tree, which guarantees exactly one solution path.

How the result is produced

1

The carve walk

The grid starts as rows of closed cells, each bounded by four walls. The generator begins at one cell, then repeatedly knocks a wall into a random neighbor that has not been visited yet, pushing each cell onto a stack. When the current cell has no unvisited neighbors, the walk pops backward until it reaches one that does. It stops once every cell is visited.

2

The perfect-maze guarantee

Because a passage is carved only into an unvisited cell, passages can never loop back: the result is a tree with exactly one route between any two cells. The count is fixed too: a W-by-H grid gets W times H minus 1 passages, so a 5-by-5 maze has 25 cells, 24 open passages, and 16 of its original 40 walls standing. What prints is the remaining walls, with gaps at the entrance and exit.

Good uses

  • Printing a batch of mazes for a classroom, birthday party, or waiting-room pile: small grids solve in under a minute, larger ones occupy a reader for a while.
  • Feeding test cases to a maze-solver program: every maze is guaranteed to have exactly one solution, so a solver that reports two routes, a loop, or an unreachable cell is demonstrably wrong.
  • Producing fresh puzzle sheets on demand - for a road trip, a restaurant, or a newsletter - in seconds, with nothing to install.

Limits and checks

  • Always one solution, never loops: a backtracking maze is structurally incapable of offering two viable routes. If you want braided mazes or multiple solutions, this algorithm - and this tool - will not produce them.
  • Settings do not reproduce: generation is randomized, so the same size and difficulty produce a different maze every time, and the solution cannot be predicted in advance. Two 'easy' mazes can also differ wildly in solution length, since the entrance and exit land wherever the random walk leaves them.
  • Grid numbers are squared: a 10-by-10 maze has 100 cells (99 passages) and a 20-by-20 has 400 cells (399 passages), so doubling the size multiplies the maze by four - 'just a little bigger' is a much larger puzzle than it sounds.

Common questions

Can I get a maze with loops or more than one solution?

No. Recursive backtracking only ever carves into unvisited cells, so the passages can never close a loop. Every maze it produces is a perfect maze: exactly one route connects any two points, and the entrance and exit have exactly one solution. A maze with loops would require a different generator.

My 'easy' maze took longer than a 'medium' one I printed earlier. Is the difficulty setting broken?

Not necessarily. Difficulty labels are the site's own, not a benchmark, and the generator is random: solving time is driven mostly by grid size and by where the entrance and exit happen to land, which changes with every maze. If you want reliably harder mazes, increase the grid size rather than trusting the label.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools