P1162,0分求助!!!

P1162 填涂颜色

IAKIOI66666 @ 2024-05-23 18:43:46

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#define maxn 50 

using namespace std;

int n,a[maxn][maxn];
int b[5][2]={{-1,0},{1,0},{0,-1},{0,1}};
queue<int> q;

int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
            if(a[i][j]==0)a[i][j]=2;
        }
    for(int i=0;i<=n+1;i++)
        a[0][i]=2,a[n][i]=2,a[i][0]=2,a[i][n]=2;
    q.push(a[0][0]);
    while(!q.empty()){
        for(int i=0;i<=3;i++){
            int x=b[i][0],y=b[i][1];
            q.pop();
            if(x>n+1||y>1+n||x<0||y<0
            ||a[x][y]==1||a[x][y]==0)continue;
            a[x][y]=0;q.push(a[x][y]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            cout<<a[i][j]<<' ';
        cout<<endl;
    }
    return 0;
}

by 123huchenghao @ 2024-06-28 18:30:59

#include<bits/stdc++.h>
using namespace std;
struct pos
{
    int x;
    int y;
};
bool mark[32][32];
int pic[32][32],dx[]= {0,0,1,-1},dy[]= {1,-1,0,0},n;
queue<pos>q;
void bfs(int x,int y)
{
    int tx,ty;
    q.push(pos{x,y});
    mark[x][y]=true;
    while(!q.empty())
    {
        tx=q.front().x;
        ty=q.front().y;
        q.pop();
        for(int i=0; i<4; i++)
        {
            if(tx+dx[i]<0||ty+dy[i]<0||tx+dx[i]>n+1||ty+dy[i]>n+1||mark[tx+dx[i]][ty+dy[i]])continue;
            q.push(pos{tx+dx[i],ty+dy[i]});
            mark[tx+dx[i]][ty+dy[i]]=true;
        }
    }
}
int main()
{
    memset(pic,0,sizeof(pic));
    memset(mark,0,sizeof(mark));
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>pic[i][j];
            mark[i][j]=pic[i][j];
        }
    }
    bfs(0,0);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(mark[i][j])cout<<pic[i][j]<<" ";
            else cout<<"2"<<" ";
        }
        cout<<endl;
    }
}

|