The sliding puzzle is a classic example problem for learning how computers solve problems (search). Here are a few methods, along with the results we got when we actually tried them.

1. Check everything (breadth-first search)

Starting from the solved board, you expand step by step: the positions reachable in 1 move, then in 2 moves, and so on. The number of moves at which you first reach a position is its minimum number of moves.

A 3×3 has only 181,440 reachable positions, so checking them all takes an instant. If you keep a table of the minimum moves for every position, from any board you just pick a move that lowers the table’s number by 1 each time, and you solve it by the shortest route (The fewest moves for the 8 puzzle).

A 4×4 has about 10.5 trillion positions, and this method cannot keep up.

2. Use an estimate (IDA*)

On big boards, you skip wasted searching by using an estimate of ”at least how many more moves this board needs.”

The simplest estimate is the total distance of the tiles from their correct places. Moving one tile can lower that total by only 1, so the minimum number of moves is never smaller than this total.

311151272614945813101
Total distance 32, fewest moves 54

IDA* (iterative deepening A*) repeats “search only the routes that, counting the estimate, finish within a given number of moves,” and if it finds none, it raises the limit a little at a time. It needs to remember only a few positions, so it works even on big boards.

The more accurate the estimate, the fewer routes it has to search.

  • If two tiles in the same row (or column) have to pass each other to reach their places, it takes at least 2 more moves (on the board above, that makes 36 moves).
  • Split the tiles into groups of a few each, and precompute a table of the minimum moves for every arrangement of each group (a pattern database).

On this site, we built tables that split the tiles into three groups of five, and used IDA* to find the minimum number of moves for 100 of the app’s ★2 (4×4) puzzles. Building the tables took about 3 seconds, and solving one puzzle took about 0.2 seconds on average (about 3 seconds for the longest). The average minimum was 53.1 moves (The fewest moves for the 15 puzzle).

3. Choose speed (the app’s hint)

The “Hint” in the app “Slide Puzzle” has to show the next move right after you press it, even on a 7×7. So it gives up on the shortest route and uses a method close to how people solve.

  1. Solve the top row one tile at a time from the left (the last two as a pair).
  2. Solve the left column one tile at a time from the top (the last two as a pair).
  3. Leave the finished row and column alone, and repeat steps 1 and 2 on the remaining board.
  4. Once it shrinks to 3×3, finish with the “table of every position” from above.

When placing one tile (or a pair of two), it only looks at the empty space and the position of the tile it wants to place. It ignores the other tiles, so it checks very few positions, and each calculation finishes almost instantly.

In exchange, it takes more moves than the minimum. Comparing on puzzles made the same way the app makes them:

BoardAverage hint movesAverage fewest moves
3×322.522.5
4×486.753.1

A 3×3 uses the table, so it is exactly the shortest. A 4×4 takes about 1.6 times the minimum (we compared 500 puzzles on 3×3 and 100 on 4×4). If you follow the hints, you will always solve it, but if you aim for the shortest route, there is room to think for yourself (How to use hints).

The bigger the board, the harder it gets

As the board grows, finding the minimum number of moves suddenly becomes hard. Finding the minimum number of moves on an n×n board is known to be an “NP-hard” problem, one for which no efficient method is known. The maximum number of moves for a 5×5 is still unknown, too.

On the other hand, if you do not insist on the shortest route, any board size can always be solved with steps like the ones above. The same goes when people solve them (How to solve 5×5 and 7×7).