题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 1010;
char a[N][N];
int dist[N][N];
int n, m;
void bfs()
{
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
memset(dist, -1, sizeof dist);
queue<pair<int, int>> q;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
if (a[i][j] == '1')
{
q.push({i, j});
dist[i][j] = 0;
}
while(q.size())
{
auto t = q.front();
q.pop();
int x = t.first, y = t.second;
int d = dist[x][y];
for (int i = 0; i < 4; i ++)
{
int a = x + dx[i], b = y +dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if(dist[a][b] != -1) continue;
q.push({a, b});
dist[a][b] = d + 1;
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
cin >> a[i][j];
bfs();
for (int i = 0; i < n; i ++)
{
for (int j = 0; j < m; j ++)
cout << dist[i][j] << " ";
cout << endl;
}
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla