Maze Generator
Make perfect mazes for worksheets and puzzle pages, rectangular or circular, and pick one of six algorithms by the challenge you want: one long winding path or many short dead ends. A seed rebuilds the same maze, and a printout can include an answer key.
Your maze
Start Finish Shortest solution
- Solution
- —
- Dead ends
- —
- Cells
- —
- Passages
- —
Settings
Cell size sets the pixel size of the SVG and PNG files; the preview and the printout scale the maze to fit.
Print and download
The worksheet never shows the solution. SVG and PNG files match the preview, with the solution line if it is switched on.
—
Everything runs in your browser. Your settings are saved on this device only, and nothing is uploaded.
Runs on your device. Nothing you type or record is uploaded. Keeps working without a connection while the page is open. Works with touch. Saves your settings and seed on this device, so a reload brings back the same maze.
Added
How to use the maze generator
- Pick a shape (rectangular or circular) and a size: Easy, Medium, Hard, or type your own rows and columns (2 to 100) or rings (2 to 40).
- Choose an algorithm; the note under it describes the look.
- Choose where the entrance and exit go. The openings are cut into the outer wall and marked with a circle (start) and a square (finish).
- Press New maze for a fresh random seed, or type a seed of your own to get a maze back later.
- Press Show solution to draw the shortest path, and read the solution length and the number of dead ends under the maze.
- Under Print and download, choose 1, 2 or 4 mazes per page and whether to add an answer key, then print, or save the maze as an SVG or PNG file.
How it works
Every maze here is a perfect maze: each cell can be reached, and there is exactly one route
between any two cells, so there are no loops. Treat the cells as points and every opening between two
neighbouring cells as a line joining them, and a perfect maze is a spanning tree of the grid.
A tree that joins n cells always has exactly n − 1 passages, which is why
a 20 × 20 maze shows 400 cells and 399 passages.
- Recursive backtracker walks from a random cell to a random unvisited neighbour, opening the wall between them. When it gets stuck it backs up to the last cell that still has an unvisited neighbour.
- Randomized Prim's keeps a list of walls between the finished part of the maze and the cells outside it, opens a random one and adds the new cell's walls to the list.
- Randomized Kruskal's takes every inner wall in random order and opens it only when the cells on its two sides are not yet connected (tracked with disjoint sets).
- Wilson's sends a random walk from a cell outside the maze until it hits the maze, erases any loops the walk made and carves what is left. The result is a uniform spanning tree: every possible perfect maze of the grid is equally likely.
- Binary tree lets every cell open one of two directions at random: north or east on a rectangle, towards the centre or clockwise on a circle.
- Eller's works one row at a time (one ring at a time from the centre on a circle). It joins random neighbours that are not yet connected, gives every connected group at least one passage into the next row, and joins all groups in the last row.
The solution comes from a breadth-first search (BFS). Starting at the entrance, it visits cells in order of distance and remembers where it came from; when it reaches the exit, following those notes backwards gives the shortest path. A dead end is a cell with a single opening, not counting the start and finish cells.
The seed text is turned into a 32-bit number that starts a pseudorandom number generator. The same seed with the same shape, size, algorithm and entrance setting always gives the same maze, in any browser. Cell size, wall thickness, markers and the solution line only change the drawing.
A circular maze has a centre cell and rings around it. Ring i gets the previous ring's
cell count multiplied by round(2π × i ÷ previous count), which keeps the cells
roughly square: 6, 12, 24, 24, 24, 48 cells and so on. Ten rings make 331 cells.
"Farthest apart" runs two searches: from any edge cell to the farthest edge cell, then from that cell to the edge cell farthest from it. In a tree, that pair has the longest route of all edge cells.
Examples
Example 1 — the default maze. Rectangular 20 × 20, recursive backtracker,
top-left to bottom-right, seed 2026: the solution runs through 167 of the 400 cells, and
there are 39 dead ends (10%).
Example 2 — same seed, Kruskal's. Change only the algorithm: the solution drops to 59 cells and the dead ends rise to 120 (30%).
Example 3 — binary tree. With the corner entrances the solution is 39 cells, the shortest route that is possible on a 20 × 20 grid, and it is 39 for every seed: along the open top row, then down the open right column. Switch to "Farthest apart along the edge" and the same maze gives a 73-cell solution.
Example 4 — circular. 10 rings, Wilson's, centre to the outside, seed
2026: 331 cells, 330 passages, a solution of 76 cells and 90 dead ends (27%).
Which algorithm makes the hardest maze?
There are two kinds of difficulty. A long, winding solution takes time to trace; many dead ends offer
many wrong turns. The table shows averages over 10,000 random 20 × 20 mazes with corner entrances
(seeds x1 to x10000), measured with this page's code on 22 October 2026. The shortest possible
solution on this grid is 39 cells.
| Algorithm | Dead ends | Average solution |
|---|---|---|
| Recursive backtracker | 10% of cells | 117 cells |
| Randomized Prim's | 32% | 44 cells |
| Randomized Kruskal's | 30% | 58 cells |
| Wilson's | 29% | 60 cells |
| Binary tree | 25% | 39 cells |
| Eller's | 28% | 57 cells |
The backtracker varies the most: eight in ten of its solutions are between 73 and 171 cells.
For a long trail, use the backtracker; for many short wrong turns, Prim's or Kruskal's on a larger grid.
Printing tips
- One 20 × 20 maze per page gives cells about 9 mm wide on A4 or Letter paper; the Easy 10 × 10 grid gives about 17 mm.
- Two or four mazes per page are different mazes. The first uses your seed, the others add
-2,-3and-4to it, so you can make the same sheet again. - The answer key is a separate page with the same mazes and their solutions.
- Walls scale with the maze on paper and are never printed thinner than about 0.3 mm.
Frequently asked questions
How do I make the same maze again?
Note the seed and the settings: together they always rebuild the same maze. The page also remembers your last settings on this device.
What is the difference between SVG and PNG?
SVG is a vector file: it stays sharp at any size and opens in vector drawing programs. PNG is an image at twice the cell size, for example 1020 × 1020 pixels for the default maze (at most 8,000 pixels a side for very large mazes). Both have a white background.
Why is the binary tree maze so easy from corner to corner?
Every cell opens north or east, so the whole top row and the whole right column are straight corridors that join the two corners.
Can it solve mazes step by step?
This page shows the finished shortest path only. To watch breadth-first search, A* and other searches explore a grid one cell at a time, use the Pathfinding Visualizer.
Sources
- Wilson DB (1996). Generating random spanning trees more quickly than the cover time. Proceedings of the 28th ACM Symposium on Theory of Computing: 296–303. The loop-erased random walk behind Wilson's algorithm and its uniform spanning trees.
- Buck J (2015). Mazes for Programmers. Pragmatic Bookshelf. Describes the binary tree, recursive backtracker, Kruskal's, Prim's, Wilson's and Eller's algorithms and circular (polar) grids.