Maze Generator - Random Mazes Online
Games & Puzzles
Generate random mazes in any size using the recursive-backtracking algorithm. Solve manually or reveal the solution path. Free & 100% browser-based.
Click Generate to create a maze.
How to use this tool
- Choose the maze size (columns × rows) and click "Generate".
- Navigate with the arrow keys, WASD, or the on-screen direction buttons to move from Start (S) to Finish (F).
- Click "Show Solution" to reveal the shortest path.
- Use Reset Player to try again, or Download PNG to save the current maze view.
How to navigate
Use the arrow keys or WASD to move your position through the maze. The direction buttons below the maze also work on touchscreens. Find your way from the start (the top-left) to the finish (the bottom-right); Reset Player returns you to the start without changing the maze.
The maze-generation algorithm
This tool generates mazes with recursive backtracking, a depth-first search (DFS) method. Every cell begins with four walls. Starting at the top-left, the generator chooses a random unvisited neighbor, removes the shared wall, and continues. When it reaches a dead end, it returns to an earlier cell and tries another branch until every cell is connected. For a grid with n cells, this process opens n − 1 passages, forming a spanning tree: there are no loops, every cell is reachable, and exactly one route joins any pair of cells. The Show Solution control uses breadth-first search (BFS) to reveal the route to the finish.
Other maze generators may use Prim’s, Kruskal’s, Wilson’s, or Eller’s algorithm. The methods below are background examples, not choices in this tool; different implementations can produce different corridor and dead-end patterns.
- Prim’s algorithm: grows a spanning tree by repeatedly connecting a frontier cell to the visited maze. Variants can produce a more branchy texture.
- Kruskal’s algorithm: considers candidate connections and joins cells from separate sets, skipping connections that would make a loop.
- Wilson’s algorithm: uses loop-erased random walks to sample a uniform spanning tree of the grid graph.
- Eller’s algorithm: processes one row at a time, which can keep memory use low for tall or streamed grids.
What makes a maze harder?
Difficulty is subjective and depends on both the layout and the person solving it. Common factors include:
- Number of dead ends: more dead ends means more false paths to explore.
- Solution path length: a longer solution route relative to the grid size increases difficulty.
- Grid size: larger grids contain more possibilities and require more effort to navigate.
Mazes in computing and robotics
Maze generation and solving are classic computer-science exercises because they model path-finding problems on a graph. DFS builds the maze by exploring unvisited cells; BFS checks paths in increasing step count and therefore finds a shortest route when every move has equal cost. A* uses a distance estimate, such as Manhattan distance on a square grid, to prioritize promising cells. Real GPS routing, game AI, circuit-board routing, and robot navigation use related graph-search ideas, often with weighted distances, obstacles, or changing costs. This maze is an unweighted grid, so its highlighted BFS route is shortest by number of steps.
How common algorithms differ
The table summarizes common tendencies, not guaranteed outcomes. Recursive backtracking is the only generation method selectable here; other algorithms are included for comparison.
| Algorithm | Dead ends | Corridor style | Statistical bias |
|---|---|---|---|
| Recursive Backtracker (DFS) | Moderate | Long, winding | River bias (long passages) |
| Prim's | Many | Short, branchy | Spreads from center |
| Kruskal's | Many | Uniform, sparse | Low bias |
| Wilson's | Moderate | Irregular | Truly uniform (UST) |
| Eller's | Variable | Row-by-row | Low; memory efficient |
Solving strategies
- Right-hand rule (wall follower): keep your right hand touching a wall and walk forward. It can find an exit in a simply connected maze when the entrance connects to the outer boundary; it is not a general shortest-path method.
- Dead-end filling: identify all dead ends and fill them in from the tip back to the last junction. Repeat until no dead ends remain - the solution path is what's left.
- Breadth-first search (BFS): explores all cells layer by layer from the start, guaranteeing the fewest-step route when each move has the same cost. Show Solution uses BFS.
- A* (A-star): uses a heuristic (often Manhattan distance) to prioritize cells that appear closer to the goal. Its performance benefit depends on the grid and heuristic.
Frequently asked questions
What algorithm is used?
Recursive backtracking (DFS). It carves a random path from a starting cell, backtracks when stuck, and continues until every cell has been visited - guaranteeing a perfect maze (one unique solution path).
What does "perfect maze" mean?
A perfect maze has exactly one path between any two cells - no loops, no isolated areas. The solver always finds the unique route from Start to Finish.
How large can the maze be?
You can set columns and rows from 5×5 up to 40×40. Larger mazes take more time to generate but are all instant in practice.
Related tools
- SudokuGenerate a number-placement puzzle when you want a different kind of grid challenge.
- Word SearchBuild a word-search puzzle to print or solve after finding your way through the maze.
- Sliding PuzzleTry a spatial puzzle that asks you to plan moves across a constrained grid.
- Nonogram PuzzleSolve a logic puzzle by using row and column clues to reveal a hidden picture.