CQUOJ 1481 - City Game

Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees,factories and buildings. There is still some space in the area that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in.

Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you're building stands is 3$.

Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F.
 

Input

The first line of the input contains an integer K – determining the number of datasets. Next lines contain the area descriptions. One description is defined in the following way: The first line contains two integers-area length M<=1000 and width N<=1000, separated by a blank space. The next M lines contain N symbols that mark the reserved or free grid units,separated by a blank space. The symbols used are:

R – reserved unit

F – free unit

In the end of each area description there is a separating line.
 

Output

For each data set in the input print on a separate line, on the standard output, the integer that represents the profit obtained by erecting the largest building in the area encoded by the data set.
 

Sample Input

2
5 6
R F F F F F
F F F F F F
R R R F F F
F F F F F F
F F F F F F

5 5
R R R R R
R R R R R
R R R R R
R R R R R
R R R R R

Sample Output

45
0

 

 1 /*
 2 2016年4月23日20:45:39
 3 
 4 这题是HDU 1506 的加强版,
 5         定义一个二维数组,
 6         H[i][j]表示第i行j列元素在前i行中的最大高度。
 7 (以第一行为底)例如测试样例:
 8 0 1 1 1 1 1                                     0 1 1 1 1 1
 9 1 1 1 1 1 1           (F=1,R=0,方便求和)       1 2 2 2 2 2
10 0 0 0 1 1 1           转化完就是右边矩阵        0 0 0 3 3 3
11 1 1 1 1 1 1                                     1 1 1 4 4 4
12 1 1 1 1 1 1                                     2 2 2 5 5 5
13    接下来就和 HDU 1506 一样了,逐行求最大矩形面积。
14 动态方程:l[j] r[j]分别表示左边界与右边界。(见上一篇:HDU 1506 Largest Rectangle in a Histogram)
15  l[j]=l[l[j]-1];
16  r[j]=r[r[j]-1]; 
17 
18  PS:  在这里有关于字符串的输入, 如果用scanf输入用超级麻烦, 所以建议用cin输入 
19 */
20 
21 # include <iostream>
22 # include <cstdio>
23 # include <cstring>
24 # include <algorithm>
25 # include <queue>
26 # include <vector>
27 # include <cmath>
28 # define INF 0x3f3f3f3f
29 using namespace std;
30 const int N = 1005;
31 
32 int L[N], R[N];
33 int H[N][N];
34 
35 int main(void)
36 {
37     int tes, n, m, i, j, t, ans;
38     char ch;
39     scanf("%d", &tes);
40     while (tes--)
41     {
42         scanf("%d %d", &n, &m);
43         memset(H, 0, sizeof(H));
44         getchar();
45         for (i = 1; i<= n; i++){
46             for (j = 1; j <= m; j++){
47                 cin>>ch; // 以后输入字符串的 就用cin 吧 
48                 if (ch =='F')                    
49                     H[i][j] = H[i-1][j] + 1;
50                 else H[i][j] = 0;
51             }
52         }
53         ans = -1;
54         for (i = 1; i <= n; i++){
55             /*L[1] = 1;
56             for (j = 2; j <= m; j++){
57                 if (H[i][j] == 0)
58                     continue;
59                 t = j;
60                 while (t>1 && H[i][j] <= H[i][t-1])
61                     t = L[t-1];
62                 L[j] = t;
63             }
64             R[m] = m;
65             for (j = m-1; j >= 1; j--){
66                 if (H[i][j] == 0)
67                     continue;
68                 t = j;
69                 while (t<m && H[i][j] <= H[i][t+1])
70                     t = R[t+1];
71                 R[j] = t;
72             }               以上为另一种解法   */
73             for (j = 1; j <= m; j++)
74                 L[j] = R[j] = j;
75             H[i][0] = H[i][m+1] = -1; // 相当于下面while的终止条件 
76             for (j = 2; j <= m; j++){
77                 while(H[i][j] <= H[i][L[j]-1])
78                     L[j] = L[L[j]-1]; 
79             }
80             for (j = m-1; j >= 1; j--){
81                 while (H[i][j] <= H[i][R[j]+1])    
82                     R[j] = R[R[j]+1];
83             }
84             for (j = 1; j <= m; j++){
85                 int len = R[j] - L[j] + 1;
86                 ans = max(ans, len*H[i][j]);
87             }
88         }
89         printf("%d\n", 3*ans);
90     }
91     
92     return 0;    
93 }

 

posted @ 2016-04-24 00:08  昵称还没有想归一  阅读(101)  评论(0)    收藏  举报