-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.cpp
59 lines (59 loc) · 1.13 KB
/
bitmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll dist[200][200];
int n,m;
int moves_x[]={-1,0,1},moves_y[]={-1,0,1};
void bfs(pair<int,int> point)
{
queue< pair<int,int> >q;
q.push(point);
while(q.empty()==false)
{
int x=q.front().first,y=q.front().second;
q.pop();
for(int i=0;i<3;i++)
{
for(int j=0;i<3;j++)
{
if((abs(moves_x[i])!=abs(moves_y[j])) and x+moves_x[i]>=0 and x+moves_x[i]<n and y+moves_y[j]>=0 and y+moves_y[j]<m and dist[x+moves_x[i]][y+moves_y[j]]>1+dist[x][y])
{
dist[x+moves_x[i]][y+moves_y[j]]=1+dist[x][y];
q.push(make_pair(x+moves_x[i],y+moves_y[j]));
}
}
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
vector< pair<int,int> >v;
cin>>n>>m;
for(int i=0;i<n;i++)
{
char val[m];
scanf("%s",&val);
for(int j=0;j<m;j++)
{
dist[i][j]=9999999;
if(val[j]-'0'==1)
{
dist[i][j]=0;
v.push_back(make_pair(i,j));
}
}
}
for(int i=0;i<v.size();i++)
bfs(v[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cout<<dist[i][j]<<" ";
cout<<endl;
}
}
}