Mine the Gradient
Time limit10sMemory limit128 MB
Given a grayscale grid, find the largest square subgrid whose values follow a vertical, horizontal, or diagonal uniform gradient, and report its area.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Implementation, Array, Matrix
- Solved
- No attempts yet
Problem
To prepare for a possible alien invasion, we first have to find out where the aliens might come from. One way to tell whether a planet is inhabited by intelligent creatures is to study high-resolution pictures of the planet and look for the tell-tale effects of intelligent life. Because there are so many candidate planets, this search has to be done by a computer program.
One distinctive feature of a colonized planet is the presence of surface mines. A mine is a square structure whose depth decreases uniformly from one side of the square toward the opposite side. On a grayscale image it therefore shows up as a square whose shade is either constant or changes gradually and uniformly from one side to another.
Your task is to find the largest mine on a grayscale bitmap of a planet. The picture is a rectangular grid of integers between and , where is the shade of grey of the pixel in row and column . Only square mines in a few special orientations are considered.
An axis-parallel square of side is the block of pixels with and .
Such a square is a mine if there exist integers and for which its shades follow one of the following four gradient patterns:
- row gradient (vertical mine): — the shade depends only on the row;
- column gradient (horizontal mine): — the shade depends only on the column;
- diagonal gradient: for some — the shade stays constant along one of the two diagonal directions.
A single pixel is always a (degenerate) mine.
Input
The input contains several pictures. Each picture begins with a line containing two integers and (), the height and the width of the picture. The next lines contain integers each: the -th integer on the -th of these lines is (), the shade of the pixel in row and column . Integers on a line are separated by at least one space; there may be extra spaces between the numbers and also at the beginning or the end of a line.
The list of pictures ends with a line containing two zeros, which is not a picture and must not be processed.
Output
For each picture, output a single line containing one integer: the area (the number of pixels) of the largest row-gradient, column-gradient, or diagonal mine in that picture.