Java Breadth First Search
This problem is asking to list the cells in the order you would reach them in an outwards fill from (rCenter, cCenter). This matches the way a bfs iterates over
This problem is asking to list the cells in the order you would reach them in an outwards fill from (rCenter, cCenter). This matches the way a bfs iterates over nodes, so if we treat each coordinate point like a node, bfs is a valid solution.\n\n# Code\n```\nclass Solution {\n    public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {\n        //bfs\n        final int[][] directions = new int[][]{{0, -1}, {-1, 0}, {1, 0}, {0, 1}};\n\n        Queue<Integer[]> q = new LinkedList<>();\n        boolean[][] seen = new boolean[rows][cols];\n\n        q.add(new Integer[]{rCenter, cCenter});\n        seen[rCenter][cCenter] = true;\n\n        int[][] result = new int[rows * cols][];\n        int i = 0;\n        while(!q.isEmpty()){\n            Integer[] curr = q.remove();\n            result[i++] = new int[]{curr[0], curr[1]};\n\n            for(int[] d : directions){\n                int r = curr[0] + d[0];\n                int c = curr[1] + d[1];\n\n                if(r >= 0 && c >= 0 && r < rows && c < cols && !seen[r][c]){\n                    seen[r][c] = true;\n                    q.add(new Integer[]{r, c});\n                }\n            }\n        }\n        return result;\n    }\n}\n```
['Breadth-First Search', 'Java']