How a Maze Generator Builds a Perfect Maze
After reading this you will understand how a randomized depth-first carve turns a blank grid into a printable maze with exactly one solution, how to size a maze for your audience, and how to read the solution overlay.
What a maze generator actually does
Start with a grid of cells, every cell walled off from its neighbors. A maze generator knocks down walls in a controlled pattern until every cell is reachable and no loops exist. The result is called a perfect maze: between any two cells there is exactly one path, no more and no less.
Consider a small 4 x 4 grid. That is 16 cells. To connect 16 cells with no loops you need exactly 15 walls removed, one fewer than the number of cells. This is not a coincidence. It is the defining property of a tree in graph theory, and every perfect maze is a spanning tree of its grid. Hold onto that number 15, because it drives everything below.
The generator on this page carves that tree with a single walk, then places the entrance and exit on opposite corners and, if you ask, traces the one path that joins them.
When to reach for it, and when not
A printable perfect maze suits activity sheets, party handouts, classroom warm-ups, and worksheet corners. A 10 x 10 maze prints crisp on a single page and takes a child two to four minutes. A 30 x 30 maze fills a page densely and can hold an adult for ten minutes or more.
It is the wrong tool when you want branching logic puzzles with keys and doors, mazes with multiple valid routes, or picture-shaped puzzles. A perfect maze has one route by construction, so it cannot offer "find the shortest of several paths" style play. If your goal is a numbered drawing rather than a maze, the Dot-to-Dot Maker fits better.
For a group of mixed ages, print two sizes of the same style. A 8 x 8 for younger solvers and a 20 x 20 for older ones keeps everyone busy for about the same length of time.
The math behind a perfect maze
Model the grid as a graph. Each cell is a node. Each pair of adjacent cells shares a potential edge (a wall you could remove). A perfect maze keeps exactly enough edges to connect every node once.
Here N is the number of cells and E is the number of removed walls (the carved passages). For a rectangular grid of w columns and h rows, N = w \cdot h. A 15 x 20 maze has N = 300 cells and therefore E = 299 carved passages, no matter how the randomness falls.
The total number of walls you could have carved is larger. In a w \times h grid the count of internal adjacent pairs is:
For 15 x 20 that is 15 \cdot 19 + 20 \cdot 14 = 285 + 280 = 565 possible passages. The maze keeps 299 of them and leaves 266 walls standing. The ratio of kept passages to total, 299/565 \approx 0.529, stays near one half for any large grid, which is why every perfect maze looks about equally dense.
How the randomized depth-first carve works
The generator uses one long, greedy walk. The steps are simple enough to trace by hand.
- Mark a start cell as visited and stand on it.
- Look at the neighbors you have not visited. If at least one exists, pick one at random, remove the wall between, step into it, mark it visited.
- If no unvisited neighbor exists, back up (pop the stack) to the last cell that still has an option.
- Repeat until you have backed all the way out. Every cell is now visited and connected.
Because the walk never revisits a cell, it never creates a loop. Because it does not stop until every cell is visited, it never leaves an island. Those two facts together guarantee the spanning tree, and therefore the unique solution.
The long corridors you see are the far reaches of the walk before it turned back. The dead ends are where it hit a wall of visited cells and backed up. Depth-first carving tends to produce long, winding passages and relatively few short dead ends, which is why these mazes feel "twisty" rather than "grid-like."
Reproducing the demo defaults
The demo button uses the tool defaults: a medium grid, square cells, entrance and exit on opposite corners, solution overlay off. Take the default as a 15 x 15 square for a concrete count.
- Cells: N = 15 \times 15 = 225.
- Carved passages: E = N - 1 = 224.
- Possible passages: W = 15 \cdot 14 + 15 \cdot 14 = 210 + 210 = 420.
- Walls left standing: 420 - 224 = 196.
- Entrance at the top-left corner, exit at the bottom-right corner. The straight-line distance across the grid is 14 cells horizontally and 14 vertically, so the shortest possible route would be 28 steps. The actual solution is longer because the single path must wind.
Every time you press the demo button you get a different maze, but all of them share those counts: 224 passages, 196 walls. Only the arrangement changes.
Reading the maze and the solution overlay
When you turn the solution on, the tool draws the single path from entrance to exit. Because the maze is a tree, there is nothing to choose: that path is forced. The overlay simply highlights it.
The solution length is worth understanding. On a perfect maze the path from one corner to the other visits far more than the 28 cells of the straight-line minimum. In practice the solution on a square grid of side n tends to run somewhere between 2n and 6n cells, with the average near 3n to 4n because depth-first carving favors long corridors. The chart below shows a typical trend measured over many carves.
Common mistakes and how to avoid them
The errors below come up again and again when people print maze sheets.
- Sizing for the page, not the solver
- A
40 x 40maze looks impressive but each cell shrinks to a few millimeters on A4. Lines blur and the path becomes hard to follow. For crayon-age children keep cells at least 8 mm wide, which caps a single page near20 x 20. - Printing the solution by accident
- If the overlay is on, the puzzle ships with the answer drawn over it. Print the blank version for solvers and keep the overlay copy for yourself.
- Expecting multiple routes
- A perfect maze has exactly one path between any two points. "Find the fastest way through" has only one answer here. If you want choices, this is the wrong maze type.
- Scaling PNG after export
- Enlarging a small PNG blurs the walls. Export SVG when you plan to resize, since it stays sharp at any scale.
Do not judge difficulty by wall count alone. Two 20 x 20 mazes both have 399 passages, yet one may have a 60-cell solution and another a 90-cell solution depending on how the random walk fell. Difficulty tracks path length and turn count, not the fixed passage total.
Related tools on this site
If you are assembling a party pack or a classroom set, pair the maze with other printables. The Bingo Card Generator makes matching game cards from your own word list or classic 75-ball numbers, which works well as a second station after solvers finish their mazes. For younger children who are not ready for a full maze, the Dot-to-Dot Maker turns a picture into a numbered connect-the-dots sheet that builds the same pencil control.
Frequently asked questions
Why does every maze have the same number of passages for a given size?
Because a perfect maze is a spanning tree, and a tree over N nodes always has exactly N - 1 edges. A 15 x 15 maze has 225 cells and always 224 passages. Only the arrangement changes between generations.
Can a generated maze ever have no solution?
No. The carve does not stop until every cell is visited and connected, so entrance and exit are always joined by exactly one path. Unreachable areas cannot occur in a perfect maze.
How do I make a maze harder without making it bigger?
You cannot change difficulty within a fixed size and style directly, since the passage count is fixed. Generate a few times and pick the one whose solution path is longest and most winding, or step up the grid size, which lengthens the typical path by roughly 4 cells per added row.
Should I print in PNG or SVG?
Use SVG if you will resize or print large, because it stays sharp at any scale. Use PNG for quick embedding at the exact pixel size you exported.
Does anything leave my device?
No. The maze is built entirely in your browser. Nothing is uploaded unless you choose to publish an embed.