AcWing 2021.5.8. Problem L. 跑图 · 已会
原题链接
简单
作者:
DPH
,
2021-05-11 18:59:36
,
所有人可见
,
阅读 152
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int N = 500 + 10;
bool vis[N][N];
int n, m, a[N][N];
struct Node
{
int x, y, t;
};
queue<Node> q;
int cx[] = {-1, 0, 1, 0};
int cy[] = {0, 1, 0, -1};
bool check(int x, int y)
{
if(vis[x][y]) return false;
if(x > n || x < 0 || y > m || y < 0) return false;
return true;
}
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>a[i][j];
if(a[i][j]) { vis[i][j] = 1; q.push((Node){i, j, 0}); }
}
}
while(q.size())
{
Node no = q.front(); q.pop();
a[no.x][no.y] = no.t;
for(int i = 0; i <= 3; i++)
{
for(int j = 0; j <= 3; j++)
{
int xx = no.x + cx[i];
int yy = no.y + cy[i];
if(check(xx, yy))
{
vis[xx][yy] = 1; q.push((Node){xx, yy, no.t + 1});
}
}
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cout<<a[i][j];
if(j == m) cout<<'\n';
else cout<<' ';
}
}
return 0;
}