#C6516. Minimum Moves to Treasure
Minimum Moves to Treasure
Minimum Moves to Treasure
You are given a grid of n rows and m columns where each cell is represented by a character. A cell with #
represents an obstacle, while any other character represents a free cell. You are also given two pairs of coordinates: the starting position and the treasure position. Your task is to calculate the minimum number of moves required to reach the treasure from the starting position by moving only up, down, left, or right. Each move counts as one step. If the treasure is unreachable, output -1
.
Note: The moves are calculated on a 0-indexed grid. The input coordinates may not correspond to a special character in the grid; they are provided separately. The formula for a move is given by:
where \(dx, dy\) belongs to \(\{(-1,0), (1,0), (0,-1), (0,1)\}\).
inputFormat
The first line contains two integers n and m representing the number of rows and columns in the grid.
The next n lines each contain a string of length m representing a row of the grid.
The last line contains four integers: xstart, ystart, xtreasure, and ytreasure, which are the coordinates of the starting position and the treasure respectively (0-indexed).
outputFormat
Output a single integer: the minimum number of moves required to reach the treasure. If the treasure cannot be reached, output -1
.
5 6
......
..###.
....#.
.#....
..P.T.
4 2 4 4
2
</p>