-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
41 lines (41 loc) · 1.34 KB
/
solution.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
/**
* 50 / 50 test cases passed.
* Runtime: 68 ms
* Memory Usage: 28.5 MB
*/
class Solution {
public:
int direct[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using pii = pair<int, int>;
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size();
vector<vector<int>> ans(m, vector<int>(n));
queue<pii> que;
for (int i = 0; i < m; ++ i) {
for (int j = 0; j < n; ++ j) {
if (mat[i][j] == 0) {
for (auto &d : direct) {
int x = i + d[0];
int y = j + d[1];
if (x < 0 || x >= m || y < 0 || y >= n) continue;
if (!mat[x][y] || ans[x][y]) continue;
ans[x][y] = 1;
que.emplace(x, y);
}
}
}
}
while (!que.empty()) {
auto [x, y] = que.front(); que.pop();
for (auto &d : direct) {
int xx = x + d[0];
int yy = y + d[1];
if (xx < 0 || xx >= m || yy < 0 || yy >= n) continue;
if (!mat[xx][yy] || ans[xx][yy]) continue;
ans[xx][yy] = ans[x][y] + 1;
que.emplace(xx, yy);
}
}
return ans;
}
};