hdu 1505(最大子矩阵)

City Game

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

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

题意:求‘F‘组成的最大子矩阵

题解:跟hdu1506一样,对每列分别求最大子矩阵。。但是死活WA,不写了。。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 1005;

int n,m;
char a[N][N];
int mp[N][N];
int L[N],R[N];
void input(){
     memset(mp,0,sizeof(mp));
     for(int i=1;i<=n;i++){
            gets(a[i]);
            for(int j=1;j<=m;j++){
                if(i==1) {     ///预处理mp
                    if(a[i][(j-1)*2]==‘R‘) mp[i][j]=0;
                    if(a[i][(j-1)*2] ==‘F‘) mp[i][j]=1;
                }else{
                    if(a[i-1][(j-1)*2]==‘R‘) {
                        if(a[i][(j-1)*2] ==‘R‘) mp[i][j]=0;
                        if(a[i][(j-1)*2] ==‘F‘) mp[i][j]=1;
                    }
                    else {
                        if(a[i][(j-1)*2] ==‘R‘) mp[i][j]=0;
                        if(a[i][(j-1)*2] ==‘F‘) mp[i][j]+=mp[i-1][j]+1;
                    }
                }
            }
        }
    /*for(int i=1;i<=n;i++){ //test
        for(int j=1;j<=m;j++){
            printf("%d ",mp[i][j]);
        }
        printf("\n");
    }*/
}
int main()
{
     int tcase;
     scanf("%d",&tcase);
     while(tcase--){
        scanf("%d%d",&n,&m);
        getchar();
        input();
        int mx = -1;
        for(int k=1;k<=n;k++){
            L[1]=1;
            R[m]=m;
            for(int i=2;i<=m;i++){ ///向右延伸
                int t = i;
                if(t>1&&mp[k][i]<=mp[k][t-1]) t = L[t-1];
                L[i] =t;
            }
            for(int i=m-1;i>=1;i--){
                int t = i;
                if(t<m&&mp[k][i]<=mp[k][t+1]) t = R[t+1];
                R[i] = t;
            }
            for(int i=1;i<=m;i++){
                if((R[i]-L[i]+1)*mp[k][i]>mx)  mx = (R[i]-L[i]+1)*mp[k][i];
            }
        }
        printf("%d\n",mx*3);
     }

}
时间: 2024-08-01 10:46:46

hdu 1505(最大子矩阵)的相关文章

HDU 1505 City Game (hdu1506 dp二维加强版)

F - City Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1505 Appoint description: Description Bob is a strategy game programming specialist. In his new city building game the gaming enviro

hdu 1505

City Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4516    Accepted Submission(s): 1909 Problem Description Bob is a strategy game programming specialist. In his new city building game th

HDU 1505 Largest Rectangle in a Histogram &amp;&amp; HDU 1506 City Game(动态规划)

1506题意:给你连续的直方图(底边边长为1),求连续的矩阵面积. 对每个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <sta

hdu 1081最大子矩阵的和DP

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8210    Accepted Submission(s): 3991 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectan

hdu 1559最大子矩阵

一直很少练dp~这几天再学学~~ 在本题中:a[i][j]的值表示左上角为(1,1)右下角为(i,j)的矩阵的所有元素之和~ 给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大. Input 输入数据的第一行为一个正整数T,表示有T组测试数据.每一组测试数据的第一行为四个正整数m,n,x,y(0<m,n<1000 AND 0<x<=m AND 0<y<=n),表示给定的矩形有m行n列.接下来这个矩阵,有m行,每行有n个不大于1000的正整数

HDU 1505 City Game-dp-(最大子矩阵模型)

题意:求最大的子矩阵 分析:直接用最大字矩阵的公式做超时了.换个思路,这题跟上一题1506有关系,先以每层为底算出每个元素能到达的最大的高度,然后就跟1506一样了.这里求高度和求面积两处地方用到了dp暂存数据.求高度用二重循环,然后每层为底求面积要二重循环加上外层就是三重循环,但是由于用了dp保存中间结果,所以这个三重循环不会超时.dp[j]表示当前层第j列能到达的最大的高度,状态转移:1.a[i][j]=='R'时,dp[j]=0:2.否则,若a[i-1][j]=='F',则dp[j]++(

hdu 1559(最大子矩阵)

最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3906    Accepted Submission(s): 1994 Problem Description 给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大. Input 输 入数据的第一行为一个正整数T,表示有T组测试数据.每一组测试数

HDU 1559 最大子矩阵 (DP)

题目地址:HDU 1559 构造二维前缀和矩阵.即矩阵上的点a[i][j]表示左上方的点为(0,0),右下方的点为(i,j)的矩阵的和.然后枚举每个矩阵的左上方的点,由于矩阵的长和宽是固定的,那么这个矩阵实际上也已经固定了.此时这个矩阵的和用公式: sum=a[i+x-1][j+y-1]-a[i+x-1][j-1]-a[i-1][j+y-1]+a[i-1][j-1]; 取最大值就可以了. 代码如下: #include <iostream> #include <cstdio> #in

HDU 1505 City Game

题意就是求最大子矩阵. 白书上的例题. 如果暴力枚举 左上角,然后长和宽.时间复杂度为O(m^3*n^3). 可以定义up[][] 为某个格子最大高度, 定义 left[][]为某个格子左扫描最大. 定义 right[][]为右扫描的最大. 最后乘起来. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include&