#K70682. Bytedreams Robotics: Minimum Moves in a Grid
Bytedreams Robotics: Minimum Moves in a Grid
Bytedreams Robotics: Minimum Moves in a Grid
This problem is a challenge from Bytedreams Robotics where you are tasked with helping a robot find the minimum number of moves required to navigate from the top-left cell to the bottom-right cell of a grid. The grid cells contain integers and the robot can move to any of the four neighboring cells (up, down, left, right), provided that the value of the destination cell is greater than or equal to the current cell's value.
The problem can be formally modeled by the recurrence relation:
(f(i,j) = \min_{(p,q) \in \text{neighbors}}{f(p,q) + 1}) with the constraint that (grid[p][q] \ge grid[i][j]).
Your solution should read input from standard input (stdin) and output the results to standard output (stdout).
inputFormat
The first line contains an integer (T), the number of test cases. For each test case, the first line contains two integers (n) and (m) representing the number of rows and columns of the grid. This is followed by (n) lines, each with (m) space-separated integers representing the grid values.
outputFormat
For each test case, output a single line with the minimum number of moves required to reach the bottom-right cell from the top-left cell. If no valid path exists, output -1.## sample
3
3 3
1 2 3
4 5 6
7 8 9
3 3
9 8 7
6 5 4
3 2 1
2 2
2 3
4 5
4
-1
2
</p>