发题解喽
2024-07-26 20:46:25
发布于:浙江
1阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
struct node{
int x, y;
};
int a[35][35];
bool vis[35][35];
int dir[4][2] = {-1, 0, 0, -1, 0, 1, 1, 0};
int n;
int check(int x, int y){
if(x < 1 || x > n)
return 1;
if(y < 1 || y > n)
return 2;
if(a[x][y] == 1)
return 3;
if(vis[x][y])
return 4;
return 5;
}
void bfs(int x, int y){
vector <node> v;
v.push_back({x, y});
bool flag = 1;
for(int i = 0; i < v.size(); i++){
node head = v[i];
if(head.x == 1 || head.x == n || head.y == 1 || head.y == n) flag = 0;
for(int j = 0; j < 4; j++){
int xx = head.x + dir[j][0], yy = head.y + dir[j][1];
if(check(xx, yy) == 5){
vis[xx][yy] = 1;
v.push_back({xx, yy});
}
}
}
if(flag)
for(auto it:v)
a[it.x][it.y] = 2;
}
int 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(a[i][j] == 0 && !vis[i][j])
bfs(i, j);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++)
cout << a[i][j] << ' ';
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个