Ctrl K

Six-Cell Tile Slider

Hard
A tiny sliding puzzle is a 2 × 3 frame holding tiles 1 to 5 and one empty cell, written as 0. In one move you slide any tile that shares a side with the empty cell into it (that is, you swap 0 with an adjacent number). The puzzle is solved when the frame reads [[1, 2, 3], [4, 5, 0]]. Given the current frame panel, return the smallest number of moves that solves it, or -1 if it cannot be solved.
Example 1
panel=[[1, 2, 3], [0, 4, 5]] → 2

Slide 4 to the left, then 5 to the left: 2 moves.

Example 2
panel=[[2, 1, 3], [4, 5, 0]] → -1

No sequence of slides can swap just two tiles, so the answer is -1.

Example 3
panel=[[1, 2, 3], [4, 5, 0]] → 0

The puzzle is already solved.

Constraints
  • panel has 2 rows of 3 numbers
  • The numbers in panel are 0, 1, 2, 3, 4, 5, each exactly once
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Uphill Walk HardUphill-Only Walks HardEvery Way to Space the Words HardCutting a Berry Cake HardMouse, Cat and Cheese Hard
loading Python…
Input
panel=[[1, 2, 3], [0, 4, 5]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 81 tests on the server, hidden ones included.