悬线法(符合条件最大子矩阵) dp

学习链接:浅谈用极大化思想解决最大子矩阵问题

模板:

 for(i=1;i<=n;i++){//初始化 
  for(j=1;j<=m;j++){
   Right[i][j]=j;
   Left[i][j]=j;
   height[i][j]=1;
  }
 }
 for(i=1;i<=n;i++){//转移left数组 
  for(j=2;j<=m;j++){
   if(a[i][j]+a[i][j-1]==1)//如果满足条件,即a[i][j]这个点不是阻碍点 
            Left[i][j]=Left[i][j-1];
  }
 }
 for(i=1;i<=n;i++){//转移right数组 
  for(j=m-1;j>=1;j--){
   if(a[i][j]+a[i][j+1]==1)//如果满足条件 
            Right[i][j]=Right[i][j+1];
  }
 }
 int ans=0;
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   if(i>1&&a[i][j]+a[i-1][j]==1){//如果满足条件 
    height[i][j]=height[i-1][j]+1;
    Left[i][j]=max(Left[i-1][j],Left[i][j]);
    Right[i][j]=min(Right[i-1][j],Right[i][j]);
   }
   s=(Right[i][j]-Left[i][j]+1)*height[i][j];//矩阵面积 
   ans=max(ans,s);//寻找满足条件最大矩阵 
  }
 }

P1169 [ZJOI2007]棋盘制作

#include<iostream>
#include<cstdio>
using namespace std;
int height[2010][2010],Left[2010][2010],Right[2010][2010];
int a[2010][2010];
int main(void){
 int n,m,i,j;
 scanf("%d%d",&n,&m);
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   scanf("%d",&a[i][j]);
  }
 }
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   Right[i][j]=j;
   Left[i][j]=j;
   height[i][j]=1;
  }
 }
 for(i=1;i<=n;i++){
  for(j=2;j<=m;j++){
   if(a[i][j]+a[i][j-1]==1)
            Left[i][j]=Left[i][j-1];
  }
 }
 for(i=1;i<=n;i++){
  for(j=m-1;j>=1;j--){
   if(a[i][j]+a[i][j+1]==1)
            Right[i][j]=Right[i][j+1];
  }
 }
 int max1=0,max2=0;
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   if(i>1&&a[i][j]+a[i-1][j]==1){
    height[i][j]=height[i-1][j]+1;
    Left[i][j]=max(Left[i-1][j],Left[i][j]);
    Right[i][j]=min(Right[i-1][j],Right[i][j]);
   }
   int len=Right[i][j]-Left[i][j]+1;
   int s=min(len,height[i][j]);
   max1=max(max1,s*s);
   max2=max(max2,len*height[i][j]);
  }
 }
 cout<<max1<<"\n"<<max2<<"\n";
 return 0;
}

P1387 最大正方形

#include<iostream>
#include<cstdio>
using namespace std;
int height[2010][2010],Left[2010][2010],Right[2010][2010];
int a[2010][2010];
int main(void){
 int n,m,i,j;
 scanf("%d%d",&n,&m);
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   scanf("%d",&a[i][j]);
  }
 }
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   Right[i][j]=j;
   Left[i][j]=j;
   height[i][j]=1;
  }
 }
 for(i=1;i<=n;i++){
  for(j=2;j<=m;j++){
   if(a[i][j-1]!=0)
            Left[i][j]=Left[i][j-1];
  }
 }
 for(i=1;i<=n;i++){
  for(j=m-1;j>=1;j--){
   if(a[i][j+1]!=0)
            Right[i][j]=Right[i][j+1];
  }
 }
 int ans=0;
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   if(i>1&&a[i-1][j]!=0){
    height[i][j]=height[i-1][j]+1;
    Left[i][j]=max(Left[i-1][j],Left[i][j]);
    Right[i][j]=min(Right[i-1][j],Right[i][j]);
   }
   if(a[i][j]==0)
   continue;
   int len=Right[i][j]-Left[i][j]+1;
   //cout<<Right[i][j]<<" "<<Left[i][j]<<"  len\n";
   int s=min(len,height[i][j]);
   ans=max(ans,s);
  }
 }
 cout<<ans<<"\n";
 return 0;
}

P2701 [USACO5.3]巨大的牛棚Big Barn

#include<iostream>
#include<cstdio>
using namespace std;
int height[2010][2010],Left[2010][2010],Right[2010][2010];
int a[2010][2010];
int main(void){
 int n,m,i,j,t,x,y;
 scanf("%d%d",&n,&t);
 m=n;
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   a[i][j]=1;
  }
 }
 for(i=0;i<t;i++){
        scanf("%d%d",&x,&y);
        a[x][y]=0;
 }
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   Right[i][j]=j;
   Left[i][j]=j;
   height[i][j]=1;
  }
 }
 for(i=1;i<=n;i++){
  for(j=2;j<=m;j++){
   if(a[i][j-1]!=0)
            Left[i][j]=Left[i][j-1];
  }
 }
 for(i=1;i<=n;i++){
  for(j=m-1;j>=1;j--){
   if(a[i][j+1]!=0)
            Right[i][j]=Right[i][j+1];
  }
 }
 int ans=0;
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   if(i>1&&a[i-1][j]!=0){
    height[i][j]=height[i-1][j]+1;
    Left[i][j]=max(Left[i-1][j],Left[i][j]);
    Right[i][j]=min(Right[i-1][j],Right[i][j]);
   }
   if(a[i][j]==0)
   continue;
   int len=Right[i][j]-Left[i][j]+1;
   int s=min(len,height[i][j]);
   ans=max(ans,s);
  }
 }
 cout<<ans<<"\n";
 return 0;
}

P4147 玉蟾宫

#include<iostream>
#include<cstdio>
using namespace std;
int height[2010][2010],Left[2010][2010],Right[2010][2010];
char a[2010][2010];
int main(void){
 int n,m,i,j,t,x,y;
 scanf("%d%d",&n,&m);
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
            cin>>a[i][j];
  }
 }
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   Right[i][j]=j;
   Left[i][j]=j;
   height[i][j]=1;
  }
 }
 for(i=1;i<=n;i++){
  for(j=2;j<=m;j++){
   if(a[i][j-1]=='F')
            Left[i][j]=Left[i][j-1];
  }
 }
 for(i=1;i<=n;i++){
  for(j=m-1;j>=1;j--){
   if(a[i][j+1]=='F')
            Right[i][j]=Right[i][j+1];
  }
 }
 int ans=0;
 for(i=1;i<=n;i++){
  for(j=1;j<=m;j++){
   if(i>1&&a[i-1][j]=='F'){
    height[i][j]=height[i-1][j]+1;
    Left[i][j]=max(Left[i-1][j],Left[i][j]);
    Right[i][j]=min(Right[i-1][j],Right[i][j]);
   }
   if(a[i][j]!='F')
   continue;
   int len=Right[i][j]-Left[i][j]+1;
   //int s=min(len,height[i][j]);
   int s=len*height[i][j];
   ans=max(ans,s);
  }
 }
 cout<<3*ans<<"\n";
 return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值