-
Notifications
You must be signed in to change notification settings - Fork 13
/
alphacode2.cpp
85 lines (83 loc) · 1.76 KB
/
alphacode2.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using namespace std;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<limits>
#include<queue>
#include<map>
#define LLU long long unsigned int
#define LLD long long double
#define FOR(i,N) for(int i=0;i<(N);i++)
int MAX=numeric_limits<int>::max();
int myarrx[]={-1,0,1,0};
int myarry[]={0,-1,0,1};
int cnt[210][210],R,C;
void init_cnt();
vector<string>inp;
void fill()
{
int x,y,xc,yc;
map<pair<int,int>, bool> vis;
pair<int,int> temp;
pair<int,int> child;
queue<pair<int,int> > q;
FOR(i,R)
FOR(j,C)
if(inp[i][j]=='1')
{
cnt[i][j]=0;
temp=make_pair(i,j);
q.push(temp);
}
while(!q.empty())
{
temp=q.front();
q.pop();
x=temp.first,y=temp.second;
for(int i=0;i<4;i++)
{
xc=x+myarrx[i],yc=y+myarry[i];
if(xc>=0 && xc<R && yc>=0 && yc<C && cnt[xc][yc]>cnt[x][y]+1)
{
child=make_pair(xc,yc);
cnt[xc][yc]=cnt[x][y]+1;
q.push(child);
}
}
}
}
int main()
{
int t;
string temp;
cin>>t;
while(t!=0)
{
inp.clear();
cin>>R>>C;
FOR(i,R)
{
cin>>temp;
inp.push_back(temp);
}
init_cnt();
fill();
FOR(i,R)
{
cout<<cnt[i][0];
for(int j=1;j<C;j++)
cout<<" "<<cnt[i][j];
cout<<endl;
}
t--;
}
return 0;
}
void init_cnt()
{
FOR(i,R)
FOR(j,C)
cnt[i][j]=MAX;
}