- js24012 的博客
O1444 【基础】填涂颜色
- @ 2026-3-18 16:40:36
O1444 【基础】填涂颜色
#include<bits/stdc++.h>
using namespace std;
int n,cnt,a[101][101];
int dx[]{0,1,-1,0,0};
int dy[]{0,0,0,1,-1};
struct node{
int x,y;
};
bool check(int x,int y){
if(a[x][y] != 0)return false;
if(x < 1 || y < 1 || x > n || y > n)return false;
return true;
}
void bfs(int x,int y){
queue<node> q;
q.push((node){x,y});
a[x][y]=2;
while(!q.empty()){
node t=q.front();
int nx=t.x;
int ny=t.y;
q.pop();
for(int i = 1;i <= 4;i++){
int X=nx+dx[i];
int Y=ny+dy[i];
if(check(X,Y)){
// cout << X << " " << Y<< endl;//
q.push((node){X,Y});
a[X][Y] = 2;
}
}
}
}
signed main(){
cin >> n;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
cin >> a[i][j];
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(i == n || j == n || i == 1 || j == 1){
if(a[i][j] == 0){
bfs(i,j);
}
}
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(a[i][j] == 2){
cout << 0 << " ";
}
if(a[i][j] == 1){
cout << 1 << " ";
}
if(a[i][j] == 0){
cout << 2 << " ";
}
}
cout << endl;
}
return 0;
}
/*
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
*/