-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBITMAP_1.cpp
More file actions
100 lines (86 loc) · 1.84 KB
/
Copy pathBITMAP_1.cpp
File metadata and controls
100 lines (86 loc) · 1.84 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int Map[182][182];
struct couple
{
int i, j;
couple(int y, int x)
{
i = y;
j = x;
}
};
couple MakeCouple(int i, int j)
{
couple x(i, j);
return x;
}
void AssignMinDistance(int i, int j, int N, int M)
{
int distance = 0;
queue<couple> ToVisit;
Map[i][j] = 0;
ToVisit.push(MakeCouple(i, j)); //root of BFS
while(ToVisit.size())
{
int y = ToVisit.front().i;
int x = ToVisit.front().j;
couple Visiting(y, x); //visit this node's neighbours
ToVisit.pop(); //remove the first element
distance = Map[y][x] + 1; //exploring immediate neighbours
//We visit a node only if the previously assigned distance is larger than the apparent distance
if (y != 0 && distance < Map[y-1][x]) //can visit up
{
Map[y-1][x] = distance;
ToVisit.push(MakeCouple(y-1, x));
}
if (y != N-1 && distance < Map[y+1][x]) //can visit down
{
Map[y+1][x] = distance;
ToVisit.push(MakeCouple(y+1, x));
}
if (x != 0 && distance < Map[y][x-1]) //can visit left
{
Map[y][x-1] = distance;
ToVisit.push(MakeCouple(y, x-1));
}
if (x != M-1 && distance < Map[y][x+1]) //can visit right
{
Map[y][x+1] = distance;
ToVisit.push(MakeCouple(y, x+1));
}
}
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int N, M;
cin>>N>>M;
std::vector<couple> Ones;
char x;
for (int i = 0; i < N; i++)
for (int j = 0; j < M && cin>>x; j++)
if (x == '1')
{
Map[i][j] = -2000;
Ones.push_back(MakeCouple(i, j));
}
else
Map[i][j] = 2000; //shall be overwritten the first time it's accessed in BFS
for (int i = 0; i < Ones.size(); i++)
AssignMinDistance(Ones[i].i, Ones[i].j, N, M);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
cout<<Map[i][j]<<' ';
cout<<endl;
}
}
return 0;
}