POJ1964 City Game(最大子矩阵)

来源:互联网 发布:电子竞技综合数据分析 编辑:程序博客网 时间:2024/06/05 18:05
                                                                                     City Game
Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 2590 Accepted: 984

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

25 6R F F F F FF F F F F FR R R F F FF F F F F FF F F F F F5 5R R R R RR R R R RR R R R RR R R R RR R R R R

Sample Output

450

Source

Southeastern Europe 2004
这题一定要注意,要用cin,不然会出错!!做完这题是比较兴奋的,这是学DP来做的第一个还比较难的,也是以前还没有搞懂的一个题目类型对于我来说是值得庆贺的。在组这题的时候看过一个大牛写的程序,但是很遗憾的是我没有看懂,(在这了鄙视自己,水平实在太差了。)最后没办法自己动手,有着那一秒的顿悟,找到了一种方法,我是这样的想的,
***********************************************
/Mh[]表示该列的连续的高度
/left1[]保存该列左边从i-1起连续不大于i列的列数
/right1[]保存该列左边从i+1起连续不大于i列的列数
/以i列为中心的面积为(left1[i]+right1[i]-1)*Mh[i]。
***********************************************
下面是我AC程序:532ms
#include<iostream>
#include<cstring>
using namespace std;
int Mh[1001],left1[1001],right1[1001],n,m;
int ans;
char s;
void slove()
{
     int i,j,best;
     //Mh[0]=Mh[m+1]=
     for(i=1;i<=m;i++)
     {
         if(Mh[i]==0) continue;
         j=i-1;
         //for(j=i-1;j>0;j-=left1[j])
         while(j>0){
              if(Mh[i]<=Mh[j])
              {
                   left1[i]+=left1[j];j=j-left1[j];
              }
              else  break;
             
         }
     }
     for(i=1;i<=m;i++)
     {
         if(Mh[i]==0) continue;
         for(j=i+1;j<=m;j+=right1[j])
         {
              if(Mh[i]<=Mh[j])
              {
                   right1[i]+=right1[j];
              }
              else  break;
         }        
     }
     for(i=1;i<=m;i++)
     {
          //if(left1[i]==0&&right1[i]==0)
               //continue;
          best=(left1[i]+right1[i]-1)*Mh[i];
          if(best>ans)  ans=best;
     }
}                                         
int main()
{
    int ncase,i,j;
    cin>>ncase;
    while(ncase--)
    {
         cin>>n>>m;
         memset(Mh,0,sizeof(Mh));
         //memset(left1,1,sizeof(left1));
         //memset(right1,1,sizeof(right1));
         for(i=1;i<=m;i++)  left1[i]=0;
         for(i=1;i<=m;i++)  right1[i]=0;
         ans=0;
         for(i=1;i<=n;i++)
         {
             for(j=1;j<=m;j++)
             {
                  cin>>s;
                  if(s=='F')
                  {
                       Mh[j]++;
                       left1[j]=right1[j]=1;
                  }
                  else
                  { 
                       Mh[j]=0;
                       left1[j]=right1[j]=0;
                  }
             }
             slove();
             //for(j=1;j<=m;j++)
                 //cout<<left1[j]<<" "<<right1[j]<<endl;
         }
         cout<<ans*3<<endl;
    }
    return 0;
}
原创粉丝点击