-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
74 lines (70 loc) · 1.59 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
void wallsAndGates(vector<vector<int>>& rooms) {
int m = rooms.size();
if (m == 0) {
return;
}
int n = rooms[0].size();
if (n == 0) {
return;
}
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.push(make_pair(i, j));
}
}
}
while (q.size() > 0) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (x - 1 >= 0 && rooms[x - 1][y] > rooms[x][y] + 1) {
rooms[x - 1][y] = rooms[x][y] + 1;
q.push(make_pair(x - 1,y));
}
if (x + 1 < m && rooms[x + 1][y] > rooms[x][y] + 1) {
rooms[x+1][y] = rooms[x][y]+1;
q.push(make_pair(x + 1,y));
}
if (y - 1 >= 0 && rooms[x][y - 1] > rooms[x][y] + 1) {
rooms[x][y - 1] = rooms[x][y] + 1;
q.push(make_pair(x,y - 1));
}
if (y + 1 < n && rooms[x][y + 1] > rooms[x][y] + 1) {
rooms[x][y + 1] = rooms[x][y] + 1;
q.push(make_pair(x,y + 1));
}
}
}
};
int main() {
Solution sol;
int INF = INT_MAX;
vector<vector<int>> rooms = {
{INF, -1, 0, INF},
{INF, INF, INF, -1},
{INF, -1, INF, -1},
{0, -1, INF, INF},
};
sol.wallsAndGates(rooms);
for (auto v: rooms) {
for (auto i: v) {
cout << i << ' ';
}
cout << endl;
}
return 0;
}