-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode542.cpp
More file actions
46 lines (34 loc) · 1.03 KB
/
leetcode542.cpp
File metadata and controls
46 lines (34 loc) · 1.03 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
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
queue<pair<int, int>> q;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0) {
q.push({i, j});
} else {
mat[i][j] = -1;
}
}
}
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
while (!q.empty()) {
auto front = q.front();
q.pop();
int r = front.first;
int c = front.second;
for (int k = 0; k < 4; k++) {
int nr = r + dr[k];
int nc = c + dc[k];
if (nr >= 0 && nr < m && nc >= 0 && nc < n && mat[nr][nc] == -1) {
mat[nr][nc] = mat[r][c] + 1;
q.push({nr, nc});
}
}
}
return mat;
}
};