#include<stdio.h>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<ctype.h>
#include<stack>
#include<math.h>
#include <string>
#include<algorithm>
using namespace std;
typedef unsigned long long ULL;
struct node
{
int x;
int y;
};
char c[801][801];
int n,m;
int dis[801][801];
int book[801][801];
int nx[4][2]={0,1,
0,-1,
-1,0,
1,0};
queue<node> q;
node next,a;
void bfs()
{
while(!q.empty())
{
a=q.front();
q.pop();
for(int i=0;i<4;i++)
{
next.x=a.x+nx[i][0];
next.y=a.y+nx[i][1];
if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&dis[next.x][next.y]==-1)
{
q.push(next);
dis[next.x][next.y]=dis[a.x][a.y]+1;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>c[i];
node t;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(c[i][j]=='0')
{
dis[i][j]=0;
t.x=i;
t.y=j;
q.push(t);
}
else
dis[i][j]=-1;
bfs();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cout<<dis[i][j]<<" ";
cout<<endl;
}
return 0;
}
HihoCoder - 1478(水陆距离)
最新推荐文章于 2020-11-09 18:42:57 发布