C++ BFS Solution Explained
Runtime: 76 ms, faster than 80.07% of C++ online submissions for Pacific Atlantic Water Flow.\nMemory Usage: 18.3 MB, less than 5.28% of C++ online submissions 
Runtime: 76 ms, faster than 80.07% of C++ online submissions for Pacific Atlantic Water Flow.\nMemory Usage: 18.3 MB, less than 5.28% of C++ online submissions for Pacific Atlantic Water Flow.\n\n\n```\nAs we want grid position from where water can reach both pacific and atlantic ocean, we can \nalso think it as grid positions which are reachable both from pacific and atlantic ocean boundary.\n\nRun BFS from all pacific boundary positions and track reachable grid positions, also run BFS from\natlantic boundary postions and track reachable grid positions. Grid position which are both\nreachable from pacific and atlantic are the result. From boundary positions water can flow in to\nneighbor grid positions which have greater or equal height.\n\nExample: \n\nYellow = Reachable from pacific boundary\nBlue = Reachable from atlantic boundary\nGreen = Reachable from both boundary\n```\n\n![image](https://assets.leetcode.com/users/images/e58f1f23-3ec8-4f7c-b5dd-87e19661bd95_1603217239.0575259.png)\n\n\n```\nclass Solution {\npublic:\n    \n    // run bfs to track reachable grid position from pacific or atlantic boundary\n    void bfs(int m, int n, queue<pair<int,int>>&Q, vector<vector<int>>& grid, vector<vector<bool>>&visited)\n    {\n        int rowOffset[] = {0,0,1,-1};\n        int colOffset[] = {1,-1,0,0};\n        int currentHeight;\n        int cx,cy;\n        int px,py;\n        \n        while(!Q.empty())\n        {\n            cx = Q.front().first;\n            cy = Q.front().second;\n            currentHeight = grid[cx][cy];\n            \n            Q.pop();\n            \n            for(int k=0;k<4;k++)\n            {\n                px = cx + rowOffset[k];\n                py = cy + colOffset[k];\n                \n                // child node can be reached from current nodes if \n                // greater or equal in height compared to current node height\n                if(px>=0 && px<m && py>=0 && py<n && grid[px][py]>=currentHeight && !visited[px][py])\n                {\n                    visited[px][py] = true;\n                    Q.push({px,py});\n                }\n            }\n        }\n    }\n    \n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& grid) {\n        \n        vector<vector<int>>result;\n        \n        int m = grid.size();\n        if(m==0) return result;\n        \n        int n = grid[0].size();\n        if(n==0) return result;\n        \n        // track nodes reachable from pacific boundary\n        vector<vector<bool>>pacificVisited(m,vector<bool>(n,false));\n        \n        // track nodes reachable from atlantic boundary\n        vector<vector<bool>>atlanticVisited(m,vector<bool>(n,false));\n        \n        queue<pair<int,int>>Q;\n        \n        // add pacific boundary nodes  in to queue\n        for(int i=0;i<n;i++)\n        {\n            if(!pacificVisited[0][i])\n            {\n                pacificVisited[0][i] = true;\n                Q.push({0,i});                    \n            }\n        }\n\n        // add pacific boundary nodes  in to queue       \n        for(int i=1;i<m;i++)\n        {\n            if(!pacificVisited[i][0])\n            {\n                pacificVisited[i][0] = true;\n                Q.push({i,0});                    \n            }\n        }\n        \n        // run bfs from pacific boundary nodes\n        bfs(m,n,Q,grid,pacificVisited);\n        \n        // add atlantic boundary nodes\n        for(int i=0;i<n;i++)\n        {\n            if(!atlanticVisited[m-1][i])\n            {\n                atlanticVisited[m-1][i] = true;\n                Q.push({m-1,i});                    \n            }\n        }\n\n        // add atlantic boundary nodes        \n        for(int i=0;i<m-1;i++)\n        {\n            if(!atlanticVisited[i][n-1])\n            {\n                atlanticVisited[i][n-1] = true;\n                Q.push({i,n-1});                    \n            }\n        }\n        \n        // run bfs from atlantic boundary nodes\n        bfs(m,n,Q,grid,atlanticVisited);\n        \n        // push nodes in to result vector which are reachable both from pacific and atlantic boundary\n        for(int i=0;i<m;i++)\n            for(int j=0;j<n;j++)\n                if(pacificVisited[i][j] && atlanticVisited[i][j])\n                    result.push_back({i,j});\n        \n        return result;\n    }\n};\n```
['Breadth-First Search', 'C']