80分,求助!!!

P1162 填涂颜色

huangxinyou @ 2023-11-07 19:22:32

思路: 循环每一格,判断是否上下左右都有为1的格子,如果满足,则变为2

#include<bits/stdc++.h>
using namespace std;
#define FO(i,a,b) for(int i=a;i<=b;i++)
int n,a[31][31],nx[31],nn[31],sx[31],sn[31];
bool ok(int a,int b){return a>=sn[b]&&a<=sx[b]&&b>=nn[a]&&b<=nx[a];}
int main(){
    memset(nn,0x3f,sizeof(nn));
    memset(sn,0x3f,sizeof(sn));
    cin>>n;
    FO(i,1,n){
        FO(j,1,n){
            cin>>a[i][j];
            if(a[i][j]==0)continue;
            nx[i]=max(nx[i],j);
            sx[j]=max(sx[j],i);
            nn[i]=min(nn[i],j);
            sn[j]=min(sn[j],i);
        }
    }
    FO(i,1,n){
        FO(j,1,n){
            if(a[i][j]==0&&ok(i,j)==1)a[i][j]=2;
            cout<<a[i][j]<<" ";
        }
        cout<<"\n";
    }
    return 0;
}

nn:每一行中最左边的1 nx:每一行中最右边的1 sn:每一列中最左边的1 sx:每一列中最右边的1


by AnnaQ @ 2023-11-07 20:26:53

不会TLE吗?


|