-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01Matrix.cpp
More file actions
43 lines (40 loc) · 1.3 KB
/
01Matrix.cpp
File metadata and controls
43 lines (40 loc) · 1.3 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
// Source: https://leetcode.com/problems/01-matrix/
// Author: Miao Zhang
// Date: 2021-02-20
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
queue<pair<int, int>> q;
vector<vector<int>> visited(m, vector<int>(n, 0));
vector<vector<int>> res(m, vector<int>(n, INT_MAX));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
q.push({i, j});
visited[i][j] = 1;
}
}
}
vector<vector<int>> dirs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int val = 0;
while (!q.empty()) {
int size = q.size();
while (size--) {
auto pair = q.front();
q.pop();
res[pair.first][pair.second] = val;
for (auto d: dirs) {
int x = pair.first + d[0];
int y = pair.second + d[1];
if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y] == 1) continue;
visited[x][y] = 1;
q.push({x, y});
}
}
val++;
}
return res;
}
};