求助
查看原帖
求助
988068
zvzbpkvo1楼主2023/11/26 22:18
#include <bits/stdc++.h>
using namespace std;
struct pos{int x, y, c;};
int a[200][200];
bool v[200][200];
int n, m;
queue<pos> q;
int fx[4]={0, 0, 1, -1};
int fy[4]={1, -1, 0, 0};
void bfs(){
	int tx, ty;
	pos now;
	while (!q.empty()){
		now=q.front();
		q.pop();
		for (int i=0;i<4;i++){
			tx=now.x+fx[i];
			ty=now.y+fy[i];
			if (tx<=0||tx>n||ty<=0||ty>m||a[tx][ty]!=0||v[tx][ty]==1) continue;
			a[now.x][now.y]=now.c;
			q.push({tx, ty, now.c+1});
			v[tx][ty]=1;
		}
	}
}
int main(){
	memset(a, 0, sizeof a);
	memset(v, 0, sizeof v);
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		string buf;
		cin >> buf;
		for (int j=0;j<m;j++){
			if (buf[j]=='1'){
				q.push({i, j+1, 0});
				v[i][j+1]=1;
			}
		}
	}
	
	bfs();
	
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++)
			cout << a[i][j] << " ";
		cout << endl;
	}
	return 0;
}
2023/11/26 22:18
加载中...