Sliding Puzzle
Problem statement
The 2 x 3 board contains tiles 1 through 5 and one empty cell 0. In one move, swap the empty cell with a tile directly above, below, left or right of it.
Return the minimum moves needed to reach [[1,2,3],[4,5,0]]. Return -1 if the target is unreachable.
Function
slidingPuzzle(board: int[][]) → intExamples
Example 1
board = [[1,2,3],[4,0,5]]return = 1Swap the empty cell with tile 5.
Example 2
board = [[1,2,3],[5,4,0]]return = -1Swapping the two numbered tiles changes the component; no sequence reaches the target.
Example 3
board = [[4,1,2],[5,0,3]]return = 5The shortest path uses five legal swaps.
Constraints
boardhas exactly two rows and three columns.- Its six cells contain each integer from
0through5exactly once.