Maze solver

How to Solve a Maze Using Search and Backtracking

Solving a maze is graph search. You occupy a cell. Legal moves are the passages stored on that cell. Maze Foundry’s solver and hint system use that graph directly. They do not interpret photographs. If you want to practice by hand, the same rules apply: never step through a wall, mark where you have been, and know whether you need any path or a shortest path.

Understanding maze connectivity

In a perfect maze the graph is a tree. There is one simple route between entrance and goal. Breadth-first search and depth-first search reconstruct that same unique path; they may visit different numbers of cells on the way. In a braided maze extra passages create cycles. Several routes can exist. BFS still returns a shortest unweighted route, meaning the fewest graph edges. DFS returns some valid route.

Identifying dead ends

A dead end has degree 1: only one passage. If you walk into one, the only legal move is back. Junctions have degree 3 or more. Corridor cells have degree 2. The workshop ledger counts these after generation. They describe structure, not intelligence.

Marking visited cells

On paper, tick a corridor you have fully explored. In the interactive challenge, visited cells receive a filled marker in addition to a muted fill, so color is not the only signal. Restart clears marks and the timer but keeps the maze. New maze draws a new seed unless you keep the old one on purpose.

Manual backtracking

Walk forward until every unused passage at the current cell is exhausted, then return to the previous junction. That is depth-first search with a stack of junctions. It will finish a connected maze. It does not guarantee a shortest route when cycles exist, because you might wander a long loop before you see a shortcut.

Depth-first search

The site’s DFS solver uses an explicit stack, records predecessor links, and reconstructs a path from the goal. It is iterative. On a perfect maze it finds the unique simple path. On a cyclic test graph it may be longer than BFS. Animate search to watch visits; pause, step, and reset are available on the solver page.

Breadth-first search

BFS uses a queue with a head index so large mazes do not pay for array.shift. Visit the start, then all cells one edge away, then two, and so on. The first time the goal is reached, the predecessor chain is a shortest path. Path length is the number of edges. Solution coverage is 100 × cells on that path / total cells.

A verified small example

Consider four cells in a square with an extra diagonal passage: 0 connected to 1 and 3 and 2; 1 to 0 and 2; 2 to 1 and 3 and 0; 3 to 0 and 2. Start at 0, goal at 2. BFS length is 1 because of the direct 0–2 edge. A DFS walk that goes 0–1–2 has length 2. Both are valid; only BFS is shortest. The automated tests on this site include that fixture.

0123

The terracotta cell is the goal. The short route is the diagonal. Wall following that insists on keeping one hand on the outer wall can miss that shortcut or loop on a cycle.

Shortest-path reconstruction

Store prev[neighbor] = current when a cell is first discovered. From the goal, follow prev until the start, then reverse. Every consecutive pair must be a passage. The SVG solution renderer draws cell-center to cell-center for rectangles and hexes, and arc or radial segments for circular mazes, never a chord through a wall.

Perfect maze solutions and braided mazes

Perfect: one simple path. Braided: possibly many. Hints compute BFS from the player’s current cell, not a leftover piece of the original start path. Next move, next three moves, and remaining route are distinct from Reveal solution.

Wall-following limitations

Right-hand and left-hand rules can solve simply connected planar mazes with the walls forming one piece relative to the walker. They are not guaranteed on braided mazes, mazes with detached wall islands, or polar and hexagonal connectivity where “right” is an angular neighbor. Maze Foundry does not present wall following as a universal algorithm.

Using the interactive maze solver

Generate a maze, open the solver, choose BFS or DFS, and read path length, visited cells, method, and duration on this device. Import a validated configuration if you want to re-solve a saved graph. The tool will not claim to read an uploaded photo.

When you play, Start challenge unpauses the timer. Pause excludes that interval from elapsed time; the implementation accumulates performance.now deltas rather than counting timeout ticks as seconds. Hint modes are next move, next three moves, and remaining route. Reveal solution is separate and is recorded in the completion summary. Extra moves equal valid moves minus the shortest length. Path efficiency is computed only after you occupy the goal cell.

Keyboard handling captures arrows and WASD only while the maze stage is focused, so typing in the seed field still works and the page still scrolls outside the game. On-screen pads remain available so swipe gestures are never required. Hexagonal movement uses the six axial directions; circular movement uses in, out, clockwise, and counterclockwise.

FAQ

Does DFS always match BFS?
On perfect mazes, yes for the unique simple path. On cyclic mazes, not necessarily for length.
Do invalid key presses count as moves?
No. Wall bumps do not change position or the valid move counter.
Is path efficiency an IQ score?
No. It is 100 × shortest / your valid moves after you finish.

Play at Play a Maze Online or compare generators in the algorithm article.