Write a program that, given a map with treasures and obstacles, computes the distance from a given initial position to the second furthest accessible treasure. The allowed movements are horizontal or vertical, but not diagonal. If needed, passing over the treasures is allowed.
Input begins with the number of rows
and the number of columns
of the map. Follow
rows with
characters each. A dot indicates an empty position, an ‘X’
indicates an obstacle, and a ‘t’ indicates a treasure.
Finally, two numbers
and
indicate the initial row and column (both of them starting at 1) where
we must start looking for treasures. You can assume that
is between 1 and
,
that
is between 1 and
,
and that the initial position is always empty.
Print the minimum number of steps to reach the second furthest treasure from the initial position. If no treasure is accessible, tell so.
Input
7 6 ..t... ..XXX. ...... tX..X. .X..Xt .XX... ..t... 5 3
Output
second maximum distance: 5
Input
4 10 ..t...X... .....X..t. XXXXX.X... t......X.t 4 3
Output
we cannot reach two or more treasures
Input
5 7 ....... .XXXXXt .X...Xt .X.X.XX ...X.Xt 5 5
Output
second maximum distance: 19
Input
1 3 t.t 1 2
Output
second maximum distance: 1