C++ Simple Readable Recursive DFS Solution
We need to find cells that can reach Pacific and Atlantic.\nSo we keep a vector "states": 1 = can reach Pacific, 2 = can reach Atlantic, 3 = can reach both.\n\n
We need to find cells that can reach Pacific and Atlantic.\nSo we keep a vector "states": 1 = can reach Pacific, 2 = can reach Atlantic, 3 = can reach both.\n```\nclass Solution {\npublic:\n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {\n        if (!matrix.size() || !matrix[0].size()) \n            return res;\n        \n        n = matrix.size();\n        m = matrix[0].size();\n        states.assign(n,vector<int> (m, 0));\n        \n        for (int i = 0; i < n; i++) {\n            recDFS(matrix, i, 0, INT_MIN, 1);\n            recDFS(matrix, i, m-1, INT_MIN, 2);\n        }\n        \n        for (int i = 0; i < m; i++) {\n            recDFS(matrix, 0, i, INT_MIN, 1);\n            recDFS(matrix, n-1, i, INT_MIN, 2);\n        }\n        \n        return res;\n    }\n    \nprivate:\n    vector<vector<int>> res, states;\n    vector<int> x_points = {1, 0, -1, 0};\n    vector<int> y_points = {0, 1, 0, -1};\n    int m, n;\n    \n    bool isValid(int x, int y) {\n        return x < n and x >= 0 and y < m and y >= 0;\n    }\n    \n    void recDFS(vector<vector<int>>& mat, int x, int y, int prev, int prev_state) {\n        if (!isValid(x, y) || mat[x][y] < prev || states[x][y] == prev_state || states[x][y] == 3) \n            return;\n        \n        states[x][y] += prev_state;\n        \n        if (states[x][y] == 3) \n            res.push_back({x, y});\n        \n        for (int i = 0; i < 4; i++) {\n            recDFS(mat, x + x_points[i], y + y_points[i], mat[x][y], prev_state);\n        }\n    }\n};\n```\n**Like it? please upvote!**
['C']