DougNo1 @ 2024-07-08 16:04:00
代码:
#include <bits/stdc++.h>
using namespace std;
int n,a[35][35],b[35][35];
struct point{
int x,y;
};
int main(){
//输入
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
//BFS前准备
queue<point> q;
b[0][0]=1;
q.push(point{0,0});
int dx[5]={0,-1,1,0,0} , dy[5]={0,0,0,-1,1};
//BFS
while(!q.empty()){
point p= q.front();
q.pop();
for(int i=1;i<=4;i++){
int x=p.x+dx[i] , y=p.y+dy[i];
if(x<0 || x>n+1 || y<0 || y>n+1) continue;
if(a[x][y]==0 || b[x][y]==0){
b[x][y] = 1;
q.push(point{x,y});
}
}
}
//输出
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==0 && b[i][j]==0) cout<<2<<" ";
else if(a[i][j]==1 && b[i][j]==0) cout<<1<<" ";
else cout<<0<<" ";
}
cout<<endl;
}
return 0;
}
提交记录
请大佬们帮忙调一下!