您的位置:首页 > 其它

HDU 1505 求最大子矩阵(二维)

2015-12-13 20:09 399 查看

City Game

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5914 Accepted Submission(s): 2534



[align=left]Problem Description[/align]
Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees,factories and buildings. There is still some space in
the area that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the
biggest possible building in each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in.

Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you're building stands is 3$.

Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied
units are marked with the symbol F.

[align=left]Input[/align]
The first line of the input contains an integer K – determining the number of datasets. Next lines contain the area descriptions. One description is defined in the following way: The first 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.

[align=left]Output[/align]
For each data set in the input print on a separate line, on the standard output, the integer that represents the profit obtained by erecting the largest building in the area encoded by the data set.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

45
0


[align=left]Source[/align]
Southeastern Europe 2004

题意:

在n*m的图中求面积最大,F是空地,R是不可用的地方。

此题就是在二维的图形上求子矩阵面积最大。类似1506

#include<bits/stdc++.h>
using namespace std;
char s[10];
int a[1020][1020];
int H[1020][1020];
int L[1020][1020];
int R[1020][1020];
int main()
{
int T,n,m;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
scanf("%s",s);      //建图
if(s[0]=='F')
a[i][j]=1;
else
a[i][j]=0;
}

memset(H,0,sizeof(H));
for(int i=n-1;i>=0;i--)   //统计每个位置上的高度
{
for(int j=0;j<m;j++)
{
if(a[i][j]==1)
H[i][j]=H[i+1][j]+1;
L[i][j]=R[i][j]=j;
}
}

int t,l,r,Max=0;
for(int i=0;i<n;i++)
{
for(int j=1;j<m;j++)       //从小到大,dp保存
{
int l=j-1;
while(H[i][j]<=H[i][l]&&L[i][l]>=0)   //找高度大于等于H  的最左下标
{
L[i][j]=L[i][l];
l=L[i][l]-1;
if(l<0)
break;
}
// cout<<L[i][j]<<endl;
}
for(int j=m-2;j>=0;j--)   //从大到小
{
int r=j+1;
while(H[i][j]<=H[i][r]&&R[i][r]<=m-1)    //右下标
{
R[i][j]=R[i][r];
r=R[i][r]+1;
if(r>m-1)
break;
}
}
int sum=0;
for(int j=0;j<m;j++)
{
sum=(R[i][j]-L[i][j]+1)*H[i][j];
Max=max(sum,Max);
// cout<<(R[i][j]-L[i][j]+1)*H[i][j]<<endl;
}
}
printf("%d\n",3*Max);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: