Sliding Puzzle

On an 2 x 3 board, there are five tiles labeled from 1 to 5, and an empty square represented by 0. A move consists of choosing 0 and a 4 directionally adjacent number and swapping it.
Given the puzzle board, print the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, print -1.

solved state is : {{1,2,3},{4,5,0}}
Input Format
Each of next 2 lines contain 3 integer.
Output Format
Min number of moves required to achieve solved state.
Question Video
Constraints
rows = 2
cols = 3
Sample Input
1 2 3
4 0 5
Sample Output
1


  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name