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=;
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=;i<=n;i++)
scanf("%s",a[i]+);
for(j=;j<=m;j++)
for(i=;i<=n;i++)
if(a[i][j]=='X') b[i][j]=b[i-][j]+;else b[i][j]=b[i-][j];
for(i=;i<=n;i++)
for(j=i;j<=n;j++)
{
x=;y=;
for(k=;k<=m;k++)
if(b[j][k]-b[i-][k]==)
{
x=max(x,k);
y=x;
while(x<m&&a[i][x+]=='.'&&a[j][x+]=='.')
{
x++;
if(b[j][x]-b[i-][x]==) y=x;
}
ans=max(ans,(j-i+)*(y-k+));
}
}
cout<<ans;
return ;
}