Everyday Tools

How to Solve the 15-Puzzle, and How Computers Make and Solve Mazes

A step-by-step method for the 8 and 15 sliding puzzles, why half of all shuffles are impossible, and how maze generators and shortest-path solvers work.

Saizul Amin
Saizul Amin30 Oct 2026 · 3 min read
How to Solve the 15-Puzzle, and How Computers Make and Solve Mazes

How to Solve the 15-Puzzle, and How Computers Make and Solve Mazes

The sliding puzzle and the maze are two of the oldest puzzles around, and both hide neat bits of mathematics. This guide gives a reliable method for solving the sliding puzzle and explains how the maze generator and solver builds and solves its mazes.

The 15-puzzle craze

In 1880 the 15-puzzle swept America and Europe: fifteen numbered tiles in a 4 × 4 frame with one gap. Sam Loyd later claimed to have offered a large prize for solving it with 14 and 15 swapped. Nobody could, and for a good reason.

Why half the shuffles are impossible

Sliding a tile changes the arrangement in a way that preserves a hidden quantity called parity. In 1879, Johnson and Story proved that only half of all arrangements can be reached from the solved position. For a 3 × 3 puzzle, count the inversions, pairs of tiles in the wrong order, ignoring the gap: the puzzle is solvable when that number is even. For a 4 × 4 puzzle, add the row of the gap counted from the bottom; the puzzle is solvable when the sum is odd. Swapping just two tiles, as in Loyd's puzzle, flips the parity and makes it impossible. Every shuffle on this page is checked before you play.

A method that always works

Speed-solvers use clever shortcuts, but this layered method never gets stuck:

  1. Top row. Place 1, 2 and 3 in their spots. For the last tile of the row (4), first park it below its target, then bring 3 and 4 into place together by rotating them round.
  2. Second row. Do the same for 5 to 8.
  3. Last two rows, column by column. Place 9 and 13 together as a vertical pair: put 13 in 9's spot, 9 beside it, then rotate both into the left column. Repeat for 10 and 14.
  4. Final 2 × 3 block. Cycle the remaining tiles round until 11, 12 and 15 drop into place.

The same idea works on the 3 × 3 and 5 × 5 boards. With practice you will need 100 to 200 moves for the 15-puzzle; the best possible solution for any position is never more than 80 single-tile moves.

How mazes are generated

The maze generator uses a randomised depth-first search:

  1. Start with every wall standing.
  2. From the current square, pick a random unvisited neighbour, knock down the wall between them and move there.
  3. When there are no unvisited neighbours, backtrack until there are.
  4. Stop when every square has been visited.

The result is a perfect maze: every square is reachable, and there is exactly one path between any two squares, with no loops. Depth-first mazes tend to have long winding corridors, which makes them satisfying to solve.

How the solver finds the shortest path

Breadth-first search explores the maze in rings: first every square one step from the start, then every square two steps away, and so on. The first time it reaches the exit, the route it took is guaranteed to be the shortest. In a perfect maze the shortest path is also the only path, so the highlighted line is the solution.

Solving mazes by hand

Two classic tricks: the wall-follower rule (keep one hand on a wall) works in any perfect maze, though not always quickly; and working backwards from the exit often reveals the route faster.

Classroom ideas

Print a maze and its solution to compare search strategies, or ask pupils to count inversions to predict whether a sliding puzzle can be solved before trying.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages