|
| 1 | +# [417. Pacific Atlantic Water Flow (Medium)](https://leetcode.com/problems/pacific-atlantic-water-flow/) |
| 2 | + |
| 3 | +<p>Given an <code>m x n</code> matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.</p> |
| 4 | + |
| 5 | +<p>Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.</p> |
| 6 | + |
| 7 | +<p>Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.</p> |
| 8 | + |
| 9 | +<p><b>Note:</b></p> |
| 10 | + |
| 11 | +<ol> |
| 12 | + <li>The order of returned grid coordinates does not matter.</li> |
| 13 | + <li>Both <i>m</i> and <i>n</i> are less than 150.</li> |
| 14 | +</ol> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | + |
| 18 | +<p><b>Example:</b></p> |
| 19 | + |
| 20 | +<pre>Given the following 5x5 matrix: |
| 21 | + |
| 22 | + Pacific ~ ~ ~ ~ ~ |
| 23 | + ~ 1 2 2 3 (5) * |
| 24 | + ~ 3 2 3 (4) (4) * |
| 25 | + ~ 2 4 (5) 3 1 * |
| 26 | + ~ (6) (7) 1 4 5 * |
| 27 | + ~ (5) 1 1 2 4 * |
| 28 | + * * * * * Atlantic |
| 29 | + |
| 30 | +Return: |
| 31 | + |
| 32 | +[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix). |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p> </p> |
| 36 | + |
| 37 | + |
| 38 | +**Related Topics**: |
| 39 | +[Depth-first Search](https://leetcode.com/tag/depth-first-search/), [Breadth-first Search](https://leetcode.com/tag/breadth-first-search/) |
| 40 | + |
| 41 | +## Solution 1. |
| 42 | + |
| 43 | +We can also use a two bits for each cell to represent if it's reachable from top-left and/or bottom-right. Here for simplicity I just used two `vector<vector<int>>`. |
| 44 | + |
| 45 | +```cpp |
| 46 | +// OJ: https://leetcode.com/problems/pacific-atlantic-water-flow/ |
| 47 | +// Author: github.com/lzl124631x |
| 48 | +// Time: O(MN) |
| 49 | +// Space: O(MN) |
| 50 | +class Solution { |
| 51 | + int dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}, M, N; |
| 52 | + void dfs(vector<vector<int>> &A, int x, int y, vector<vector<int>> &m) { |
| 53 | + if (m[x][y]) return; |
| 54 | + m[x][y] = 1; |
| 55 | + for (auto &[dx, dy] : dirs) { |
| 56 | + int a = x + dx, b = y + dy; |
| 57 | + if (a < 0 || a >= M || b < 0 || b >= N || A[a][b] < A[x][y]) continue; |
| 58 | + dfs(A, a, b, m); |
| 59 | + } |
| 60 | + } |
| 61 | +public: |
| 62 | + vector<vector<int>> pacificAtlantic(vector<vector<int>>& A) { |
| 63 | + if (A.empty() || A[0].empty()) return {}; |
| 64 | + M = A.size(), N = A[0].size(); |
| 65 | + vector<vector<int>> a(M, vector<int>(N)), b(M, vector<int>(N)), ans; |
| 66 | + for (int i = 0; i < M; ++i) { |
| 67 | + dfs(A, i, 0, a); |
| 68 | + dfs(A, i, N - 1, b); |
| 69 | + } |
| 70 | + for (int j = 0; j < N; ++j) { |
| 71 | + dfs(A, 0, j, a); |
| 72 | + dfs(A, M - 1, j, b); |
| 73 | + } |
| 74 | + for (int i = 0; i < M; ++i) { |
| 75 | + for (int j = 0; j < N; ++j) { |
| 76 | + if (a[i][j] && b[i][j]) ans.push_back({i, j}); |
| 77 | + } |
| 78 | + } |
| 79 | + return ans; |
| 80 | + } |
| 81 | +}; |
| 82 | +``` |
0 commit comments