BFS
class Solution {
public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
int[][] Matrix = new int[R*C][2];
Queue<int[]> queue = new LinkedList();
boolean[][] visited = new boolean[R][C];
int idx = 0;
queue.offer(new int[]{r0, c0});
while (!queue.isEmpty()) {
int[] cell = queue.poll();
int row = cell[0];
int colume = cell[1];
if (row<0 || row>=R || colume<0 || colume>=C || visited[row][colume])
continue;
Matrix[idx++] = cell;
visited[row][colume] = true;
queue.offer(new int[]{row-1,colume});
queue.offer(new int[]{row,colume-1});
queue.offer(new int[]{row+1,colume});
queue.offer(new int[]{row,colume+1});
}
return Matrix;
}
}