-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBitmap.cpp
88 lines (77 loc) · 3.64 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
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
#include <iostream>
#include <queue>
#include <climits>
using namespace std;
#define INF INT_MAX
void breadthFirstSearch(vector<string>& arr, queue<pair<long long, long long> >& aQueue, vector<vector<long long> >& distance, long long ROWS, long long COLS);
bool AreValidCoordinates(long long r, long long c, long long ROWS, long long COLS);
int main() {
long long T;
cin >> T;
while(T--) {
long long m, n;
vector<string> arr;
cin >> m >> n;
for(long long i = 0; i < m; i++) {
string tmp;
cin >> tmp;
arr.push_back(tmp);
}
vector<vector<long long> > distanceArr(m, vector<long long>(n, INF));
queue<pair<long long, long long> > aQueue;
for(long long i = 0; i < m; i++) {
for(long long j = 0; j < n; j++) {
if(arr[i][j] == '1') {
aQueue.push(make_pair(i, j));
distanceArr[i][j] = 0;
}
}
}
breadthFirstSearch(arr, aQueue, distanceArr, m, n);
for(long long i = 0; i < m; i++) {
for(long long j = 0; j < n; j++)
cout << distanceArr[i][j] << " ";
cout << endl;
}
}
return 0;
}
void breadthFirstSearch(vector<string>& arr, queue<pair<long long, long long> >& aQueue, vector<vector<long long> >& distance, long long ROWS, long long COLS) {
vector<vector<bool> > isVisited(ROWS, vector<bool>(COLS, false));
while(!aQueue.empty()) {
pair<long long, long long> curElement = aQueue.front();
aQueue.pop();
// check all neighbours :
// 1. (x+1, y)
if(AreValidCoordinates(curElement.first+1, curElement.second, ROWS, COLS) && !isVisited[curElement.first+1][curElement.second]) {
distance[curElement.first+1][curElement.second] = std::min(distance[curElement.first][curElement.second] + 1,
distance[curElement.first+1][curElement.second]);
isVisited[curElement.first+1][curElement.second] = true;
aQueue.push(make_pair(curElement.first+1, curElement.second));
}
// 2. (x, y+1)
if(AreValidCoordinates(curElement.first, curElement.second+1, ROWS, COLS) && !isVisited[curElement.first][curElement.second+1]) {
distance[curElement.first][curElement.second+1] = std::min(distance[curElement.first][curElement.second] + 1,
distance[curElement.first][curElement.second+1]);
isVisited[curElement.first][curElement.second+1] = true;
aQueue.push(make_pair(curElement.first, curElement.second+1));
}
// 3. (x-1, y)
if(AreValidCoordinates(curElement.first-1, curElement.second, ROWS, COLS) && !isVisited[curElement.first-1][curElement.second]) {
distance[curElement.first-1][curElement.second] = std::min(distance[curElement.first][curElement.second] + 1,
distance[curElement.first-1][curElement.second]);
isVisited[curElement.first-1][curElement.second] = true;
aQueue.push(make_pair(curElement.first-1, curElement.second));
}
// 4. (x, y-1)
if(AreValidCoordinates(curElement.first, curElement.second-1, ROWS, COLS) && !isVisited[curElement.first][curElement.second-1]) {
distance[curElement.first][curElement.second-1] = std::min(distance[curElement.first][curElement.second] + 1,
distance[curElement.first][curElement.second-1]);
isVisited[curElement.first][curElement.second-1] = true;
aQueue.push(make_pair(curElement.first, curElement.second-1));
}
}
}
bool AreValidCoordinates(long long r, long long c, long long ROWS, long long COLS) {
return r >= 0 && r < ROWS && c >= 0 && c < COLS;
}