Square dp

Meaning of the questions: to give you a 01 matrix, which now lets you find a square on the diagonal are all 1, the rest are all zero. Ask you this diagonal of the square is the largest of how much.

Ideas: a coordinate position in a matrix as the lower right corner of the square index as a state, dp [i] [j] expressed as i, j is the lower right corner of the square subject.

Then the square of the diagonal length of the three squares Release adjacent thereto, dp [i-1] [j], dp [i] [j-1], dp [i-1] [j-1], wherein the lowermost take the value +1.

This problem seems to be in the square often fuss with four points. .

#include<bits/stdc++.h>
using namespace std;

int n,m;
int s1[2509][2509],s2[2509][2509];
int dp[2509][2509];
int ans=0;
int a[2509][2509];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
    {
        scanf("%d",&a[i][j]);
        if(!a[i][j])
        {
            s1[i][j]=s1[i][j-1]+1;
            s2[i][j]=s2[i-1][j]+1;
        }
        else if(a[i][j])
        {
            dp[i][j]=min(min(s1[i][j-1],s2[i-1][j]),dp[i-1][j-1])+1;
            ans=max(ans,dp[i][j]);
        }

    }

    memset(s1,0,sizeof s1);
    memset(s2,0,sizeof s2);
    memset(dp,0,sizeof dp);

    for(int i=1;i<=n;i++)
     for(int j=m;j>=1;j--)
    {
        if(!a[i][j])
        {
            s1[i][j]=s1[i][j+1]+1;
            s2[i][j]=s2[i-1][j]+1;
        }
        else if(a[i][j])
        {
            dp[i][j]=min(min(s1[i][j+1],s2[i-1][j]),dp[i-1][j+1])+1;
            ans=max(ans,dp[i][j]);
        }
    }

    cout<<ans;

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/rainyskywx/p/11230217.html
DP