本地没问题,提交满江紫
查看原帖
本地没问题,提交满江紫
1073741
zhangjiaan楼主2024/11/20 20:12
#include<bits/stdc++.h>
using namespace std;
int a[35][35],n;
struct p{
	p(int a,int b):x(a),y(b){}
	int x,y;
};
int d[4][2]{{-1,0},{1,0},{0,-1},{0,1}};
inline bool check(p k){
	return  k.x>=0
		    &&k.y>=0
			&&k.x<=n+2
			&&k.y<=n+2
			&&a[k.x][k.y]==0;
}
queue<p> q;
void bfs(void){
	q.push({0,0});
	a[0][0]=-1;
	while(!q.empty()){
		p dq=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			p nq={dq.x+d[0][i],dq.y+d[1][i]};
			if(check(nq)){
				a[nq.x][nq.y]=-1;
				q.push(nq);
			}
		}
	}
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
		}
	}
	bfs();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(a[i][j]==-1){
				cout<<0<<" ";
			}else if(a[i][j]==0){
				cout<<2<<" ";
			}else{
				cout<<1<<" ";
			}
		}
		cout<<'\n';
	}
	cout<<flush;
	return 0;
}
2024/11/20 20:12
加载中...