#K45267. Minimum Moves on Grid
Minimum Moves on Grid
Minimum Moves on Grid
This problem asks you to compute the minimum number of moves required for a robot to travel from a starting position to a destination in a 2D grid.
The grid is composed of open spaces represented by a dot ('.') and walls represented by a pound sign ('#'). The robot can move in four directions (up, down, left, right) but cannot pass through walls.
If the destination is unreachable, the program should output -1
. The movement cost for each valid move is assumed to be 1.
Note: All formulas, if any, are written in LaTeX format. For example, the distance can be defined as \( d = d + 1 \) for each move.
inputFormat
The first line contains two integers \(n\) and \(m\) -- the number of rows and columns of the grid, respectively.
The next \(n\) lines each contain a string of length \(m\) representing the grid. Each character is either '.' (an open space) or '#' (a wall).
The last line contains four integers: \(sx\), \(sy\), \(dx\), \(dy\) representing the starting coordinates and the destination coordinates, respectively. The coordinates are \(0\)-indexed.
outputFormat
Output a single integer: the minimum number of moves required for the robot to reach the destination using only valid moves. If the destination cannot be reached, output -1
.
5 5
.....
.#.#.
..#..
.#.#.
.....
0 0 4 4
8