Maze Generator
This tool builds a maze with a depth-first search known as recursive backtracking. Every square is connected to every other by exactly one route, so there is one way from the entrance at the top left to the exit at the bottom right.
How to use
- Pick a size from 10 x 10 for kids to 40 x 40 for a long challenge, then press New maze.
- Press Show solution path to see the route from the entrance at the top left to the exit at the bottom right. Press it again to hide it.
- Press Print to print only the maze. Turn on the solution path first if you want an answer sheet.
- Each maze has a number. Type the same number and size later, or share the link, to get exactly the same maze again.
How it works
The maze is carved with recursive backtracking, a depth-first search. It starts at a random square with every wall closed and repeatedly moves to a random neighbor that has not been visited, knocking down the wall between them. When it reaches a square with no unvisited neighbors, it backs up along its path until it finds one that does.
Because every square is visited once and each step opens exactly one wall, the result is a perfect maze: all squares are connected and there are no loops, so there are exactly (squares - 1) passages and one route between any two squares. This method tends to make long winding corridors with few branches.
The solution is found with a breadth-first search from the entrance, which gives the shortest route. Since a perfect maze has only one route, that route is the answer.
The maze number is the seed of a pseudo-random number generator. The same seed and size always produce the same maze, so you can share a maze by sharing its number.
Maze sizes
| Size | Squares | Good for |
|---|---|---|
| 10 x 10 | 100 | Young children |
| 15 x 15 | 225 | Early school age |
| 20 x 20 | 400 | General puzzle |
| 30 x 30 | 900 | Teens and adults |
| 40 x 40 | 1,600 | A long challenge, best printed full page |
FAQ
Does every maze have a solution?
Yes. All squares are connected, and the entrance at the top left and the exit at the bottom right are both open. There is exactly one route between them.
Which size should I choose for children?
10 x 10 suits young children. 15 x 15 and 20 x 20 are good for school age, and 30 x 30 and 40 x 40 are for a real challenge. Print larger ones on a full page.
Can I get the same maze again?
Yes. Keep the maze number and size, or use Share link. Entering the same number and size always gives the same maze.
Why do these mazes have long winding paths?
Recursive backtracking goes as deep as it can before turning back, which makes long corridors and fewer short dead ends than other methods.
Is anything uploaded?
No. The maze is generated and drawn in your browser.