#K86767. Minimum Moves to Find Hider
Minimum Moves to Find Hider
Minimum Moves to Find Hider
You are given a grid with n
rows and m
columns where each cell is either walkable ('.') or blocked by an obstacle ('#'). A seeker starts at position (sr, sc)
and needs to reach the hider located at position (hr, hc)
. In one move, the seeker can move one step up, down, left, or right, but cannot move into blocked cells or outside the grid.
Your task is to determine the minimum number of moves required for the seeker to reach the hider. If the hider cannot be reached, output -1
.
The moves satisfy the following LaTeX formula:
\(moves = \min \{ d((sr, sc),(hr, hc)) \}\)
where d((sr, sc),(hr, hc))
is the Manhattan distance along a valid path that does not cross obstacles.
inputFormat
The input is given from standard input (stdin) in the following format:
n m s1 s2 ... sn sr sc hr hc
Here, the first line contains two integers n
and m
representing the number of rows and columns of the grid. The next n
lines each contain a string of length m
representing a row of the grid (using '.' for walkable cells and '#' for obstacles). The final line contains four integers: sr
, sc
(the starting row and column of the seeker) and hr
, hc
(the row and column of the hider). Rows and columns are 0-indexed.
outputFormat
Output a single integer to standard output (stdout): the minimum number of moves required for the seeker to reach the hider. If the hider cannot be reached, output -1
.
5 7
.......
.#.#.#.
.......
.#####.
.......
0 0 4 6
10