-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0542-01-matrix.cpp
More file actions
28 lines (28 loc) · 963 Bytes
/
0542-01-matrix.cpp
File metadata and controls
28 lines (28 loc) · 963 Bytes
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
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int n = (int) mat.size(), m = (int) mat[0].size();
vector<vector<int>> d(n, vector<int>(m, -1));
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == 0) {
q.push(make_pair(i, j));
d[i][j] = 0;
}
}
}
vector<pair<int, int>> directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
for (auto [dx, dy]: directions) {
int nx = x + dx, ny = y + dy;
if (nx >= 0 && ny >= 0 && nx < n && ny < m && d[nx][ny] == -1) {
d[nx][ny] = d[x][y] + 1;
q.push(make_pair(nx, ny));
}
}
}
return d;
}
};