- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1030.java
33 lines (29 loc) · 1.09 KB
/
_1030.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
packagecom.fishercoder.solutions.secondthousand;
importjava.util.LinkedList;
importjava.util.Queue;
publicclass_1030 {
publicstaticclassSolution1 {
publicint[][] allCellsDistOrder(intR, intC, intr0, intc0) {
int[][] result = newint[R * C][2];
Queue<int[]> queue = newLinkedList<>();
queue.offer(newint[] {r0, c0});
boolean[][] visited = newboolean[R][C];
inti = 0;
while (!queue.isEmpty()) {
int[] cell = queue.poll();
introw = cell[0];
intcol = cell[1];
if (row < 0 || row >= R || col < 0 || col >= C || visited[row][col]) {
continue;
}
visited[row][col] = true;
result[i++] = newint[] {row, col};
queue.offer(newint[] {row, col + 1});
queue.offer(newint[] {row + 1, col});
queue.offer(newint[] {row - 1, col});
queue.offer(newint[] {row, col - 1});
}
returnresult;
}
}
}