L - Largest Allowed Area

L - Largest Allowed Area

Topic link: http://codeforces.com/gym/102091/attachments

The matrix given to a n*m is only 1 and 0. In this matrix, a square is contained in the matrix containing 1 at most, and the square with the largest circle can be asked.

Train of thought: using two dimensional prefix and maintaining the upper left corner and (sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j]), traversing all the points, making the traversed points as the top left corner of the square, and then traversing the length, conforming to the condition of length plus 1, which does not meet the requirement of direct break point. Length only increases.

Note: Don't read T fast.

Code:

#include <bits/stdc++.h>

using namespace std;

int a[1005][1005];
int sum[1005][1005]; // Two-dimensional prefix sum
int n,m;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

int main()
{
    int listt,i,j;
    cin >> listt;
    while ( listt-- ) {
        cin >> n >> m;
        memset(sum,0,sizeof(sum));
        for ( i=1; i<=n; i++ ) {
            for ( j=1; j<=m; j++ ) {
                a[i][j] = read();
            }
        }
        for ( i=1; i<=n; i++ ) {
            for ( j=1; j<=m; j++ ) {
                sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
            }
        }
        int ans = 0;
        for ( i=1; i<=n; i++ ) {
            for ( j=1; j<=m; j++ ) {
                for ( int len=ans; i+len<=n&&j+len<=m; len++ ) {
                    int posi = i+len;
                    int posj = j+len;
                    if ( sum[posi][posj]-sum[i-1][posj]-sum[posi][j-1]+sum[i-1][j-1]<=1 ) {
                        ans = max(ans,len+1);
                    }
                    else if ( sum[posi][posj]-sum[i-1][posj]-sum[posi][j-1]+sum[i-1][j-1]>1 ) {
                        break ;
                    }
                }
            }
        }
        cout << ans << endl;


    }

    return 0;
}

 

Posted by jassikundi on Fri, 04 Oct 2019 02:49:39 -0700