← 返回 airbnb 的题目列表Sliding Puzzle
类型:online_judge
Given a 2 x 3 sliding-puzzle board, where 0 represents the empty cell, one move consists of swapping the empty cell with one of its up, down, left, or right neighbors.
Return the minimum number of moves needed to transform the board into the target state:
1 2 3
4 5 0
Return -1 if the target state cannot be reached.
Input format
Two lines with three integers each:
a b c
d e f
The six values are a permutation of 0 through 5.
Output format
The minimum number of moves, or -1 if impossible
Constraints
The board size is fixed at 2 x 3.
Input values are a distinct permutation of 0, 1, 2, 3, 4, 5.
Example 1
Input:
1 2 3
4 0 5
Output: 1
Example 2
Input:
1 2 3
5 4 0
Output: -1
Example
Input
1 2 3
4 5 0
Output
0