[CF1185E] Polycarp and Snakes

Given a (n \ times m \) \ matrix, each time you can add a horizontal or vertical chain in alphabetical order, this chain which would be a ~ z \ (26 \) lowercase letters a, and will overwrite the original chain. Now the matrix after a given operation, whether to answer the requirements may form such a case, each chain and from which point to which point.

Solution

Reverse process, letters peel layer by layer, while leaving the recording layer prior to the "do not care" position, i.e. the position previously covered since now dispensable

So we present the letters to cfind cthe letter four border check whether this is a straight chain, if not quit

If so, check whether the unrelated plus the letters fail to fill within this linear range, if not quit

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

const int N = 2005;

char a[N][N];
int t,n,m,l[N],r[N],u[N],b[N],t1[N],t2[N],t3[N],t4[N];

int calc(int c) {
    int cnt=0;
    for(int i=b[c];i<=u[c];i++) {
        for(int j=l[c];j<=r[c];j++) {
            if(a[i][j]=='*'||a[i][j]==c) ++cnt;
            a[i][j]='*';
        }
    }
    return cnt;
}

void putss(string s) {cout<<s<<endl;}

signed main() {
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--) {
        cin>>n>>m;
        for(int i=1;i<=n;i++) cin>>a[i]+1;
        for(int i='a';i<='z';i++) l[i]=b[i]=1e9,r[i]=u[i]=t1[i]=t2[i]=t3[i]=t4[i]=0;
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                if(a[i][j]>='a' && a[i][j]<='z') {
                    l[a[i][j]]=min(l[a[i][j]],j);
                    r[a[i][j]]=max(r[a[i][j]],j);
                    b[a[i][j]]=min(b[a[i][j]],i);
                    u[a[i][j]]=max(u[a[i][j]],i);
                }
            }
        }
        int cnt=0;
        for(int i='z';i>='a';--i) {
            if(u[i]==0) continue;
            if(u[i]!=b[i] && l[i]!=r[i]) goto EN;
            if(calc(i)!=(r[i]-l[i]+1)*(u[i]-b[i]+1)) goto EN;
        }
        putss("YES");
        for(int i='a';i<='z';i++) if(u[i]) cnt=i-'a'+1;
        cout<<cnt<<endl;
        for(int i='y';i>='a';i--) if(u[i]==0) {
            b[i]=b[i+1];
            u[i]=u[i+1];
            l[i]=l[i+1];
            r[i]=r[i+1];
        }
        for(int i='a';i<='z';i++) if(u[i]) cout<<b[i]<<" "<<l[i]<<" "<<u[i]<<" "<<r[i]<<endl;
        continue;
        EN: putss("NO");
    }
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12631776.html