A loading screen shows a tiny sliding puzzle: a 2 by 3 board with tiles 1 to 5 and one empty slot, written 0. One move slides a tile that is directly above, below, left, or right of the empty slot into it.
The puzzle is solved when the board is [[1, 2, 3], [4, 5, 0]]. Write fewestSlides(board) that returns the fewest moves to solve it, or -1 if it can't be solved.
Input: board = [[1,2,3],[4,0,5]] Output: 1
Slide the 5 left into the empty slot.
Input: board = [[1,2,3],[5,4,0]] Output: -1
Swapping just two tiles is impossible with sliding moves, so this board can never be solved.
board is 2 rows of 3 numbers.0 to 5, each exactly once.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #432 Slide the Tiles Into Place. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.