-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
26 lines (26 loc) · 966 Bytes
/
Solution.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
class Solution {
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[]{rCenter, cCenter});
boolean[][] vis = new boolean[rows][cols];
vis[rCenter][cCenter] = true;
int[][] ans = new int[rows * cols][2];
int idx = 0;
int[] dirs = {-1, 0, 1, 0, -1};
while (!q.isEmpty()) {
for (int n = q.size(); n > 0; --n) {
int[] p = q.poll();
ans[idx++] = p;
for (int k = 0; k < 4; ++k) {
int x = p[0] + dirs[k];
int y = p[1] + dirs[k + 1];
if (x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y]) {
q.offer(new int[]{x, y});
vis[x][y] = true;
}
}
}
}
return ans;
}
}