Petrozavodsk Winter Camp, Warsaw U, 2014, A The Carpet

时间:2024-10-30 16:03:44

一个地图上有若干障碍,问允许出现一个障碍的最大子矩形为多大?

最大子矩形改编

#include<bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
const int N = ;
int n, m, l[N][], r[N][], h[N][];
char s[N][N]; int main() {
scanf("%d%d", &n, &m);
rep(i, , n) scanf("%s", s[i] + );
int ans = ;
// 枚举障碍不在悬线上
// l[j][0]表示最左扩展(不能包含障碍),l[j][1]表示最右扩展(能包含障碍)
rep(j, , m) l[j][] = l[j][] = j, r[j][] = r[j][] = m + - j;
rep(i, , n) {
int lb0 = , lb1 = ;
rep(j, , m) {
if (s[i][j] == '.') {
h[j][] ++; ans = max(ans, h[j][]);
l[j][] = max(min(l[j][], j - lb0), min(l[j][], j - lb1));
l[j][] = min(l[j][], j - lb0);
}
else {
lb1 = lb0;
lb0 = j;
l[j][] = l[j][] = j;
h[j][] = ;
}
}
int rb0 = m + , rb1 = m + ;
dwn(j, m, ) {
if (s[i][j] == '.') {
r[j][] = max(min(r[j][], rb0 - j), min(r[j][], rb1 - j));
r[j][] = min(r[j][], rb0 - j);
ans = max(ans, (l[j][] + r[j][] - ) * h[j][]);
ans = max(ans, (l[j][] + r[j][] - ) * h[j][]);
ans = max(ans, (l[j][] + r[j][] - ) * h[j][]);
}
else {
rb1 = rb0;
rb0 = j;
r[j][] = r[j][] = m + - j;
}
}
}
// 枚举障碍在悬线上
// 这时候的l[j][0]表示没有障碍的悬线最左扩展,l[j][1]表示有障碍的悬线最左扩展
rep(j, , m) l[j][] = l[j][] = j, r[j][] = r[j][] = m + - j, h[j][] = h[j][] = ;
rep(i, , n) {
int lb = ;
rep(j, , m) {
if (s[i][j] == '.') {
h[j][] ++; h[j][] ++;
l[j][] = min(l[j][], j - lb);
l[j][] = min(l[j][], j - lb);
}
else {
l[j][] = min(l[j][], j - lb);
h[j][] = h[j][] + ; h[j][] = ;
l[j][] = j;
lb = j;
}
}
int rb = m + ;
dwn(j, m, ) {
if (s[i][j] == '.') {
r[j][] = min(r[j][], rb - j);
r[j][] = min(r[j][], rb - j);
}
else {
r[j][] = min(r[j][], rb - j);
r[j][] = m + - j;
rb = j;
}
}
rep(j, , m) {
ans = max(ans, h[j][] * (l[j][] + r[j][] - ));
}
}
cout << ans << '\n';
}
/*
4 5
#.#..
....#
..#..
....#
*/