bzoj 4506: [Usaco2016 Jan]Fort Moo
4506: [Usaco2016 Jan]Fort Moo
Description
Bessie is building a fort with her friend Elsie. Like any good fort, this one needs to start with a sturdy frame. Bessie wants to build a frame in the shape of a one-meter-wide rectangular outline, atop which she will build the fort.
Bessie has already chosen a site on which to build the fort -- a piece of land measuring NN meters by MM meters (1< = N,M< = 2001< = N,M< = 200). Unfortunately, the site has some swampy areas that cannot be used to support the frame. Please help Bessie determine the largest area she can cover with her fort (the area of the rectangle supported by the frame), such that the frame avoids sitting on any of the swampy areas.
Input
Line 1 contains integers N and M.
The next N lines each contain M characters, forming a grid describing the site. A character of '.' represents normal grass, while 'X' represents a swampy spot.
Output
A single integer representing the maximum area that Bessie can cover with her fort.
Sample Input
5 6
......
..X..X
X..X..
......
..X...
......
..X..X
X..X..
......
..X...
Sample Output
16
In the example, the placement of the optimal frame is indicated by 'f's below:
.ffff.
.fX.fX
Xf.Xf.
.ffff.
..X...
In the example, the placement of the optimal frame is indicated by 'f's below:
.ffff.
.fX.fX
Xf.Xf.
.ffff.
..X...
题解:
题目大意是找到一个最大的矩形使得矩形的外框均在不在沼泽上。
o(n4)的暴力我就不说了,也许会卡过。
讲一讲n3吧,我们只需枚举矩形的上下边界,然后用n的时间扫一遍。
#include<stdio.h> #include<iostream> using namespace std; const int N=205; char a[N][N]; int n,m,i,j,k,x,y,ans,b[N][N]; int main() { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%s",a[i]+1); for(j=1;j<=m;j++) for(i=1;i<=n;i++) if(a[i][j]=='X') b[i][j]=b[i-1][j]+1;else b[i][j]=b[i-1][j]; for(i=1;i<=n;i++) for(j=i;j<=n;j++) { x=0;y=0; for(k=1;k<=m;k++) if(b[j][k]-b[i-1][k]==0) { x=max(x,k); y=x; while(x<m&&a[i][x+1]=='.'&&a[j][x+1]=='.') { x++; if(b[j][x]-b[i-1][x]==0) y=x; } ans=max(ans,(j-i+1)*(y-k+1)); } } cout<<ans; return 0; }
一念起,天涯咫尺; 一念灭,咫尺天涯。