<p>There is an <code>m x n</code> rectangular island that borders both the <strong>Pacific Ocean</strong> and <strong>Atlantic Ocean</strong>. The <strong>Pacific Ocean</strong> touches the island&#39;s left and top edges, and the <strong>Atlantic Ocean</strong> touches the island&#39;s right and bottom edges.</p>

<p>The island is partitioned into a grid of square cells. You are given an <code>m x n</code> integer matrix <code>heights</code> where <code>heights[r][c]</code> represents the <strong>height above sea level</strong> of the cell at coordinate <code>(r, c)</code>.</p>

<p>The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell&#39;s height is <strong>less than or equal to</strong> the current cell&#39;s height. Water can flow from any cell adjacent to an ocean into the ocean.</p>

<p>Return <em>a <strong>2D list</strong> of grid coordinates </em><code>result</code><em> where </em><code>result[i] = [r<sub>i</sub>, c<sub>i</sub>]</code><em> denotes that rain water can flow from cell </em><code>(r<sub>i</sub>, c<sub>i</sub>)</code><em> to <strong>both</strong> the Pacific and Atlantic oceans</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg" style="width: 400px; height: 400px;" />
<pre>
<strong>Input:</strong> heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
<strong>Output:</strong> [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
<strong>Explanation:</strong> The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -&gt; Pacific Ocean 
&nbsp;      [0,4] -&gt; Atlantic Ocean
[1,3]: [1,3] -&gt; [0,3] -&gt; Pacific Ocean 
&nbsp;      [1,3] -&gt; [1,4] -&gt; Atlantic Ocean
[1,4]: [1,4] -&gt; [1,3] -&gt; [0,3] -&gt; Pacific Ocean 
&nbsp;      [1,4] -&gt; Atlantic Ocean
[2,2]: [2,2] -&gt; [1,2] -&gt; [0,2] -&gt; Pacific Ocean 
&nbsp;      [2,2] -&gt; [2,3] -&gt; [2,4] -&gt; Atlantic Ocean
[3,0]: [3,0] -&gt; Pacific Ocean 
&nbsp;      [3,0] -&gt; [4,0] -&gt; Atlantic Ocean
[3,1]: [3,1] -&gt; [3,0] -&gt; Pacific Ocean 
&nbsp;      [3,1] -&gt; [4,1] -&gt; Atlantic Ocean
[4,0]: [4,0] -&gt; Pacific Ocean 
       [4,0] -&gt; Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> heights = [[1]]
<strong>Output:</strong> [[0,0]]
<strong>Explanation:</strong> The water can flow from the only cell to the Pacific and Atlantic oceans.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == heights.length</code></li>
	<li><code>n == heights[r].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>0 &lt;= heights[r][c] &lt;= 10<sup>5</sup></code></li>
</ul>

//BFS
//TC & SC : O(numRows*numCols)

class Solution {   
public:
// Directions for moving right, down, up, and left
    const vector<vector<int>> DIRECTIONS = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    int numRows;
    int numCols;
    vector<vector<int>> landHeights;
    

    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
        // Check if input is empty
        if (matrix.empty() || matrix[0].empty()) {
            return {};
        }

        // Save initial values to member variables
        numRows = matrix.size();
        numCols = matrix[0].size();
        landHeights = matrix;
        
        // Setup each queue with cells adjacent to their respective ocean
        queue<pair<int, int>> pacificQueue;
        queue<pair<int, int>> atlanticQueue;

        for (int i = 0; i < numRows; i++) {
            pacificQueue.push({i, 0}); //left
            atlanticQueue.push({i, numCols - 1}); //right
        }

        for (int i = 0; i < numCols; i++) {
            pacificQueue.push({0, i}); //up
            atlanticQueue.push({numRows - 1, i}); //down
        }
        
        // Perform a BFS for each ocean to find all cells accessible by each ocean
        vector<vector<bool>> pacificReachable = bfs(pacificQueue);
        vector<vector<bool>> atlanticReachable = bfs(atlanticQueue);
        
        // Find all cells that can reach both oceans
        vector<vector<int>> commonCells;
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                if (pacificReachable[i][j] && atlanticReachable[i][j]) {
                    commonCells.push_back({i, j});
                }
            }
        }
        return commonCells;
    }
    
private:
    vector<vector<bool>> bfs(queue<pair<int, int>>& queue) {
        vector<vector<bool>> reachable(numRows, vector<bool>(numCols, false));
        while (!queue.empty()) {
            auto cell = queue.front();
            queue.pop();
            int row = cell.first;
            int col = cell.second;

            // This cell is reachable, so mark it
            reachable[row][col] = true;
            for (const auto& dir : DIRECTIONS) { // Check all 4 directions
                int newRow = row + dir[0];
                int newCol = col + dir[1];
                
                // Check if new cell is within bounds
                if (newRow < 0 || newRow >= numRows || newCol < 0 || newCol >= numCols) {
                    continue;
                }

                // Check that the new cell hasn't already been visited
                if (reachable[newRow][newCol]) {
                    continue;
                }

                // Check that the new cell has a higher or equal height,
                // So that water can flow from the new cell to the old cell
                if (landHeights[newRow][newCol] < landHeights[row][col]) {
                    continue;
                }

                // If we've gotten this far, that means the new cell is reachable
                queue.push({newRow, newCol});
            }
        }
        return reachable;
    }
};

23600