forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
60 lines (57 loc) · 1.77 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
typedef pair<int, int> pii;
class Solution {
public:
vector<vector<int>> heights;
int m;
int n;
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
m = heights.size();
n = heights[0].size();
this->heights = heights;
queue<pii> q1;
queue<pii> q2;
unordered_set<int> vis1;
unordered_set<int> vis2;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i == 0 || j == 0) {
vis1.insert(i * n + j);
q1.emplace(i, j);
}
if (i == m - 1 || j == n - 1) {
vis2.insert(i * n + j);
q2.emplace(i, j);
}
}
}
bfs(q1, vis1);
bfs(q2, vis2);
vector<vector<int>> ans;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int x = i * n + j;
if (vis1.count(x) && vis2.count(x)) {
ans.push_back({i, j});
}
}
}
return ans;
}
void bfs(queue<pii>& q, unordered_set<int>& vis) {
vector<int> dirs = {-1, 0, 1, 0, -1};
while (!q.empty()) {
for (int k = q.size(); k > 0; --k) {
auto p = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int x = p.first + dirs[i];
int y = p.second + dirs[i + 1];
if (x >= 0 && x < m && y >= 0 && y < n && !vis.count(x * n + y) && heights[x][y] >= heights[p.first][p.second]) {
vis.insert(x * n + y);
q.emplace(x, y);
}
}
}
}
}
};