Safest Path
View as PDFYou find yourself on a grid with rows numbered from
to
and
columns numbered from
to
, with
cells having enemies. You are allowed to move within the grid by going directly up, down, left, or right to an adjacent cell. You would like to find the safest path from the top-left cell (where you begin) to the bottom-right of the grid. The safest path is any path such that the closest you ever end up to any of the monsters is as far as possible. The distance between your current position on the grid and a the cell of a monster is the difference in the row numbers added to the difference in the column numbers.
How close do you end up to any monster on any of the safest paths?
Input Specification
The first line contains spaced integers,
,
, and
.
The next lines contains two spaced integers,
and
, indicating the row and column of a cell that has a monster.
Output Specification
On a single line, output the highest possible closest distance that you end up reaching to any monster on any path.
Sample Input 1
4 4 2
0 3
3 0
Output for Sample Input 1
2
Explanation of Output for Sample Input 1
One of the safest paths can be seen below, where is a cell with a monster and
is an empty cell. On this path, the closest you end up to a monster is
units away.

Sample Input 2
3 3 1
0 2
Output for Sample Input 2
2
Explanation of Output for Sample Input 2
One of the safest paths can be seen below. On this path, the closest you end up to a monster is units away.

Sample Input 3
3 3 2
0 0
2 2
Output for Sample Input 2
0
Explanation of Output for Sample Input 2
No matter which path you go through, you go through the first cell, which already contains a monster. Thus, the you will always end up at least away from any monster cell.

Comments