-
Notifications
You must be signed in to change notification settings - Fork 110
/
286-walls-and-gates.cpp
39 lines (37 loc) · 1.21 KB
/
286-walls-and-gates.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
class Solution {
public:
void wallsAndGates(vector<vector<int>>& rooms) {
int m = rooms.size();
if (m == 0) return;
int n = rooms[0].size();
vector<vector<int>> diff = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
queue<pair<int, int>> q;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (rooms[i][j] == 0) {
q.emplace(i, j);
}
}
}
int steps = 0;
while (!q.empty()) {
int layerSize = q.size();
while (layerSize--) {
pair<int, int> curr = q.front();
q.pop();
int i = curr.first;
int j = curr.second;
rooms[i][j] = steps;
for (auto& d : diff) {
int newi = i + d[0];
int newj = j + d[1];
if (newi >= 0 && newi < m && newj >= 0 && newj < n && rooms[newi][newj] == INT_MAX) {
rooms[newi][newj] = min(rooms[newi][newj], steps);
q.emplace(newi, newj);
}
}
}
steps++;
}
}
};