WHY

P1162 填涂颜色

woyaoAKIOI0924PTY @ 2024-12-05 21:53:52

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,dx[]={0,1,-1,0},dy[]={1,0,0,-1},a[100][100];
bool vis[100][100]; 
struct Node{
    int x,y;
};
bool check(int x,int y)
{
    if(x>=0&&x<=n+1&&y>=0&&y<=n+1)
        return true;
    return false;   
}
void bfs(int x,int y)
{
    queue<Node> q;
    Node s;
    s.x=x;
    s.y=y;
    q.push(s);
    a[s.x][s.y]= 2;
    vis[s.x][s.y]=true;
    while(!q.empty())
    {
        Node u=q.front();
        q.pop();
        for(int i=0;i<4;++i)
        {
            Node w;
            w.x=u.x+dx[i];
            w.y=u.y+dy[i];
            if(check(w.x,w.y))
            {
                if(a[w.x][w.y]==0&&!(vis[w.x][w.y]))
                {
                    vis[w.x][w.y]=true;
                    a[w.x][w.y]=2;
                    q.push(w);
                }

            }

        }
    }

}
signed main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            cin>>a[i][j];
    bfs(0,0);
    for (int i=1;i<=n;++i)
    {
        for (int j=1;j<=n;++j)
        {
            if(a[i][j]==0)
            {
                cout<<"2 ";
                break;
            }
            if(a[i][j]==1)
            {
                cout<<"1 ";
                break;
            }
            if(a[i][j]==2)
            {
                cout<<"0 ";
                break;
            }

        }
        cout<<endl;
    }
    return 0;
}

|