Clean BFS(Breadth-First Search) Solution
Summary\nVery simple easy to understand beginner friendly BFS code. In this cod we have to basically run BFS two times for the opposite sets of edges and mark t
# Summary\nVery simple easy to understand beginner friendly BFS code. In this cod we have to basically run BFS two times for the opposite sets of edges and mark the visited array for each bfs. Once that is done we can just iterate and check if a node was tagged by both the BFS, if so we can put it in our ans. Finally return the ans.\n\nIf any doubt please ask, will be happy to help, Happy coding :).\n\n\n# Code\n```\nclass Solution {\npublic:\n\n    const string PACIFIC_OCEAN = "pacific";\n    const string ATLANTIC_OCEAN = "atlantic";\n    \n    bool checkVisited(string ocean, pair<int, int> p){\n        if(ocean == ATLANTIC_OCEAN)\n            return p.second == 1;\n        return p.first == 1;\n    }\n\n    void bfs(string ocean, vector<vector<int>>& heights, vector<vector<pair<int, int>>>& visited, queue<pair<int, int>>& q, vector<pair<int, int>>& directions, int n, int m){\n        while(!q.empty()){\n            int row = q.front().first;\n            int col = q.front().second;\n            q.pop();\n\n            for(auto it : directions){\n                int x = row + it.first;\n                int y = col + it.second;\n\n                if(x<0 || x>=n || y<0 || y>=m || heights[x][y] < heights[row][col] || checkVisited(ocean, visited[x][y]))\n                    continue;\n\n                if(ocean == ATLANTIC_OCEAN)\n                    visited[x][y].second = 1;\n                else\n                    visited[x][y].first = 1;\n\n                q.push(make_pair(x, y));\n            }\n        }\n    }\n\n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {\n        int n = heights.size();\n        int m = heights[0].size();\n\n        vector<vector<pair<int, int>>> visited(n, vector<pair<int, int>> (m, {0, 0}));\n        vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n\n        queue<pair<int, int>> q;\n        for(int i=0; i<m; i++){\n            visited[n-1][i].second = 1;\n            q.push({n-1, i});\n        } \n        for(int i=0; i<n; i++){\n            visited[i][m-1].second = 1;\n            q.push({i, m-1});\n        } \n\n        bfs(ATLANTIC_OCEAN ,heights, visited, q, directions, n, m);\n\n        for(int i=0; i<n; i++){\n            visited[i][0].first = 1;\n            q.push({i, 0});\n        } \n        for(int i=0; i<m; i++){\n            visited[0][i].first = 1;\n             q.push({0, i});\n        }\n\n        bfs(PACIFIC_OCEAN, heights, visited, q, directions, n, m);\n\n        vector<vector<int>> ans;\n\n        for(int i=0; i<n; i++){\n            for(int j=0; j<m; j++){\n                pair<int, int> it = visited[i][j];\n                if(it.first && it.second)\n                    ans.push_back({i, j});\n            }\n        }\n\n        return ans;\n\n    }\n};\n\n```
['Breadth-First Search', 'C++']