12265 - Selling Land

来源:互联网 发布:古墓丽影 mac 编辑:程序博客网 时间:2024/05/05 21:12

As you may know, the country of Absurdistanis full of abnormalities. For example, the whole country can be divided intounit squares that are either grass or swamp. Also, the country is famous forits incapable bureaucrats. If you want to buy a piece of land (called aparcel), you can only buy a rectangular area, because they cannot handle othershapes. The price of the parcel is determined by them and is proportional tothe perimeter of the parcel, since the bureaucrats are unable to multiplyintegers and thus cannot calculate the area of the parcel.

Per owns a parcel in Absurdistan surroundedby swamp and he wants to sell it, possibly in parts, to some buyers. When hesells a rectangular part of his land, he is obliged to announce this to thelocal bureaucrats. They will first tell him the price he is supposed to sell itfor. Then they will write down the name of the new owner and the coordinates ofthe south-east corner of the parcel being sold. If somebody else already owns aparcel with a south-east corner at the same spot, the bureaucrats will deny thechange of ownership.

Per realizes that he can easily trick thesystem. He can sell overlapping areas, because bureaucrats only check whetherthe south-east corners are identical. However, nobody wants to buy a parcelcontaining swamp.

Figure 1: In this example, dark squaresrepresent swamp. Per may, for example, sell three overlapping grey areas, withdimensions 2×1, 2×4 and 4×1 respectively. The total perimeter is 6 + 12 +10=28. Note that he can get more money by selling even more land. This figurecorresponds to the case in the sample input.

Now Per would like to know how many parcelsof each perimeter he needs to sell in order to maximize his profit. Can youhelp him? You may assume that he can always find a buyer for each piece ofland, as long as it doesn't contain any swamps. Also, Per is sure that nosquare within his parcel is owned by somebody else.

Input

On the first line a positive integer: thenumber of test cases, at most 100. After that per test case:

  • One line with two integers n and m (1 ≤ n, m ≤ 1 000): the dimensions of Per's parcel.
  • n lines, each with m characters. Each character is either `#' or `.'. The j-th character on the i-th line is a `#' if position (i, j) is a swamp, and `.' if it is grass. The north-west corner of Per's parcel has coordinates (1, 1), and the south-east corner has coordinates (n,m).

Output

Per test case:

  • Zero or more lines containing a complete list of how many parcels of each perimeter Per needs to sell in order to maximize his profit. More specifically, if Per should sell piparcels of perimeter i in the optimal solution, output a single line "pi x i". The lines should be sorted in increasing order of i. No two lines should have the same value of i, and you should not output lines with pi=0.

Sample in- and output

Input

Output

1

6 5

..#.#

.#...

#..##

...#.

#....

#..#.

6 x 4

5 x 6

5 x 8

3 x 10

1 x 12

代码:


#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int maxn = 1000 + 10;

char s[maxn][maxn];

int height[maxn], ans[maxn*2];

struct Rect {

int c, h;

Rect(int c=0, int h=0):c(c),h(h){}

};

Rect rect[maxn]; // stack

int main() {

int n, m, T;

scanf("%d", &T);

while(T--) {

scanf("%d%d", &n, &m);

for(int i = 0; i < n; i ++) scanf("%s", s[i]);

memset(height, 0, sizeof(height));

memset(ans, 0, sizeof(ans));

for(int i = 0; i < n; i ++) {

int top = -1;

for(int j = 0; j < m; j ++) {

if(s[i][j] == '#') {

height[j] = 0;

top = -1; // clear stack

} else {

height[j]++;

Rect r(j, height[j]);

if(top < 0) rect[++top] = r;

else {

while(top >= 0 && r.h <= rect[top].h) r.c = rect[top--].c;

if(top < 0 || r.h - r.c > rect[top].h - rect[top].c) rect[++top] = r;

}

ans[j-rect[top].c+rect[top].h+1]++;

}

}

}

for(int i = 1; i <= n + m; i++)

if(ans[i]) printf("%d x %d\n", ans[i], i*2);

}

return 0;

}

 

0 0