Runtime: 64 ms, faster than 99.78% of C++ online submissions for Pacific Atlantic Water Flow.
STEP 1- if we start from the cells connected to altantic ocean and visit all cells having height greater than current cell (water can only flow from a cell to a
**STEP 1**- if we start from the cells connected to altantic ocean and visit all cells having height greater than current cell (**water can only flow from a cell to another one with height equal or lower**), we are able to reach some subset of cells (let\'s call them A).\n \n ![image](https://assets.leetcode.com/users/images/923e6cc7-ab34-41e0-8126-38f828f11b38_1654803119.413273.png)\n \n**STEP-2**\n Next, we start from the cells connected to pacific ocean and repeat the same process, we find another subset (let\'s call this one B).\n \n![image](https://assets.leetcode.com/users/images/28272544-fe83-43a5-bf1b-fb7e7ceff7d4_1654803241.1074493.png)\n\n**The final answer we get will be the intersection of sets A and B (A \u2229 B).**\n\n![image](https://assets.leetcode.com/users/images/79807c93-0015-4b10-b162-169d901e7444_1654803270.5645876.png)\n\n```\nclass Solution\n{\n\n\tvoid bfs(vector<vector<int>> &h, queue<pair<int, int>> q, int n, int m, vector<vector<int>> &visi)\n\t{\n\t\tint ar1[] = {0, 0, 1, -1};\n\t\tint ar2[] = {1, -1, 0, 0};\n\t\twhile (!q.empty())\n\t\t{\n\t\t\tint x = q.front().first;\n\t\t\tint y = q.front().second;\n\t\t\tq.pop();\n\t\t\tfor (int i = 0; i < 4; i++)\n\t\t\t{\n\t\t\t\tint xn = x + ar1[i];\n\t\t\t\tint yn = y + ar2[i];\n\t\t\t\tif (xn < 0 || yn < 0 || xn >= n || yn >= m || visi[xn][yn] == 1)\n\t\t\t\t\tcontinue;\n\t\t\t\tif (h[x][y] <= h[xn][yn])\n\t\t\t\t\tq.push({xn, yn}), visi[xn][yn] = 1;\n\t\t\t}\n\t\t}\n\t}\n\npublic:\n\tvector<vector<int>> pacificAtlantic(vector<vector<int>> &heights)\n\t{\n        int n = heights.size();\n\t\tint m = heights[0].size();\n\t\tvector<vector<int>> visi1(n, vector<int>(m, 0)); // for Pacific Ocean\n\t\tvector<vector<int>> visi2(n, vector<int>(m, 0)); // for Atlantic Ocean\n\n\t\tqueue<pair<int, int>> q;\n\t\t// STEP-1\n\t\tfor (int i = 0; i < m; i++)\n\t\t\tq.push({0, i}), visi1[0][i] = 1;\n\t\tfor (int j = 0; j < n; j++)\n\t\t\tq.push({j, 0}), visi1[j][0] = 1;\n\n\t\tbfs(heights, q, n, m, visi1); // for pacific ocean which are greator and equal to value\n\n\t\t// to empty the queue for atlantic Ocean\n\t\twhile (!q.empty())\n\t\t\tq.pop();\n\t\t// STEP-2\n\t\tfor (int i = 0; i < m; i++)\n\t\t\tq.push({n - 1, i}), visi2[n - 1][i] = 1;\n\t\tfor (int j = 0; j < n; j++)\n\t\t\tq.push({j, m - 1}), visi2[j][m - 1] = 1;\n\n\t\tbfs(heights, q, n, m, visi2); // for Atalantic ocean which are greator and equal to value\n\t    // STEP-3\n\t\tvector<vector<int>> ans;\t  // to check the common in both visi1 and visi2 vector;\n\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tfor (int j = 0; j < m; j++)\n\t\t\t\tif (visi1[i][j] == visi2[i][j] and visi1[i][j] == 1)\n\t\t\t\t\tans.push_back({i, j});\n\t\t}\n\t\treturn ans;\n\t}\n};\n```
['Breadth-First Search', 'Graph', 'C', 'C++']