-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_matrix.cpp
58 lines (48 loc) · 1.67 KB
/
01_matrix.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
#include "vector"
#include "queue"
using namespace std;
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
// not visited = -1, any other value represents distance from
// closest 0
vector<vector<int>> answer(m, vector<int>(n, -1));
queue<pair<int, int>> q;
// Fill the queue with indices of 0 elements and mark them as visited i.e. distance = 0
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (mat[i][j] == 0) {
q.push({i,j});
answer[i][j] = 0;
}
}
}
//BFS. Note that the only unvisited nodes will be the elements with 1
while (!q.empty()) {
// indices of parent vertice
int i = q.front().first;
int j = q.front().second;
q.pop();
// check whether adjacent vertices exist, add them to queue if not visited and update answer
if (i+1 < m && answer[i+1][j] == -1) {
q.push({i+1,j});
answer[i+1][j] = answer[i][j] + 1;
}
if (i-1 > -1 && answer[i-1][j] == -1) {
q.push({i-1, j});
answer[i-1][j] = answer[i][j] + 1;
}
if (j+1 < n && answer[i][j+1] == -1) {
q.push({i, j+1});
answer[i][j+1] = answer[i][j] + 1;
}
if (j-1 > -1 && answer[i][j-1] == -1) {
q.push({i, j-1});
answer[i][j-1] = answer[i][j] + 1;
}
}
return answer;
}
};