Klotski Solver - Free Online Klotski Huarong Dao Solver
What Is Klotski?
Klotski is a sliding-block puzzle played on a 4-wide, 5-tall board. The largest block is a 2×2 piece representing the general Cao Cao; the goal is to slide him out of the escape opening at the bottom-centre of the board. Around him sit vertical 1×2 generals, one horizontal 2×1 general (Guan Yu), and small 1×1 soldiers - and only two cells of the board are ever empty. The best-known starting layout, 横刀立马 (Horse Riding Sideways), needs 81 moves when a straight-line slide counts as one move.
The puzzle is named after the Three Kingdoms story of Cao Cao escaping through the Huarong Trail after the Battle of Red Cliffs - ironically helped by Guan Yu, the very general guarding the pass. Physical wooden versions have been sold for over a century, and mathematicians have catalogued its roughly 25,955 reachable positions. This Klotski solver runs a breadth-first search over that state space and replays the shortest solution for any layout you enter.
Klotski Rules
Slide, do not lift. A piece moves one square at a time, up, down, left or right, into empty space.
Two empty cells. The board always has exactly two empty squares; pieces cannot overlap or jump.
The goal. Move the 2×2 Cao Cao piece so it sits over the bottom-centre exit.
Counting. Traditional counting treats a piece sliding several squares in one direction as a single move; this solver counts each single-square step, so its move counts are equal or larger.
How to Use the Klotski Solver
Enter the layout. The board uses one letter per piece - the shape decides the type (2×2 = Cao Cao, 1×2 vertical = general, 2×1 horizontal = Guan Yu, 1×1 = soldier) - and a dot for empty cells. The classic 横刀立马 layout is preloaded.
Check the parse. The solver validates letters, sizes and total coverage (18 cells) instantly.
Press Solve. The breadth-first search returns the minimum number of single-step moves - or proves that the layout has no solution.
Replay the solution. Use ⏮ ◀ ▶ ⏭ to step through the solution; the pieces slide smoothly. ▶ auto-plays the whole escape.
Edit and retry. Change letters to build your own layout and solve it the same way.
What the Solver Tells You
For the classic layout the solver returns 116 single-step moves - exactly the famous 81-move solution counted the traditional way - and replays every step:

Klotski layouts are not all solvable. If your letters describe a position from which Cao Cao can never escape, the BFS exhausts the entire reachable state space and reports it - a proof, not a timeout:

How the Klotski Solver Works
The solver is a breadth-first search (BFS) over game states. Each state is the complete position of every piece; the key trick is state canonicalisation - two positions that differ only by swapping two identical soldiers are the same state, so the search merges them. From every state the solver generates all legal one-square slides, marks each new state with its predecessor, and expands outward level by level. The first state to reach the goal sits at the minimum possible depth - that is what BFS guarantees.
The classic layout has about 25,955 reachable states, so the whole search - including path reconstruction into animation frames - finishes in a third of a second. The solver deliberately canonicalises by piece type rather than piece identity: with 4 identical generals and 4 identical soldiers, identity-based keys would explode the state space millions-fold while adding nothing but redundant branches.
Worked Example: The First Moves of 横刀立马
In the classic layout, Cao Cao starts at the top-centre with Guan Yu right below him. The opening ideas: the two bottom soldiers shuffle sideways to open a lane, the vertical generals on the edges slide down to surround Cao Cao, and Guan Yu sidesteps so Cao Cao can begin his descent. The full solution takes 116 single-square slides - 81 in the traditional counting - and the solver replays every one of them, so you can watch exactly when each general steps aside and why the soldiers must keep clearing the corridor. Watch for the moment Guan Yu first moves horizontally: that is the pivot on which the whole escape turns.
Tips for Solving Klotski
Never trap Cao Cao against a wall early. Keep him near the centre; every step he takes toward a side wall costs a dozen moves to undo.
Think in corridors. A vertical general sliding down opens a column; two soldiers shuffling in opposite directions keep that column alive.
Guan Yu is the gatekeeper. He sits directly under Cao Cao in the classic layout - moving him is the single most important decision in the puzzle.
Soldiers are gears. The four 1×1 soldiers exist to rotate space around the big pieces; think of them as one mechanism, not four items.
Count in straight lines. When planning, treat a three-square slide as one move - it matches the traditional counting and keeps your plan readable.
When stuck, restart the opening. Most dead ends in Klotski are irreversible; the solver's replay makes it painless to compare your line with the optimal one.
Is Klotski Good for Your Brain?
Klotski is pure spatial planning. Because only two cells are free, every move changes the board everywhere at once, forcing you to hold a multi-piece configuration in your head and think several slides ahead - a genuine working-memory workout. It also teaches the counterintuitive lesson that sometimes the best move slides a piece away from where it ultimately needs to go, which is foundational planning practice for chess, logistics and programming alike.
Frequently Asked Questions
What is Klotski?
A sliding-block puzzle on a 4×5 board: slide the 2×2 Cao Cao block to the bottom-centre exit past vertical generals, a horizontal Guan Yu and small soldiers, with only two empty squares on the board.
Where does the name Huarong Dao come from?
From the Romance of the Three Kingdoms: after the defeat at Red Cliffs, Cao Cao fled along the Huarong Trail, where Guan Yu - whom he had once spared - let him pass. The puzzle casts you as Cao Cao squeezing past Guan Yu's blockade.
What is the minimum number of moves for the classic puzzle?
The famous layout 横刀立马 needs 81 moves when a straight multi-square slide counts once. Counting every single-square step separately - as this solver does - the same solution is 116 steps, and BFS proves nothing shorter exists either way.
Are all Klotski layouts solvable?
No. Roughly speaking most random layouts are not. The solver runs a complete search over the reachable states (about 25,955 for the classic piece set), so an "impossible" verdict is a genuine proof.
Why does the solver count 116 moves when the puzzle says 81?
This solver counts each one-square step. The traditional count merges a piece sliding several squares in a straight line into one move. Both count the same escape; only the bookkeeping differs.
Can I enter my own layout?
Yes - edit the five-row letter map. Use a different letter for every piece (the shape decides the type) and a dot for empty cells. The solver validates sizes, connectivity and the 18-cell coverage before searching.
Is this Klotski solver free?
Completely free with no sign-up. The search and the animated replay run entirely in your browser after the page loads, so it even works offline.
More Logic Puzzles to Explore
Enjoy sliding logic? Try the Sliding Puzzle Solver for the 8/15-puzzle, play our free Klotski game, or explore the Sumplete Solver.
