UVaLive 3695 City Game (扫描线)

题意:给定m*n的矩阵,有的是空地有的是墙,找出一个面积最大的子矩阵。

析:如果暴力,一定会超时的。我们可以使用扫描线,up[i][j] 表示从(i, j)向上可以到达的最高高度,left[i][j]表示(i, j) 的左边界,right[i][j]右边界。

这三个可以用递推来实现。从向下扫描,每次更新最大值。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
bool a[maxn][maxn];
int up[maxn][maxn], l[maxn][maxn], r[maxn][maxn];

int main(){
    int T;  cin >> T;
    while(T--){
        scanf("%d %d", &n, &m);
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < m; ++j){
                int ch = getchar();
                while(ch != 'F' && ch != 'R')  ch = getchar();
                a[i][j] = ch == 'F' ? false : true;
            }

        int ans = 0;
        for(int i = 0; i < n; ++i){
            int lo = -1, ro = m;
            for(int j = 0; j < m; ++j)
                if(a[i][j]) {  up[i][j] = l[i][j] = 0; lo = j; }
                else{
                    up[i][j] = i ? up[i-1][j] + 1 : 1;
                    l[i][j] = i ? max(l[i-1][j], lo + 1) : lo + 1;
                }
            for(int j = m-1; j >= 0; --j)
                if(a[i][j])   r[i][j] = n, ro = j;
                else{
                    r[i][j] = i ? min(r[i-1][j], ro-1) : ro - 1;
                    ans = max(ans, up[i][j] * (r[i][j] - l[i][j] + 1));
                }
        }
        printf("%d
", ans * 3);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/6525303.html