Tigergraph | SWE | USA | PhoneScreen
Anonymous User
241

You are given a grid of size 3 x 3, and each cell of this grid has a number from 0 to 8. Which 0 represents the empty cell.
Input :
4 3 8
0 7 5
2 1 3

At any given time, you can slide a single cell which is adjacent to the empty cell to the position of the empty cell. A move is represented by a letter:
L: sliding a cell left to the position of the empty cell
R: sliding a cell right to the position of the empty cell
U: sliding a cell up to the position of the empty cell
D: sliding a cell down to the position of the empty cell

The goal of the game is to perform a sequence of moves so that the grid becomes sorted in ascending order. A follow-up requirement is to perform the least number of moves.

Return a string ans, where ans[i] should be a letter L, R, U, or D representing the moves.

The output should be string but Output State will be like this (Just for your reference)

0 1 2
3 4 5
6 7 8

Note: No Swapping allowed between non-empty cells, you have to use the empty cell (i.e 0) to make moves

Comments (1)