Solving The 8 Puzzle With A Algorithm
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The 8-puzzle is a small search problem with a large enough state space to make brute force wasteful. A* is a good fit because it combines the exact path cost so far with a heuristic estimate of remaining work, which lets it prioritize promising board states while still finding an optimal solution when the heuristic is admissible.
Represent the board as an immutable state
A simple representation is a tuple of nine numbers where 0 is the blank tile. The goal state is usually (1, 2, 3, 4, 5, 6, 7, 8, 0).
Using an immutable state makes it easy to store boards in sets and dictionaries for visited tracking.
A* uses g, h, and f
For each state:
- '
gis the number of moves from the start state' - '
his the heuristic estimate to the goal' - '
f = g + his the priority used in the search frontier'
For the 8-puzzle, Manhattan distance is the standard heuristic. It sums how many row and column moves each tile is away from its target position.
Manhattan distance is admissible for the 8-puzzle, which means it never overestimates the remaining cost. That is why A* with this heuristic still finds shortest solutions.
Generate neighbor states by moving the blank
Each move swaps the blank with one adjacent tile. The valid moves depend on the blank position.
This is the transition model A* explores.
A minimal A* solver
This solver stores the best known path to each state and reconstructs the solution once the goal is reached.
Check solvability before searching
Not every 8-puzzle arrangement is solvable. For the 3x3 puzzle, solvability depends on inversion parity. If the number of inversions is odd, the puzzle cannot be solved from the chosen goal state.
That check saves time because no search algorithm can find a path that does not exist.
Why A* works well here
Breadth-first search also finds an optimal solution, but it explores many more states because it ignores how close a board appears to the goal. A* uses the heuristic to focus effort where it matters. For the 8-puzzle, Manhattan distance is simple, cheap, and strong enough to make a real difference.
For larger puzzles such as the 15-puzzle, heuristic quality matters even more because the search space grows dramatically.
Common Pitfalls
- Using a heuristic that overestimates and then assuming A* is still guaranteed optimal.
- Forgetting to track visited states or best known path costs.
- Mutating board state in place and then corrupting set or dictionary keys.
- Skipping the solvability check and wasting time on impossible inputs.
- Treating the blank tile as a normal tile in the heuristic calculation.
Summary
- Represent the 8-puzzle as an immutable state and search over valid blank-tile moves.
- A* prioritizes states using
f = g + h. - Manhattan distance is the standard admissible heuristic for this puzzle.
- Track parent pointers and path costs so you can reconstruct the optimal path.
- Check solvability first to avoid searching impossible configurations.
Related reading
- Sorting an Array in TensorFlow
- Sorting by simliarity
- SpaCy Spancat Model is Not Making Predictions
- Spark K-fold Cross Validation
- Some followup questions about consistent hashing
- Sort 2 lists in Python based on the ratio of individual corresponding elements or based on a third list
- Spark ML - MulticlassClassificationEvaluator - can we get precision/recall by each class label?
- Spark MLlib / K-Means intuition

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.