1+ //
2+ // Created by 10578 on 2021/3/26.
3+ //
4+
5+ #include " iostream"
6+ #include " fstream"
7+ #include " sstream"
8+ #include " iterator"
9+ #include " utility"
10+ #include " vector"
11+ #include " unordered_set"
12+ #include " algorithm"
13+ #include " stack"
14+ #include " limits"
15+ #include " string"
16+
17+ #define ALTER_IN (filename ) std::fstream fs (filename); std::cin.rdbuf(fs.rdbuf());
18+
19+ using pii = std::pair<int , int >;
20+ using namespace std ;
21+
22+ namespace std {
23+ template <>
24+ struct hash <pii> {
25+ size_t operator ()(const pii &p) const {
26+ hash<int > hash_fn;
27+ size_t seed = hash_fn (p.first );
28+ return hash_fn (p.second ) + 0x9e3779b9 + (seed << 6 ) + (seed >> 2 );
29+ }
30+ };
31+ }
32+
33+ class Solution {
34+ public:
35+ vector<vector<int >> pacificAtlantic (vector<vector<int >> &matrix) {
36+ auto m = matrix.size (), n = matrix.front ().size ();
37+ vector<vector<unordered_set<pii>>> adj (m, vector<unordered_set<pii>>(n));
38+
39+ unordered_set<pii> pac_set;
40+ unordered_set<pii> atl_set;
41+
42+ for (auto i = 0 ; i < m; ++i) {
43+ for (auto j = 0 ; j < n; ++j) {
44+ int elem = matrix[i][j];
45+
46+ if (i - 1 > 0 && matrix[i - 1 ][j] >= elem) {
47+ adj[i][j].emplace (i - 1 , j);
48+ }
49+
50+ if (i + 1 < m && matrix[i + 1 ][j] >= elem) {
51+ adj[i][j].emplace (i + 1 , j);
52+ }
53+
54+ if (j - 1 > 0 && matrix[i][j - 1 ] >= elem) {
55+ adj[i][j].emplace (i, j - 1 );
56+ }
57+
58+ if (j + 1 < n && matrix[i][j + 1 ] >= elem) {
59+ adj[i][j].emplace (i, j + 1 );
60+ }
61+ }
62+ }
63+
64+ stack<pii> s;
65+
66+ // pacific ocean
67+ for (auto i = 0 ; i < n; ++i) {
68+ s.emplace (i, 0 );
69+ }
70+ for (auto j = 1 ; j < m; ++j) {
71+ s.emplace (0 , j);
72+ }
73+ while (!s.empty ()) {
74+ auto curr = s.top ();
75+ s.pop ();
76+
77+ pac_set.insert (curr);
78+ for (auto &&x: adj[curr.first ][curr.second ]) {
79+ if (pac_set.count (x) == 0 ) {
80+ s.push (x);
81+ }
82+ }
83+ }
84+
85+ // atlantic ocean
86+ for (auto i = 0 ; i < n; ++i) {
87+ s.emplace (i, n - 1 );
88+ }
89+ for (auto j = 1 ; j < m; ++j) {
90+ s.emplace (m - 1 , j);
91+ }
92+ while (!s.empty ()) {
93+ auto curr = s.top ();
94+ s.pop ();
95+
96+ atl_set.insert (curr);
97+ for (auto &&x: adj[curr.first ][curr.second ]) {
98+ if (atl_set.count (x) == 0 ) {
99+ s.push (x);
100+ }
101+ }
102+ }
103+
104+ auto result = vector<pii>();
105+
106+ copy_if (pac_set.begin (), pac_set.end (), back_inserter (result), [&](const pii &p) {
107+ return atl_set.count (p) != 0 ;
108+ });
109+
110+ auto final_result = vector<vector<int >>(result.size ());
111+ transform (result.begin (), result.end (), begin (final_result), [](pii x) {
112+ return vector<int >{x.first , x.second };
113+ });
114+
115+ return final_result;
116+ }
117+ };
118+
119+ int main () {
120+ ALTER_IN (" in.txt" );
121+
122+ int m, n;
123+ cin >> m >> n;
124+ cin.ignore (numeric_limits<streamsize>::max (), ' \n ' );
125+
126+ vector<vector<int >> matrix (m);
127+ for (auto &x: matrix) {
128+ string line;
129+ getline (cin, line);
130+ istringstream iss (line);
131+
132+ x = vector<int >{istream_iterator<int >(iss), istream_iterator<int >()};
133+ }
134+
135+ Solution s;
136+ for (auto p: s.pacificAtlantic (matrix)) {
137+ cout << p[0 ] << " " << p[1 ] << endl;
138+ }
139+
140+ return 0 ;
141+ }
0 commit comments