-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path417_Pacific_Atlantic_water_flow.txt
More file actions
86 lines (71 loc) · 2.62 KB
/
417_Pacific_Atlantic_water_flow.txt
File metadata and controls
86 lines (71 loc) · 2.62 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
class Solution {
public:
int coord[4][2] = {{-1,0}, {0,-1}, {0,1},{1,0}};
bool visited[200][200];
bool isPossible(int i,int j, int n,int m){
return i < n && i >= 0 && j < m && j >= 0 && visited[i][j] != true;
}
set<pair<int, int>> dfs(vector<vector<int>>& heights, queue<pair<int, int>>& currentQueue, set<pair<int, int>>& resOcean){
int n = heights.size();
int m = heights[0].size();
while(!currentQueue.empty()){
pair<int, int> currHeight = currentQueue.front();
currentQueue.pop();
int x = currHeight.first;
int y = currHeight.second;
for(int k = 0; k < 4; k++){
int nextx = x + coord[k][0];
int nexty = y + coord[k][1];
if(isPossible( nextx, nexty, n, m)){
if(heights[x][y] <= heights[nextx][nexty]){
currentQueue.push({nextx, nexty});
visited[nextx][nexty] = true;
resOcean.insert({nextx, nexty});
}
}
}
}
return resOcean;
}
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
queue<pair<int, int>> queueP;
queue<pair<int, int>> queueA;
set<pair<int, int>> coorP;
set<pair<int, int>> coorA;
int n = heights.size();
int m = heights[0].size();
memset(visited, false, sizeof(visited));
// pacific fill
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(i == 0 || j == 0){
queueP.push({i,j});
visited[i][j] = true;
coorP.insert({i,j});
}
}
}
set<pair<int, int>> resP = dfs(heights, queueP, coorP);
memset(visited, false, sizeof(visited));
// Atlantic fill
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(i == n-1 || j == m-1){
queueA.push({i,j});
visited[i][j] = true;
coorA.insert({i,j});
}
}
}
set<pair<int, int>> resA = dfs(heights, queueA, coorA);
set<pair<int,int>> interseccion;
set_intersection(resP.begin(), resP.end(),
resA.begin(), resA.end(),
inserter(interseccion, interseccion.begin()));
vector<vector<int>> island;
for( auto item : interseccion){
island.push_back({item.first, item.second});
}
return island;
}
};