Sokoban Solver

Stuck on a level? Draw it below or paste it as text, and the solver finds a way through, with the fewest pushes whenever it can prove it, then plays it back one step at a time. If a level has no solution, it tells you that too.

Built by Brian Hamilton

Draw with
Thinking time
Examples
Paste or copy the level as text

Spaces, - or _ are floor. Run-length counts such as 4# and | between rows are fine, and title lines are ignored.

Draw a level, or pick an example, then press Solve.

How to use the solver, and the rules it follows

The level follows the standard Sokoban rules: the player walks up, down, left and right, pushes one box at a time by walking into it, and can never pull. The level is solved when every goal has a box on it.

  • Draw by choosing Wall, Floor, Box, Goal or Player and tapping or dragging across the grid. Tap a box or goal again to remove it. Set the size with Width and Height (up to 30).
  • Paste a level in the standard text format under Paste or copy the level as text; the same box shows the level you drew, ready to copy. Levels pasted as text can be up to 50 by 50.
  • Solve searches in the background, so the page stays usable; Stop ends it. The answer gives the number of pushes and moves and plays back on the board.
  • The solution is written in LURD: u d l r are steps, capitals U D L R are pushes.
  • Copy link puts the level in a link, so you can share it or come back to it.

Type a level in, or paste the text

Sokoban levels are shared online as plain text, one character for each square, and the solver reads that format directly. Paste a level under Paste or copy the level as text, or draw it on the grid and the same box shows it as text, ready to copy.

The characters in a Sokoban level, as the solver reads them.
CharacterSquare
#Wall
space, - or _Floor
.Goal
$Box
*Box already on a goal
@Player
+Player standing on a goal

Two shortcuts turn up often and both work: a number before a character repeats it, so 4# is four walls, and | can stand for a new row, which fits a whole level on one line. Title lines are skipped. A box already on a goal and sealed off inside the walls, as some levels use for decoration, counts as wall. A level needs exactly one player and as many goals as boxes, and the solver says so if it has not.

Read the answer: lower case walks, capitals push

The solution comes as one letter per move, in what Sokoban players call LURD: u, d, l and r are a step up, down, left or right, and the capitals U, D, L and R are a step that pushes a box. Count the capitals for the pushes and every letter for the moves. The playback on the board draws the walk to the next push as a green line, so you can follow it without reading the letters at all.

The first seven letters of the warm-up solutionThe warm-up level at the start: two boxes, one already on a goal, and two goals. A dashed line shows the six steps rrrddl, three right, two down and one left, which bring the player below the box on the goal. A green arrow shows the push U that moves that box up, off its goal.
The warm-up's solution starts rrrddlU: six steps round to the far side of the box that is already on a goal (dashed), then a push that takes it off. That looks backwards, and it is still the shortest way: no solution uses fewer than 12 pushes, and none with 12 pushes takes fewer than 36 moves.

Fewest pushes is not always fewest steps

The solver counts pushes, because a push is the decision; the walking in between is just finding a route. Once it has the fewest pushes, it searches again for the solution with that many pushes and the least walking, and it says so when it has proved that too.

Usually one solution is best both ways. We compared the solver's answers on the 17 small levels of our own Sokoban game with a separate search that counts every step: on 15 of them the solver's answer also has the fewest moves. On the other two, saving steps costs extra pushes.

The two small levels from our Sokoban game where fewest pushes and fewest moves are different solutions.
LevelFewest pushesFewest moves
Level 278 pushes, 71 moves10 pushes, 67 moves
Level 7827 pushes, 120 moves33 pushes, 118 moves

So a medal-chasing route in the game, where only the clock matters, may push more than the solver does. Either way, the solver's answer is a solution you can follow move for move.

"No solution" means every position was checked

A search can end without a solution in two ways, and the solver keeps them apart. If it runs out of time, that proves nothing, and it says so. No solution is stronger: every position reachable from the start has been looked at, apart from ones that are certainly stuck, and none has every box on a goal.

No way through, at the startThe level: two boxes side by side in the middle, two goals in the top corners, the player below. Either box can only be pushed straight up first.
At the start, either box can only be pushed straight up.
Both boxes pushed upAfter both boxes are pushed straight up they sit side by side against the top wall, outlined in red. Neither can be pushed again: each blocks the square the player would need to push the other.
Both pushed up: side by side against the wall, and neither can move again.
Left box up, right box rightAfter the left box goes up, the right box can be pushed right instead, but it lands against the right-hand wall below the corner goal, outlined in red. It is a dead square: to push it up the player would have to stand inside the wall below it.
The right box pushed right instead lands on a square it can never leave.

The No way through example shows how that happens on a board that looks easy. Either box can only go straight up first. Whichever does, the other is trapped: pushed up too, it ends side by side with the first against the top wall, where neither can move again; pushed right, it lands against the wall under the corner goal with no square to push it up from. Altogether the two boxes can only ever reach 22 arrangements, 200 if you count where the player stands. One box can reach a goal; both never can.

If a level you know is solvable comes back with no solution, look for a wall in the wrong place or a goal that was missed when it was copied.

Why some levels take seconds and others never finish

The solver treats a position as where the boxes are and which area the player can walk to, and the number shown with every answer is how many of those it looked at. That number grows very fast with more boxes and more space, so most of the work is ruling positions out early:

  • Dead squares. A box on a square from which no goal can ever be reached is never pushed there. Show dead squares marks them on the playback board.
  • Frozen boxes. Boxes that hold each other against walls, like the pair in the example above, end the search down that path unless every one is on a goal.
  • Closed-off areas. When boxes wall off a part of the level the player cannot reach, and every push that could open it can be made now, the solver only tries those pushes first. It is safe: it never removes the shortest solution.

The rest is a best-first search guided by how far each box is from a goal. When the exact search for the fewest pushes runs out of time, a faster search takes over with whatever time is left and returns any solution it finds, marked as not proved shortest.

How far the solver gets in 15 seconds, the page's standard thinking time, on our own levels and four well-known level collections. Measured on a desktop PC; a phone will be slower. Every solution counted was replayed move by move to check it.
LevelsLevelsSolvedFewest pushes provedTypical time
Our Sokoban game, levels without sand or barrels5151510.01 s
Minicosmos (Aymeric du Peloux)404040under 0.01 s
Microcosmos (Aymeric du Peloux)4040400.01 s
Microban (David Skinner)155153151under 0.01 s
Sasquatch (David Skinner)5016130.7 s

The typical time is the median for the levels it solved. Small, neat levels like Microban's are solved before you can let go of the button. Sasquatch is a collection of larger, much harder levels, and most of them are beyond what this solver can search in a browser; it says so rather than guessing. Up to 60 s gives big levels more room.

Solvers, LURD and level text: the names

A Sokoban solver is sometimes called a Sokoban AI or a box-pushing solver; they all mean a program that searches for the pushes. LURD stands for the four directions. The plain-text level format is often called XSB, and most Sokoban programs and level collections read and write it, so a level copied from one usually pastes straight into another.

Play some levels yourself

Our free Sokoban game has 100 levels in ten worlds, with sliding barrels and sand as well as boxes, and its own hints. For apps worth installing, see the best Sokoban games. The other best move calculators, from Gomoku to Dots and Boxes, are on the calculators page.