2019 cattle off more field correction VIII A All-one Matrices suspension line method, to be completed stack monotone

All-one Matrices

The meaning of problems

For a n, m 01 matrix, and asked how many great matrix.

analysis

For hanging line method, its enumeration process is the great matrix of the process, how to count that it? For a point, we have been enumerated if it had a matrix comprising a continuous border around the border point on a suspended line, then that point out of the matrix suspension wires was covered with the previous line, and vice versa does not cover cnt ++, belong to a single line about a great weight matrix how to do? Only you need to record about this line, on the left and right borders enumerate a great matrix, and can be present for it. For a height of the current point of time, the need for de-duplication judgment Patent, similar logic.

#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define mkp make_pair
const int maxn=3000+5;
using namespace std;
int Left[maxn][maxn],Right[maxn][maxn],up[maxn][maxn];
char s[maxn][maxn];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",s[i]+1);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(s[i][j]=='1'){
                Left[i][j]=Left[i][j-1]==0?j:Left[i][j-1];
                up[i][j]=up[i-1][j]+1;
            }
        }
        for(int j=m;j>=1;j--){
            if(s[i][j]=='1'){
                Right[i][j]=Right[i][j+1]==0?j:Right[i][j+1];
            }
        }
    }
/*  for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
        {
            printf("%d ",Left[i][j]);
        }
        puts("");
    }
    puts("");
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
        {
            printf("%d ",Right[i][j]);
        }
        puts("");
    }
 */

    int l_right=-1,l_left=-1;
    int cnt=0;
    for(int i=1;i<=n;i++){
     l_right=-1,l_left=-1;
    int  l_right1=-1,l_left1=-1;
        for(int j=1;j<=m;j++){
            if(up[i][j]==1&&(Right[i][j]!=l_right1||Left[i][j]!=l_left1)){
                    //cout<<i<<" "<<j<<endl; 
                    cnt++;
                l_right1=Right[i][j];l_left1=Left[i][j];
            }
            else if(up[i][j]>1){
                int tmpr=min(Right[i][j],Right[i-1][j]);
                int tmpl=max(Left[i][j],Left[i-1][j]);
                if((tmpl!=l_left||tmpr!=l_right)&&(tmpr!=Right[i-1][j]||tmpl!=Left[i-1][j])){
                    //cout<<i<<" "<<j<<endl; 
                    cnt++;
                l_right=tmpr;l_left=tmpl;
                }
                Right[i][j]=min(Right[i][j],Right[i-1][j]);
                Left[i][j]=max(Left[i][j],Left[i-1][j]);
            }
        }
    }
    printf("%d\n",cnt);

    return 0;
}

Guess you like

Origin www.cnblogs.com/ttttttttrx/p/11432156.html