hardGraphsPure DSA~35 min
Solve the 2x3 Sliding Tile Puzzle
A 2x3 board holds tiles numbered 1–5 and one empty slot (0). A move slides a tile adjacent to the empty slot into it. Find the fewest moves to reach the solved board.
Problem
On a 2x3 board, tiles are numbered 1..5 with 0 as the blank. A move swaps 0 with an adjacent tile. Given the start board, return the least number of moves to reach [[1,2,3],[4,5,0]], or -1 if unreachable.
Input
A 2x3 integer matrix board containing a permutation of 0..5.
Output
An integer: minimum moves to solve, or -1 if impossible.
Constraints
- board.length == 2 and board[i].length == 3
- board contains exactly the values 0,1,2,3,4,5 once each.
Examples
Example 1
Input
[[1,2,3],[4,0,5]]
Output
1
Slide the 5 left into the blank.
Example 2
Input
[[1,2,3],[5,4,0]]
Output
-1
This configuration is unsolvable.