题解
2025-03-04 20:13:42
发布于:广东
3阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
const int maxn=410;
typedef pair<int,int>pi;
queue<pi> q;
int n,m,a[maxn][maxn];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
bool vis[maxn][maxn];
int main(){
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
q.push((pi){0,0});
vis[0][0]=1;
while(!q.empty()){
pi tmp=q.front();
q.pop();
int x=tmp.first,y=tmp.second;
for(int i=0;i<4;i++){
int xx=dx[i]+x;
int yy=dy[i]+y;
if(xx>=0&&xx<=n+1&&yy>=0&&yy<=n+1&&!vis[xx][yy]&&!a[xx][yy]){
q.push((pi){xx,yy});
vis[xx][yy]=1;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!vis[i][j]&&!a[i][j]) a[i][j]=2;
cout<<a[i][j]<<' ';
}
cout<<endl;
}
return 0;
}
这里空空如也
有帮助,赞一个