HDU_1505_矩阵中的最大矩形_dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1505

City Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6195    Accepted Submission(s): 2640


Problem Description
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.
 

 

Input
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.
 

 

Output
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.
 

 

Sample Input
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
 

 

Sample Output
45 0
 
做了1506后来看的这道加强版,虽然之前知道此题为1506的加强版,但还是没有想出做法,看了题解自己默写出来的。
此题为hdu1506的加强版,1506是给定一维上的高度求其中的最大的矩形,该题为给出矩阵求其中的的最大的矩形(有些单元有障碍),该题可转化为1506,只是多了一个维度,即对每一行都进行1506中的动态规划。为加深印象,在写一遍1506的思想,从左往右遍历i,找到i的左边界(只要左边的(t-1)的高度大于等于i的高度,即a[t-1][j]>=a[j][i],那么i的左边界就可以向左延伸),注意限制i(i>1),是为了避免边上的高度为0而循环无法结束而超时;右边界同理,最后再计算(r[j]-l[j]+1)*a[j][i],取所有中最大的。
在这之前还需要构造a[][]这个数组,详细见代码。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

char map[1005][1005];
int a[1005][1005];
int main()
{
    int l[1005],r[1005];
    int t,m,n;
    char ch;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i=1; i<=m; i++)
            for(int j=1; j<=n; j++)
                cin>>map[i][j];
        for(int i=1; i<=m; i++)
            for(int j=1; j<=n; j++)
            {
                if(map[i][j]=='F')
                    a[j][i]=a[j][i-1]+1;
                else
                    a[j][i]=0;
            }
            int maxn=0;
        for(int i=1; i<=m; i++)
        {
            l[1]=1;
            r[n]=n;
            for(int j=2; j<=n; j++)
            {
                int tt=j;
                while(a[tt-1][i]>=a[j][i]&&tt>1)
                    tt=l[tt-1];
                l[j]=tt;
            }
            for(int j=n-1;j>=1;j--)
            {
                int tt=j;
                while(a[tt+1][i]>=a[j][i]&&tt<n)
                    tt=r[tt+1];
                r[j]=tt;
            }
            for(int j=1;j<=n;j++)
                if(maxn<(r[j]-l[j]+1)*a[j][i])
                    maxn=(r[j]-l[j]+1)*a[j][i];
        }
        printf("%d\n",maxn*3);
    }
    return 0;
}
View Code

 

posted on 2016-04-22 20:10  JASONlee3  阅读(505)  评论(0编辑  收藏  举报

导航