Boundless Boxes
Time limit1sMemory limit128 MB
Given a grid and up to 1000 seed cells, find the largest Chebyshev distance from any cell to its nearest seed, plus one.
- Level
Medium4 of 10
- Topics
- Geometry, Brute force, Array
- Solved
- No attempts yet
Problem
Remember the painter Peer from the 2008 ACM ICPC World Finals? Peer was one of the inventors of monochromy, meaning that each of his paintings uses a single color but in several different shades. He also favors simple geometric forms.
Several months ago, Peer painted triangles on a canvas from the outside in. Now that triangles are out and squares are in, his newest paintings use concentric squares drawn from the inside out! Peer starts with a rectangular canvas divided into a perfect square grid. He picks some single grid cells to act as central seeds and paints them with the darkest shade. From each seed he paints a larger square, one shade lighter, that encloses it, and keeps enclosing it with ever larger squares until the whole canvas is covered. Each square is exactly one grid cell larger, and one shade lighter, than the one it encloses. When squares overlap, the cell is always filled with the darker shade.

Figure 1: Example of one of Peer's most recent works, using six shades of color.
Equivalently, a cell's shade number is plus its Chebyshev distance to the nearest seed: if a cell lies at distance from its closest seed , its shade number is , with the darkest shade being . The number of shades required is the largest shade number that appears anywhere on the canvas.
Given the size of the canvas and the locations of the seeds, write a program that computes the number of shades needed for the painting.
Input
The input contains multiple test cases. Each test case begins with one line of three space-separated integers , , and . The canvas has exactly grid cells (); rows are numbered to vertically and columns to horizontally. The painting uses seed cells (), given on the next lines. Each of those lines contains two integers and (, ): the row and column of one seed cell. Every seed lies within the canvas.
A blank line separates consecutive test cases. A line containing 0 0 0 marks the end of the input and must not be processed.
Output
For each test case, print a single line containing one integer: the number of different shades required for the described painting.