-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path417.cpp
More file actions
executable file
·66 lines (61 loc) · 2.24 KB
/
417.cpp
File metadata and controls
executable file
·66 lines (61 loc) · 2.24 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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
struct pair_hash {
template <class T1, class T2>
std::size_t operator () (const std::pair<T1,T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
// Mainly for demonstration purposes, i.e. works but is overly simple
// In the real world, use sth. like boost.hash_combine
return h1 ^ h2;
}
};
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int height = 0,width = 0;
vector<vector<int>> new_height;
unordered_map<pair<int,int>, pair<bool,bool>, pair_hash> records;
void dfs(pair<int, int> cur, bool & pac, bool & aln, vector<vector<bool>> flag){
if (records.count(cur)){
pac = records[cur].first;
aln = records[cur].second;
return;
}
flag[cur.first][cur.second] = true;
bool final_pac = false;
bool final_aln = false;
for (int i = 0; i < 4; i++){
new_x = cur.first + dir[i][0];
new_y = cur.second + dir[i][1];
if (new_x < 0 || new_y < 0){
final_pac = true;
}else if (new_x >= width || new_y >= height){
final_aln = true;
}else if (height[new_x][new_y] <= heigth[cur.first][cur.second] && flag[new_x][new_y] == false){
dfs(new_x, new_y, pac, aln);
final_pac = final_pac | pac;
final_aln = final_aln | final_aln;
}
}
pac = final_pac;
aln = final_aln;
return;
}
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
width = heighs.size();
height = heights[0].size();
vector<vector<int>> res;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++){
vector<vector<bool>> flag(width, vector<bool>(height,false));
bool pac = false,aln = false;
dfs(make_pair(i,j),pac,aln,flag);
records[make_pair(i,j)] = make_pair(pac,aln);
if (pac && aln){
res.push_back({i,j});
}
}
return res;
}
};