UVA - 1330 City Game

Input
The rst line of the input le contains an integer K | determining the number of datasets. Next lines
contain the area descriptions. One description is de ned in the following way: The rst 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 le print on a separate line, on the standard output, the integer that
represents the pro t 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.颠倒看数组都是一样的

#include <cstdio>
#include <algorithm>
#include <iostream>

#define MAX 1000

using namespace std;
int A[MAX][MAX], up[MAX][MAX], l[MAX][MAX], r[MAX][MAX], m, n, T;

int main() {
    cin >> T;
    while (T--) {
        cin >> m >> n;
        for (int i = 0, c; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                do { c = getchar(); }
                while (c != 'F' && c != 'R');
                A[i][j] = c == 'F';
            }
        }
        int ans = 0;
        for (int i = 0; i < m; ++i) {
            for (int j = 0, lo = 0; j < n; ++j) {
                if (!A[i][j]) {
                    up[i][j] = l[i][j] = 0; //把l设到最左,方便上方的点求lo
                    lo = j + 1;
                } else {
                    up[i][j] = i == 0 ? 1 : up[i - 1][j] + 1;
                    l[i][j] = i == 0 ? lo : max(l[i - 1][j], lo);
                }
            }

            for (int j = n - 1, ro = n; j >= 0; --j) {
                if (!A[i][j]) {
                    r[i][j] = n, ro = j;
                } else {
                    r[i][j] = i == 0 ? ro : min(r[i - 1][j], ro);
                    ans = max(ans, up[i][j] * (r[i][j] - l[i][j])); //在求右边界时同时求矩形面积最大值
                }
            }
        }
        cout << ans * 3 << endl;
    }
}

 

posted @ 2017-10-15 12:02  少年啦飞驰  阅读(122)  评论(0编辑  收藏  举报