矩阵距离
https://www.acwing.com/problem/content/175/
多源转化为单源最短路问题
#include <cstring>
#include <iostream>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1010, M = N * N;
int n, m;
char g[N][N];
PII q[M];
int dist[N][N];
bool st[N][N];
void bfs()
{
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int hh = 0, tt = -1;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
if (g[i][j] == '1')
{
dist[i][j] = 0;
q[ ++ tt] = {i, j};
st[i][j]=true;
}
while (hh <= tt)
{
auto t = q[hh ++ ];
for (int i = 0; i < 4; i ++ )
{
int a = t.x + dx[i], b = t.y + dy[i];
if (a < 1 || a > n || b < 1 || b > m) continue;
if (st[a][b]) continue;
dist[a][b] = dist[t.x][t.y] + 1;
q[ ++ tt] = {a, b};
st[a][b]=true;
}
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%s", g[i] + 1);
bfs();
for (int i = 1; i <= n; i ++ )
{
for (int j = 1; j <= m; j ++ ) printf("%d ", dist[i][j]);
puts("");
}
return 0;
}