JAVA - BFS Solution (Easy to Understand)
\npublic int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n\tint[][] sol = new int[R*C][2];\n\tQueue<int[]> q = new LinkedList<>();\n\tboolean[][] visi
```\npublic int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n\tint[][] sol = new int[R*C][2];\n\tQueue<int[]> q = new LinkedList<>();\n\tboolean[][] visited = new boolean[R][C];\n\tq.offer(new int[] {r0,c0});\n\tint i = 0;\n\twhile(!q.isEmpty()) {\n\t\tint[] curr = q.poll();\n\t\tint r = curr[0], c = curr[1];\n\t\tif(r<0 || c<0 || r>=R || c>=C)\n\t\t\tcontinue;\n\t\tif(visited[r][c])\n\t\t\tcontinue;\n\t\tvisited[r][c] = true;\n\t\tsol[i++] = curr;\n\t\tq.offer(new int[]{r,c+1});\n\t\tq.offer(new int[]{r,c-1});\n\t\tq.offer(new int[]{r+1,c});\n\t\tq.offer(new int[]{r-1,c});\n\t}\n\treturn sol;\n}\n```
[]