Selling Land
Time limit1sMemory limit128 MB
For every grid cell (as a rectangle's bottom-right corner), find the maximum perimeter of an all-grass rectangle ending there, then output counts grouped by perimeter.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Array, Matrix
- Solved
- No attempts yet
Problem
The country of Absurdistan is divided into unit squares, each of which is either grass or swamp. Land can only be bought as a rectangular block whose sides are aligned with the grid, because the local bureaucrats cannot handle any other shape. The price of a block equals its perimeter: the bureaucrats cannot multiply, so they charge for the perimeter rather than the area.
Per owns one rectangular parcel and wants to sell it off, possibly in many overlapping pieces. Whenever he sells a rectangular block, the bureaucrats record only the coordinates of its south-east (bottom-right) corner. They refuse a sale if a block with the same south-east corner has already been sold. Otherwise overlapping blocks are allowed, so Per may sell one block for every distinct south-east corner. No buyer will accept a block that contains any swamp square, so every block he sells must consist entirely of grass.
To make as much money as possible, for each possible south-east corner Per sells the all-grass rectangular block that has that corner and the largest possible perimeter. For example, a block that is wide and tall has perimeter . Over all the blocks he sells, determine how many blocks he sells of each perimeter.
Input
The first line contains an integer (), the number of test cases. Each test case is given as follows:
- One line with two integers and (): the number of rows and columns of Per's parcel.
- lines follow, each containing characters. Each character is either
#(swamp) or.(grass). The character in row , column describes the square at position ; the north-west corner of the parcel is and the south-east corner is .
Output
For each test case, output zero or more lines describing how many blocks of each perimeter Per sells in the optimal plan. If he sells blocks of perimeter , print one line of the form count x perimeter (the count, a space, the letter x, a space, then the perimeter). Sort the lines by increasing perimeter , print no two lines with the same , and omit any perimeter for which .