On a 2 x 3 `board` there are five tiles labeled 1 to 5 and one empty square shown as 0. A move swaps 0 with a tile directly up, down, left or right of it. The puzzle is solved when the board is `[[1,2,3],[4,5,0]]`. Return the fewest moves needed to solve it, or -1 if it cannot be solved.
On a 2 x 3 board there are five tiles labeled 1 to 5 and one empty square shown as 0. A move swaps 0 with a tile directly up, down, left or right of it. The puzzle is solved when the board is [[1,2,3],[4,5,0]]. Return the fewest moves needed to solve it, or -1 if it cannot be solved.
Example 1:
Input: {"board":[[1,2,3],[4,0,5]]}
Output: 1
Constraints:
board.length == 2
board[i].length == 3
0≤board[i][j]≤5
Each value board[i][j] is unique
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"board":[[1,2,3],[4,0,5]]}
board
1
2
3
4
5
1
2
3
4
5
0
goal 123450
steps 0
seen 1/360
queue for level 0: 1 board(s)
1
2
3
4
0
5
boards first reached at each number of moves
0
Flatten the board to "123405". Each board is a node and each slide is an edge, so BFS from the start finds the fewest moves to "123450".