#C11589. Minimum Moves to Target Drone
Minimum Moves to Target Drone
Minimum Moves to Target Drone
You are given a grid of altitudes with dimensions \(m \times n\) (i.e. \(m\) rows and \(n\) columns). A drone is initially located at cell \((s_x, s_y)\) and must reach the target cell \((t_x, t_y)\). The drone can move up, down, left, or right by one cell at a time. However, the drone can only move to a neighboring cell if the absolute difference in altitude between the current cell and the neighboring cell is at most \(1\). If the target is unreachable, output \(-1\).
Your task is to determine the minimum number of moves required for the drone to reach the target cell. The movement rule can be formalized as follows:
Let \(grid[i][j]\) denote the altitude at cell \((i,j)\). A move from cell \((i,j)\) to cell \((k,l)\) is valid if \( |grid[i][j] - grid[k][l]| \le 1 \).
If the starting cell is the same as the target cell, the answer is \(0\).
inputFormat
The input is read from standard input (stdin) and has the following format:
m n row1 (n integers separated by spaces) row2 ... row m s_x s_y t_x t_y
Here, \(m\) and \(n\) represent the number of rows and columns of the grid respectively. Each of the next \(m\) lines contains \(n\) integers representing the altitudes of the grid cells. Finally, two lines follow providing the starting coordinates \((s_x, s_y)\) and the target coordinates \((t_x, t_y)\) respectively.
outputFormat
The output should be printed to standard output (stdout) as a single integer: the minimum number of moves required for the drone to reach the target cell. If reaching the target is impossible, output \(-1\).
## sample4 5
1 2 2 3 4
2 3 3 4 5
3 3 3 3 2
4 5 6 5 6
0 0
3 4
7