Think the other way! 💡 Ocean to Land DFS ✅ [C++] || Explaind 🔥
Intution \uD83D\uDCA1\ninstead of going from land to ocean\nwe will do a DFS from both the oceans towards the land\nif the cell of the land can be visited from 
## **Intution** \uD83D\uDCA1\ninstead of going from land to ocean\nwe will do a *DFS* from **both the oceans** towards the land\nif the cell of the land can be visited from both oceans, vice-versa of that (that is asked in question) is also **true**\n```\nclass Solution {\nprivate:\n    void dfs(vector<vector<int>>& land, vector<vector<bool>> &oceanToLand, int i, int j, int rows, int cols) {\n        if(i < 0 || i >= rows || j < 0 || j >= cols || oceanToLand[i][j] == true)\n            return;\n        \n        oceanToLand[i][j] = true; // marking visited, means ocean water can come to that land\n        \n        // just checking the neighbouring cells\n        if(i + 1 < rows && land[i + 1][j] >= land[i][j])\n            dfs(land, oceanToLand, i + 1, j, rows, cols);\n        \n        if(i - 1 >= 0 && land[i - 1][j] >= land[i][j])\n            dfs(land, oceanToLand, i - 1, j, rows, cols);\n        \n        if(j + 1 < cols && land[i][j + 1] >= land[i][j])\n            dfs(land, oceanToLand, i, j + 1, rows, cols);\n        \n        if(j - 1 >= 0 && land[i][j - 1] >= land[i][j])\n            dfs(land, oceanToLand, i, j - 1, rows, cols);\n    }\n\npublic:\n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {\n        \n        vector<vector<int>> ans;\n        \n        int rows = heights.size();\n        int cols = heights[0].size();\n        \n        vector<vector<bool>> pacificToLand(rows, vector<bool>(cols, false));\n        vector<vector<bool>> atlanticToLand(rows, vector<bool>(cols, false));\n        \n        // column 0 is pacific ocean\'s boundary and last column (cols - 1) is atlantic\'s\n        for(int i = 0; i < rows; ++i) {\n            dfs(heights, pacificToLand, i, 0, rows, cols);\n            dfs(heights, atlanticToLand, i, cols - 1, rows, cols);\n        }\n        \n        // row 0 is pacific ocean\'s boundary and last row (rows - 1) is atlantic\'s\n        for(int i = 0; i < cols; ++i) {\n            dfs(heights, pacificToLand, 0, i, rows, cols);\n            dfs(heights, atlanticToLand, rows - 1, i, rows, cols);\n        }\n        \n        \n        // if from *both* oceans, we can reach to a particular cell of land, then vice-versa is also true\n        for(int i = 0; i < rows; ++i) {\n            for(int j = 0; j < cols; ++j) {\n                if(pacificToLand[i][j] && atlanticToLand[i][j]) {\n                    ans.push_back({i, j});\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\nIf you have any doubts, *Please ask in the Comments* \nIf it helped you, **Please UpVote** \uD83D\uDD3C :)
['Depth-First Search', 'Graph', 'C', 'C++']