Solving Mazes

How to solve a maze? Practical methods for paper, walking, and code

11 min readBy Rachel Bennett
how-tosolvingmazesalgorithms

Learn how to solve a maze with the hand-on-wall rule, dead-end filling, Trémaux's method, Pledge, BFS, A*, and flood fill.

In this article

Short answer: for a standard "perfect" maze, with one route and no loops, the hand-on-wall rule works. Put one hand on a wall, keep it there, and keep moving. It may take you through every wrong turn first, but it will lead you to an edge exit.

For paper mazes, though, there is usually a faster pencil method: dead-end filling. And if the maze has loops, islands, or a goal in the middle, you need a different approach. This guide walks through the useful options: wall following, dead-end filling, Trémaux's method, the Pledge algorithm, and the computer methods behind searches for "maze solving algorithm."

Want to practice right now? Open our maze generator to make a fresh maze, or grab a free sheet from our printable mazes collection and follow along with a pencil.

How to solve a maze diagram showing the hand-on-wall rule and dead-end filling

the short answer

Most one-page maze worksheets, including the mazes in our printable mazes for kids, are simply connected. That means the walls all tie back to the outside border. There is no free-floating wall island in the middle.

For that kind of maze, the basic rule is simple:

Keep one hand on the wall and walk forward. Do not switch hands. Follow every turn, including the dead ends. When the corridor turns you around, keep the same hand on the same wall and come back out.

It is not the shortest route. It can feel painfully slow. But on a simply connected maze with an edge exit, it gets you out. Everything else on this page either makes you faster, handles odd maze layouts, or finds the shortest route instead of simply finding a route.

the hand-on-wall rule, step by step

The hand-on-wall rule is also called the wall follower, the left-hand rule, or the right-hand rule. Left or right does not matter. Switching sides halfway through does.

Here is the method on paper or on foot:

  1. Choose one side. Use your right hand or your left hand. Stay with it.
  2. Touch the wall at the entrance. If you chose your right hand, touch the wall on the right side of the start opening.
  3. Move forward with that contact. When the wall bends, you bend with it.
  4. Let the wall choose the turn. With your right hand on the wall, you turn right whenever you can, go straight when you cannot, and turn left only when the wall forces you.
  5. Walk into dead ends and back out. A dead end is not a mistake in this method. It is a U-turn.
  6. Stop when you reach the exit.

On paper, your "hand" is your pencil tip. Trace lightly so you can see the full route after you finish.

Here is the right-hand rule running on a maze. Notice how the path stays attached to one side, even when it wastes time in dead ends:

why it works (and when it fails)

Think of a simply connected maze as one connected wall system. When you keep your hand on that wall, you are not guessing at each fork. You are tracing the outside edge of the wall system. That edge eventually passes every opening connected to it, including the exit.

The failures are worth knowing before you rely on it:

  • The maze has wall islands or loops. If a wall section floats free in the middle, your hand can circle that island and return you to where you started. Mazes with multiple routes or deliberate loops can break plain wall following.
  • The goal is inside the maze. If the finish is a treasure spot in the center instead of an opening on the border, wall following may loop around the outside and never touch it.

On a printed sheet, look for wall blobs that do not touch the border. Also check whether the goal sits inside the maze rather than on an edge. If you see either, use Trémaux's algorithm or the Pledge algorithm below. Our standard sheets, including most rectangular easy mazes, are perfect mazes with edge starts and exits, so the hand rule works well on them.

dead-end filling: the best way on paper

If you have the whole maze in front of you, dead-end filling is usually cleaner than tracing a single route. It is the pencil method we use when checking maze worksheets, because it removes wrong corridors instead of asking you to guess the right one.

Here is the process:

  1. Find the dead ends. Look for corridors that stop at a wall.
  2. Shade backward from each dead end. Fill that corridor until you reach the first junction.
  3. Look again. Shading one dead end often turns the previous junction into a new dead end.
  4. Keep filling until the dead ends are gone. What remains open is the solution route. If the maze has more than one valid route, more than one route remains.

This is faster than tracing because you do not have to commit to one path and then backtrack out of every mistake. You can work from the start, the finish, and the middle at the same time. A true dead end cannot be part of the only route from start to finish, so shading it will not erase the answer.

For hard printable mazes, this is the method to teach first. It turns a dense page into a series of small, obvious decisions.

trémaux's algorithm for unknown mazes

If you are walking a maze and cannot see the whole layout, use Trémaux's algorithm, named for Charles Pierre Trémaux. It solves mazes with loops because it records where you have already been. The old thread-through-the-labyrinth idea works for the same reason.

The rules:

  1. Mark every passage you walk. Each passage can get at most two marks.
  2. At a new junction, choose any unmarked passage.
  3. At a junction you have seen before, choose the passage with the fewest marks. Prefer unmarked passages. If none are available, take a once-marked passage.
  4. Never take a twice-marked passage unless it is the only way to backtrack.

When you reach the exit, the passages marked once are the clean route. The passages marked twice are detours you tested and ruled out. In a hedge maze, you can mark with chalk, a trail of pebbles, or a note on your phone. The mark only needs to answer one question: "Have I already used this passage?"

the pledge algorithm

The hand-on-wall rule struggles when you start inside a maze with free-floating walls. Your hand can keep you orbiting the same island. The Pledge algorithm is built for that situation: escaping toward an outside boundary when the wall layout is not one connected piece.

You need a fixed direction, such as north.

  1. Pick a heading. For example, "I will keep trying to go north."
  2. Walk that direction until you hit a wall.
  3. Start wall-following and count your turns. Count right turns as +1 and left turns as -1, using 90-degree steps.
  4. Leave the wall only when both conditions are true: you are facing your original heading again, and your turn count has returned to 0.
  5. Walk your heading again until you hit the next obstacle.

