forked from rachitiitr/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path542.01-matrix.cpp
More file actions
63 lines (50 loc) · 1.67 KB
/
542.01-matrix.cpp
File metadata and controls
63 lines (50 loc) · 1.67 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
using loc = pair<int, int>;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int n = mat.size(), m = mat[0].size();
vector<vector<int>> dis(n, vector<int>(m, INT_MAX));
// dis[i][j] = nearest distance (i, j) to nearest 0
queue<loc> q;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if (mat[i][j] == 0) {
q.push({i, j});
dis[i][j] = 0;
}
}
}
while(!q.empty()) {
// loc cur = q.front();
// int x = cur.first, y = cur.second;
auto [x, y] = q.front(); // current loc in mat, structured binding in C++
q.pop();
// (2,3) -> (1, 3) up, (3, 3) down, +1 -1 on the columns as well
for(int k = 0; k < 4; k++) {
int nx = x + dx[k], ny = y + dy[k];
if (0 <= nx && nx < n && 0 <= ny && ny < m) {
// valid neighbour
if (dis[nx][ny] == INT_MAX) {
dis[nx][ny] = 1 + dis[x][y];
q.push({nx, ny});
}
}
}
}
return dis;
}
};
/*
// BFS gives the shortest path in unweighted graphs
n * m <--- 0s and 1s
for every 1:
compute the smallest distance to 0
dis[i][j] = 0 where mat[i][j] = 0
queue<> Q = {(i,j)} where mat[i][j] = 0;
cur = Q.front();
for(nei in neighbors(cur)) {
dis[nei] = 1 + dis[cur] if nei is not alreaady in queue
}
*/