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
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.
| Character | Square |
|---|---|
| # | 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.
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.
| Level | Fewest pushes | Fewest moves |
|---|---|---|
| Level 27 | 8 pushes, 71 moves | 10 pushes, 67 moves |
| Level 78 | 27 pushes, 120 moves | 33 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.
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.
| Levels | Levels | Solved | Fewest pushes proved | Typical time |
|---|---|---|---|---|
| Our Sokoban game, levels without sand or barrels | 51 | 51 | 51 | 0.01 s |
| Minicosmos (Aymeric du Peloux) | 40 | 40 | 40 | under 0.01 s |
| Microcosmos (Aymeric du Peloux) | 40 | 40 | 40 | 0.01 s |
| Microban (David Skinner) | 155 | 153 | 151 | under 0.01 s |
| Sasquatch (David Skinner) | 50 | 16 | 13 | 0.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.