← Back to Visualizer Gallery
Traversal · State-Space BFS LeetCode 773

Sliding Puzzle / string serialization & permutation BFS

The $2 \times 3$ sliding puzzle board is serialized into a 6-character string. Blank tile '0' swaps with precomputed adjacent indices. The state space has at most $6! / 2 = 360$ reachable permutations.

Execution Stage

idle
INVARIANT Sliding Puzzle Invariant: The board is serialized into a 6-char string. Precomputed neighbor array DIR = [[1,3],[0,2,4],[1,5],[0,4],[1,3,5],[2,4]] guides all 0-swaps. The target is reached when string equals "123450".
Moves 0
Queue Frontier 0
Visited States 0
Press Run to trace Sliding Puzzle BFS.
0 / 0
Speed

Java · Reference Algorithm

INTERVIEW SCRIPT

How to Present Sliding Puzzle to an Interviewer

1. String Serialization

"A 2D $2 \times 3$ grid is inconvenient to hash. We flatten the board into a 6-character string like '123405'. This enables $O(1)$ string hashing and set lookups."

2. Precomputed Adjacency Table

"The blank tile '0' only swaps with its cardinal neighbors. We hardcode DIR = {{1,3},{0,2,4},{1,5},{0,4},{1,3,5},{2,4}} to avoid 2D coordinate conversion inside the inner loop."

3. Finite State Space

"There are $6! = 720$ total permutations of 6 digits. Due to parity of inversions, exactly half ($720 / 2 = 360$) are reachable. Thus, BFS visits at most 360 states before concluding."

4. BFS Optimality

"Because each swap costs 1 move, BFS guarantees that the first time we encounter '123450', it is the minimal move sequence."

Sliding Puzzle (LC 773) — Permutation graph BFS with string serialization · Time $O(V + E) \le 360$, Space $O(V) \le 360$.