本地没问题,提交满江紫

P1162 填涂颜色

UKE_bound @ 2024-11-20 20:12:57

#include<bits/stdc++.h>
using namespace std;
int a[35][35],n;
struct p{
    p(int a,int b):x(a),y(b){}
    int x,y;
};
int d[4][2]{{-1,0},{1,0},{0,-1},{0,1}};
inline bool check(p k){
    return  k.x>=0
            &&k.y>=0
            &&k.x<=n+2
            &&k.y<=n+2
            &&a[k.x][k.y]==0;
}
queue<p> q;
void bfs(void){
    q.push({0,0});
    a[0][0]=-1;
    while(!q.empty()){
        p dq=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            p nq={dq.x+d[0][i],dq.y+d[1][i]};
            if(check(nq)){
                a[nq.x][nq.y]=-1;
                q.push(nq);
            }
        }
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    bfs();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i][j]==-1){
                cout<<0<<" ";
            }else if(a[i][j]==0){
                cout<<2<<" ";
            }else{
                cout<<1<<" ";
            }
        }
        cout<<'\n';
    }
    cout<<flush;
    return 0;
}

by zhang_jun @ 2024-11-20 20:17:40

#include<bits/stdc++.h>
using namespace std;
int a[35][35],n;
struct p{
    p(int a,int b):x(a),y(b){}
    int x,y;
};
int d[4][2]{{-1,0},{1,0},{0,-1},{0,1}};
inline bool check(p k){
    return  k.x>=0
            &&k.y>=0
            &&k.x<=n+2
            &&k.y<=n+2
            &&a[k.x][k.y]==0;
}
queue<p> q;
void bfs(void){
    q.push({0,0});
    a[0][0]=-1;
    while(!q.empty()){
        p dq=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            p nq={dq.x+d[i][0],dq.y+d[i][1]};
            if(check(nq)){
                a[nq.x][nq.y]=-1;
                q.push(nq);
            }
        }
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    bfs();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i][j]==-1){
                cout<<0<<" ";
            }else if(a[i][j]==0){
                cout<<2<<" ";
            }else{
                cout<<1<<" ";
            }
        }
        cout<<'\n';
    }
    cout<<flush;
    return 0;
}

|