[Java] BFS Easy to Understand | Let water flow from respective occean sides
\nclass Solution {\n    public List<List<Integer>> pacificAtlantic(int[][] matrix) {\n        if(matrix.length == 0)\n            return new ArrayList();\n     
```\nclass Solution {\n    public List<List<Integer>> pacificAtlantic(int[][] matrix) {\n        if(matrix.length == 0)\n            return new ArrayList();\n        \n        int R = matrix.length, C = matrix[0].length;\n        \n        boolean[][] pacific = new boolean[R][C];\n        boolean[][] atlantic = new boolean[R][C];\n        \n        for(int i = 0; i < R; i++) {\n            bfs(i, 0, matrix, pacific);\n            bfs(i, C - 1, matrix, atlantic);\n        }\n        \n        for(int j = 0; j < C; j++) {\n            bfs(0, j, matrix, pacific);\n            bfs(R - 1, j, matrix, atlantic);\n        }\n        \n        List<List<Integer>> list = new ArrayList();\n        for(int i = 0; i < R; i++) {\n            for(int j = 0; j < C; j++) {\n                if(pacific[i][j] && atlantic[i][j]) {\n                    list.add(Arrays.asList(i, j));\n                }\n            }\n        }\n        \n        return list;\n    }\n    \n    private void bfs(int x, int y, int[][] matrix, boolean[][] visited) {\n        int R = matrix.length, C = matrix[0].length;\n        Queue<int[]> q = new LinkedList<>();\n        q.add(new int[]{x, y});\n        visited[x][y] = true;\n        int[][] dir = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n        while(q.size() > 0) {\n            x = q.peek()[0];\n            y = q.peek()[1];\n            q.poll();\n            \n            for(int[] d : dir) {\n                int nx = x + d[0];\n                int ny = y + d[1];\n                \n                if(nx >= 0 && nx < R && ny >= 0 && ny < C && matrix[x][y] <= matrix[nx][ny] && !visited[nx][ny]) {\n                    q.add(new int[]{nx, ny});\n                    visited[nx][ny] = true;\n                }\n            }\n        }\n    }\n}\n```
[]