Safest Path

View as PDF

Submit solution

Points: 10
Time limit: 3.0s
Memory limit: 256M

Author:
Problem type

You find yourself on a grid with R rows numbered from 0 to R-1 and C columns numbered from 0 to C-1, with K 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 3 spaced integers, R, C (1 \le R,C \le 10^3), and K (1 \le R \times C).

The next K lines contains two spaced integers, r_i (0 \le r_i \le R - 1) and c_i (0 \le c_i \le C - 1), 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 1 is a cell with a monster and 0 is an empty cell. On this path, the closest you end up to a monster is 2 units away.

sample diagram 1

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 2 units away.

sample diagram 2

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 0 away from any monster cell.

sample diagram 2


Comments

There are no comments at the moment.