The turn count is the important part. It keeps you from leaving a wall too early and circling the same island. Like wall following, Pledge is for escaping to an outer exit. It is not meant for finding a goal hidden inside the maze.

computer methods: bfs, a*, flood fill

If you searched for a maze solving algorithm for code, you probably want the shortest path and you can see the whole maze as data. That changes the problem. A program does not need to hug a wall; it can search the graph of cells and corridors.

The common choices are:

  • Breadth-first search (BFS). Explore outward from the start one layer at a time. The first time the search reaches the exit, you have the shortest path by number of steps. BFS is simple and reliable, but it may inspect many cells.
  • A* (A-star). Search outward like BFS, but give priority to cells that look closer to the goal. With a good distance estimate, A* often checks fewer cells while still finding the shortest route.
  • Flood fill. Assign each cell a distance from the goal, then move from the start to neighboring cells with lower numbers until you reach 0. This is common in micromouse-style robot solving because the map can update as the robot discovers walls.

A quick note on cost: BFS and flood fill run in about O(n) time, where n is the number of cells, because they touch each cell a limited number of times. A* has the same worst case, but the heuristic usually steers it away from large parts of the maze. For the formal versions, see Think Labyrinth and the Wikipedia maze-solving article.

The human methods above are for partial knowledge. The code methods are for a full grid or graph.

method comparison table

Use the situation to choose the method:

MethodBest forGuaranteed?Handles loops/islands?
Hand-on-wall (wall follower)Walking or tracing simple mazesYes, if simply connected with an edge exitNo
Dead-end fillingSolving on paper or printoutsYesYes
Trémaux's algorithmUnknown or loopy mazes on footYesYes
Pledge algorithmStarting inside / disconnected wallsYes (to an outer exit)Yes
BFSShortest path by computerYes (shortest)Yes
A*Fast shortest path by computerYes (shortest)Yes
Flood fillRobots / partial knowledgeYes (shortest)Yes

Rule of thumb: use dead-end filling on paper, wall following for a simple walk-through maze, Trémaux for a maze with loops, and A* when you are writing code.

paper vs real-life mazes

The best method depends on what you can see.

On paper, use dead-end filling. You can see the whole puzzle, shade wrong turns, and work from both ends. Another useful pencil trick: start lightly from the finish and work backward. The exit side often has fewer branches, so the route can reveal itself faster in reverse.

In a walk-through maze or corn maze, you usually cannot see the layout. If it is a simple edge-to-edge maze, choose a hand and keep it on the wall. You may walk extra distance, but you always know what to do next. If the maze has bridges, loops, or a center goal, switch to Trémaux and mark your route. Some commercial corn mazes are designed to defeat plain wall following, so do not assume the hand rule is enough.

Curious how the screen and paper experiences compare for practice? Browse printable mazes to try either format with the same puzzle.

helping a kid who's stuck

A stuck kid usually does not need the answer handed over. They need the next small move. The same ideas show up in our guide to helping kids when they get stuck:

  • Start from the finish, not the start. The exit usually has fewer false turns leading into it. Working backward narrows the puzzle fast and feels like progress.
  • Mark the dead ends together. Hand them a crayon and shade the obvious dead ends. Each shaded branch makes the real path easier to see.
  • Teach the hand-on-wall rule as a game. "Keep your finger on the wall and don't let go" is easier to remember than a lecture about algorithms.
  • Drop the difficulty. If a sheet is causing tears, it's too hard. Move down a level. The maze difficulty guide explains how the levels are built and how to choose the next one.
  • Celebrate the finish, not the speed. Reaching the end is the win. Timing comes later.

For grown-ups who want more challenge once the basics click, the adult printable mazes collection is the next step.

faq

what is the trick to solving any maze?

For a normal edge-to-edge maze with no loops, use the hand-on-wall rule: keep one hand on the same wall and walk. For mazes with loops or an interior goal, use Trémaux's algorithm or the Pledge algorithm. On paper, dead-end filling is usually the fastest reliable method.

how do you solve a maze step by step?

Pick the method that fits the maze. On paper, scan for dead ends, shade each one back to its junction, and repeat until the solution remains. In a simple walk-through maze, keep one hand on the wall. In a complex walk-through maze, mark every passage as you go and avoid twice-marked paths.

why does the hand-on-wall rule sometimes fail?

It fails when the maze has free-floating wall islands, deliberate loops, or a goal that is not on the outer boundary. In those cases, your hand can circle the wrong wall system. Use Trémaux's method or the Pledge algorithm instead.

what is the best maze solving algorithm for code?

For the shortest path when you can see the whole maze, use A*. It finds the shortest route and often checks fewer cells than breadth-first search (BFS) because its heuristic points it toward the goal. BFS is simpler and also finds the shortest path. Flood fill is useful for robots that discover the maze as they move.

how do you solve a maze on paper fast?

Use dead-end filling and work from both ends. Shade each dead end back to the nearest junction, repeat, and follow whatever stays open. Tracing one line is slower because every wrong turn forces you to backtrack.

how do you help a child solve a maze?

Start from the finish and work backward. Shade the obvious dead ends together, and teach the hand-on-wall rule as a "don't let go of the wall" game. If the maze is causing frustration, move to an easier difficulty.

what's the difference between a maze and a labyrinth?

A maze has branching paths and dead ends, so it's a puzzle to solve. A labyrinth has a single winding path with no choices; you just follow it. We cover the full distinction in maze vs labyrinth.

next step

Pick a method and try it on a fresh maze: