Slide Puzzle Solver

3x3 Sliding Puzzle Solver

Optimal solutions for the 8-puzzle — every board solved in 31 moves or fewer.

Click a cell and type its number. Use arrows to focus, Backspace to clear, Enter to solve. Separate two-digit tiles with a space.

Solvable

Inversions: 0 (counting out-of-order nonblank pairs); an even count is required for an odd-width board.

Example: a solution for this board

82673154
Initial example board. Follow every move below to reach the solved position.

24 moves · Shortest solution

Steps 1–10

  1. Step 1: Move 8 left
  2. Step 2: Move 7 up
  3. Step 3: Move 6 right
  4. Step 4: Move 1 up
  5. Step 5: Move 5 left
  6. Step 6: Move 6 down
  7. Step 7: Move 7 down
  8. Step 8: Move 8 right
  9. Step 9: Move 1 up
  10. Step 10: Move 7 left

Steps 11–20

  1. Step 11: Move 8 down
  2. Step 12: Move 2 left
  3. Step 13: Move 3 up
  4. Step 14: Move 4 up
  5. Step 15: Move 6 right
  6. Step 16: Move 8 down
  7. Step 17: Move 4 left
  8. Step 18: Move 6 up
  9. Step 19: Move 8 right
  10. Step 20: Move 5 right

Steps 21–24

  1. Step 21: Move 7 down
  2. Step 22: Move 4 left
  3. Step 23: Move 5 up
  4. Step 24: Move 8 left

How the 3x3 sliding puzzle solver finds the shortest path

A 3x3 board has 9! = 362,880 possible arrangements, and exactly half of them, 181,440, can be solved. That is small enough for a computer to search carefully, so this 3x3 sliding puzzle solver doesn’t settle for a solution that merely works: it proves that no shorter one exists. The method is called A* search.

A*: try the most promising board first

Picture every board you could reach as a stop on a map. A* keeps a list of boards it hasn’t explored yet and always picks the one where moves made so far + estimated moves still needed is smallest. As long as the estimate never overstates the real distance, the first time the search reaches the solved board, it has found the shortest route.

The estimate is what keeps the search fast. A good estimate lets A* ignore most of the 181,440 positions and head almost straight for the answer.

Manhattan distance

The basic estimate is Manhattan distance. For every tile, count how many rows plus how many columns it is from its home square, then add the counts up. Each move shifts one tile by one square, so a board can never be solved in fewer moves than this total.

12346758
Tile 5 is one row from home and tile 8 is one column from home: Manhattan distance 2. This board is solved in exactly 2 moves: slide 5 up, then 8 left.

Linear conflict

Manhattan distance treats tiles as if they could pass through each other. When two tiles sit in their home row (or column) but in the wrong order, one of them has to step out of the line and come back, which costs at least 2 extra moves. Adding these linear conflicts makes the estimate sharper without ever overstating it, so the answer is still the shortest.

Solving a 3x3 by hand

If you want to finish the 8-puzzle yourself, three habits do most of the work:

  1. Finish the top row first, and place 2 and 3 as a pair. Park 2 in the top-right corner with 3 right under it, then slide them both into place. Placing 3 on its own usually means breaking the row again.
  2. Do the left column the same way. Put 4 and 7 into place together, without disturbing the top row.
  3. Rotate the last 2x2. Tiles 5, 6 and 8 and the blank just cycle around the bottom-right square until they line up.

The full method, with diagrams for each step, is in how to solve a sliding puzzle. By hand you will usually use more moves than the solver, which is fine: the solver’s answer is the minimum.

The hardest 3x3 positions

Every solvable 3x3 board can be finished in 31 moves or fewer, and only two positions need all 31. Here is one of them:

86725431
One of the two hardest 3x3 positions: 8 6 7 / 2 5 4 / 3 _ 1 needs 31 moves.

Notice how far each tile is from home: 8, 6 and 7 fill the top row, and 1 sits in the bottom-right corner. All other solvable boards need fewer moves. Type 8 6 7 2 5 4 3 0 1 into the solver above to watch all 31 moves play out.

Frequently asked questions

Is the 3x3 solution always the shortest?

Yes. The solver uses A* search with an estimate that never overstates the moves left, so the first solution it reaches has the fewest possible single-tile moves. No other sequence of slides can finish your board in fewer moves, though there may be other routes of the same length.

What is the 8-puzzle?

The 8-puzzle is the usual name for the 3x3 sliding puzzle: eight numbered tiles and one empty square in a 3x3 frame. You slide tiles into the empty square until they read 1 to 8 in order with the blank in the bottom-right corner. It is the 3x3 version of the classic 15 puzzle.

What is the most moves a 3x3 sliding puzzle can need?

31 moves. Every solvable 3x3 board can be finished in 31 single-tile moves or fewer, and only two positions need all 31. One of them is 8 6 7 / 2 5 4 / 3 _ 1, shown on this page. You can paste it into the solver above to see the full solution.

Why does the solver say my 3x3 puzzle is unsolvable?

Because the board has an odd number of inversions, and on a 3x3 only an even count can be solved. Most of the time this comes from a typo, so check every tile against your puzzle first. If the board really is as entered, swapping any two tiles other than the blank makes it solvable.