Pacific Atlantic Water Flow | Easy solution | simple code using DFS
The idea is we are visiting every index that is reachable from pacific and atlantic and marking it as visited=true , and Finally we are checking if any index is
The idea is we are visiting every index that is reachable from pacific and atlantic and marking it as `visited=true` , and Finally we are checking if any index is reachable from both pacific and atlantic,then it is pushed into `res`.\n\n**DO UPVOTE** if you find it helpful!!\n```\nclass Solution {\npublic:\n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& mat) {\n        vector<vector<int>>res;\n        int n = mat.size();\n        if(n==0) return res;\n        int m = mat[0].size();\n        if(m==0) return res;\n        vector<vector<bool>>pc(n,vector<bool>(m,false));\n        vector<vector<bool>>at(n,vector<bool>(m,false));\n        \n        for(int i=0;i<n;i++){\n            dfs(mat,pc,i,0,INT_MIN);\n            dfs(mat,at,i,m-1,INT_MIN);\n        }\n        for(int j=0;j<m;j++){\n            dfs(mat,pc,0,j,INT_MIN);\n            dfs(mat,at,n-1,j,INT_MIN);\n        }\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                if(pc[i][j] && at[i][j])\n                    res.push_back({i,j});\n            }\n        }\n        return res;\n    }    \n    void dfs(vector<vector<int>>&mat,vector<vector<bool>> &visited,int i,int j,int prev){\n        if(i<0 || j<0 || i>=mat.size() || j>= mat[0].size() || visited[i][j] || mat[i][j]<prev) return;\n        visited[i][j] = true;\n            dfs(mat,visited,i+1,j,mat[i][j]);\n            dfs(mat,visited,i-1,j,mat[i][j]);\n            dfs(mat,visited,i,j+1,mat[i][j]);\n            dfs(mat,visited,i,j-1,mat[i][j]);\n    }\n};\n```\n
[]