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.
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:
- 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.
- Do the left column the same way. Put 4 and 7 into place together, without disturbing the top row.
- 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:
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.