代码:
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define db double
using namespace std;
const ll N = 1e7;
struct node {
ll x;
ll y;
} q[34225];
ll n, m, f[185][185], x, y, l, r;
char a[185][185];
void init() {
// cin >> n >> m;
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
// scanf("%c", &a[i][j]);
}
}
}
void bfs(ll kx, ll ky) {
ll dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
memset(f, 0, sizeof(f));
l = r = 1;
q[l] = {kx, ky};
while (l <= r) {
for (int i = 0; i < 4; i++) {
ll tx = q[l].x + dx[i], ty = q[l].y + dy[i];
if (tx <= n && tx >= 1 && ty <= m && ty >= 1 && !f[tx][ty]) {
q[++r] = {tx, ty};
f[tx][ty] = 1;
}
if (a[tx][ty] == '1') {
x = tx;
y = ty;
return ;
}
}
l++;
}
}
void solve() {
for (int i = 1; i <= n; /*cout << endl*/printf("\n"), i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == '0') {
bfs(i, j);
// cout << abs(i - x) + abs(j - y) << ' ';
printf("%lld ", abs(i - x) + abs(j - y));
} else {
// cout << 0 << ' ';
printf("0 ");
}
}
}
}
int main() {
init();
solve();
return 0;
}