#C1529. Treasure Hunt
Treasure Hunt
Treasure Hunt
You are given a grid representing a forest with n rows and m columns. Each cell in the grid is either a free cell denoted by '.' or an obstacle denoted by '#'. Starting at the top-left corner (0,0), you need to find the minimum number of moves required to reach a treasure located at position (tx, ty). You can move one step at a time in the four cardinal directions (up, down, left, right).
If either the start or the treasure cell is blocked, or the treasure is unreachable, output IMPOSSIBLE
.
The number of moves (if reachable) is defined as the minimum number of steps needed. Formally, if the solution takes moves steps, then \( moves \) is minimized.
inputFormat
The first line contains two integers, n and m (1 \(\leq n, m \leq 1000\)), representing the number of rows and columns in the grid, respectively.
The following n lines each contain a string of length m made up of '.' and '#' characters, representing the forest grid.
The last line contains two integers, tx and ty (0-indexed), indicating the position of the treasure in the grid.
outputFormat
If the treasure is reachable, output a single integer—the minimum number of moves required to reach the treasure from the starting position at (0, 0). Otherwise, output the string IMPOSSIBLE
(without quotes).
3 4
....
.#..
....
2 3
5