Skip to content

Commit a014ed1

Browse files
committed
feat: add solutions to lc problem: No.1030
No.1030.Matrix Cells in Distance Order
1 parent 2f93880 commit a014ed1

File tree

7 files changed

+316
-70
lines changed

7 files changed

+316
-70
lines changed

solution/1000-1099/1029.Two City Scheduling/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
**方法一:贪心**
5959

60-
选出 `aCost - bCost` 最小的 N 个人,让他们飞往 A 市,其余人飞往 B 市。
60+
选出 `aCost - bCost` 最小的 N 个人,让他们飞往 a 市,其余人飞往 b 市。
6161

6262
<!-- tabs:start -->
6363

solution/1000-1099/1030.Matrix Cells in Distance Order/README.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,133 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:BFS**
58+
59+
从坐标点 `(rCenter, cCenter)` 往上下左右 4 个方向进行搜索,将搜索到的坐标点添加到结果列表 ans 中,并记录访问过的节点,防止重复搜索。
60+
61+
搜索结束,返回结果列表 ans 即可。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
6066

6167
<!-- 这里可写当前语言的特殊实现逻辑 -->
6268

6369
```python
64-
70+
class Solution:
71+
def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:
72+
q = deque([(rCenter, cCenter)])
73+
vis = [[False] * cols for _ in range(rows)]
74+
vis[rCenter][cCenter] = True
75+
ans = []
76+
while q:
77+
for _ in range(len(q)):
78+
i, j = q.popleft()
79+
ans.append([i, j])
80+
for a, b in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
81+
x, y = i + a, j + b
82+
if 0 <= x < rows and 0 <= y < cols and not vis[x][y]:
83+
q.append((x, y))
84+
vis[x][y] = True
85+
return ans
6586
```
6687

6788
### **Java**
6889

6990
<!-- 这里可写当前语言的特殊实现逻辑 -->
7091

7192
```java
93+
class Solution {
94+
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
95+
Deque<int[]> q = new ArrayDeque<>();
96+
q.offer(new int[]{rCenter, cCenter});
97+
boolean[][] vis = new boolean[rows][cols];
98+
vis[rCenter][cCenter] = true;
99+
int[][] ans = new int[rows * cols][2];
100+
int idx = 0;
101+
int[] dirs = {-1, 0, 1, 0, -1};
102+
while (!q.isEmpty()) {
103+
for (int n = q.size(); n > 0; --n) {
104+
int[] p = q.poll();
105+
ans[idx++] = p;
106+
for (int k = 0; k < 4; ++k) {
107+
int x = p[0] + dirs[k];
108+
int y = p[1] + dirs[k + 1];
109+
if (x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y]) {
110+
q.offer(new int[]{x, y});
111+
vis[x][y] = true;
112+
}
113+
}
114+
}
115+
}
116+
return ans;
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
127+
queue<vector<int>> q;
128+
q.push({rCenter, cCenter});
129+
vector<vector<bool>> vis(rows, vector<bool>(cols));
130+
vis[rCenter][cCenter] = true;
131+
vector<vector<int>> ans;
132+
vector<int> dirs = {-1, 0, 1, 0, -1};
133+
while (!q.empty())
134+
{
135+
for (int n = q.size(); n > 0; --n)
136+
{
137+
auto p = q.front();
138+
q.pop();
139+
ans.push_back(p);
140+
for (int k = 0; k < 4; ++k)
141+
{
142+
int x = p[0] + dirs[k], y = p[1] + dirs[k + 1];
143+
if (x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y])
144+
{
145+
q.push({x, y});
146+
vis[x][y] = true;
147+
}
148+
}
149+
}
150+
}
151+
return ans;
152+
}
153+
};
154+
```
72155
156+
### **Go**
157+
158+
```go
159+
func allCellsDistOrder(rows int, cols int, rCenter int, cCenter int) [][]int {
160+
q := [][]int{{rCenter, cCenter}}
161+
vis := make([][]bool, rows)
162+
for i := range vis {
163+
vis[i] = make([]bool, cols)
164+
}
165+
vis[rCenter][cCenter] = true
166+
var ans [][]int
167+
dirs := []int{-1, 0, 1, 0, -1}
168+
for len(q) > 0 {
169+
for n := len(q); n > 0; n-- {
170+
p := q[0]
171+
q = q[1:]
172+
ans = append(ans, p)
173+
for k := 0; k < 4; k++ {
174+
x, y := p[0]+dirs[k], p[1]+dirs[k+1]
175+
if x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y] {
176+
q = append(q, []int{x, y})
177+
vis[x][y] = true
178+
}
179+
}
180+
}
181+
}
182+
return ans
183+
}
73184
```
74185

75186
### **...**

solution/1000-1099/1030.Matrix Cells in Distance Order/README_EN.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,118 @@ There are other answers that would also be accepted as correct, such as [[1,2],[
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:
58+
q = deque([(rCenter, cCenter)])
59+
vis = [[False] * cols for _ in range(rows)]
60+
vis[rCenter][cCenter] = True
61+
ans = []
62+
while q:
63+
for _ in range(len(q)):
64+
i, j = q.popleft()
65+
ans.append([i, j])
66+
for a, b in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
67+
x, y = i + a, j + b
68+
if 0 <= x < rows and 0 <= y < cols and not vis[x][y]:
69+
q.append((x, y))
70+
vis[x][y] = True
71+
return ans
5772
```
5873

5974
### **Java**
6075

6176
```java
77+
class Solution {
78+
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
79+
Deque<int[]> q = new ArrayDeque<>();
80+
q.offer(new int[]{rCenter, cCenter});
81+
boolean[][] vis = new boolean[rows][cols];
82+
vis[rCenter][cCenter] = true;
83+
int[][] ans = new int[rows * cols][2];
84+
int idx = 0;
85+
int[] dirs = {-1, 0, 1, 0, -1};
86+
while (!q.isEmpty()) {
87+
for (int n = q.size(); n > 0; --n) {
88+
int[] p = q.poll();
89+
ans[idx++] = p;
90+
for (int k = 0; k < 4; ++k) {
91+
int x = p[0] + dirs[k];
92+
int y = p[1] + dirs[k + 1];
93+
if (x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y]) {
94+
q.offer(new int[]{x, y});
95+
vis[x][y] = true;
96+
}
97+
}
98+
}
99+
}
100+
return ans;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
111+
queue<vector<int>> q;
112+
q.push({rCenter, cCenter});
113+
vector<vector<bool>> vis(rows, vector<bool>(cols));
114+
vis[rCenter][cCenter] = true;
115+
vector<vector<int>> ans;
116+
vector<int> dirs = {-1, 0, 1, 0, -1};
117+
while (!q.empty())
118+
{
119+
for (int n = q.size(); n > 0; --n)
120+
{
121+
auto p = q.front();
122+
q.pop();
123+
ans.push_back(p);
124+
for (int k = 0; k < 4; ++k)
125+
{
126+
int x = p[0] + dirs[k], y = p[1] + dirs[k + 1];
127+
if (x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y])
128+
{
129+
q.push({x, y});
130+
vis[x][y] = true;
131+
}
132+
}
133+
}
134+
}
135+
return ans;
136+
}
137+
};
138+
```
62139
140+
### **Go**
141+
142+
```go
143+
func allCellsDistOrder(rows int, cols int, rCenter int, cCenter int) [][]int {
144+
q := [][]int{{rCenter, cCenter}}
145+
vis := make([][]bool, rows)
146+
for i := range vis {
147+
vis[i] = make([]bool, cols)
148+
}
149+
vis[rCenter][cCenter] = true
150+
var ans [][]int
151+
dirs := []int{-1, 0, 1, 0, -1}
152+
for len(q) > 0 {
153+
for n := len(q); n > 0; n-- {
154+
p := q[0]
155+
q = q[1:]
156+
ans = append(ans, p)
157+
for k := 0; k < 4; k++ {
158+
x, y := p[0]+dirs[k], p[1]+dirs[k+1]
159+
if x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y] {
160+
q = append(q, []int{x, y})
161+
vis[x][y] = true
162+
}
163+
}
164+
}
165+
}
166+
return ans
167+
}
63168
```
64169

65170
### **...**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
4+
queue<vector<int>> q;
5+
q.push({rCenter, cCenter});
6+
vector<vector<bool>> vis(rows, vector<bool>(cols));
7+
vis[rCenter][cCenter] = true;
8+
vector<vector<int>> ans;
9+
vector<int> dirs = {-1, 0, 1, 0, -1};
10+
while (!q.empty())
11+
{
12+
for (int n = q.size(); n > 0; --n)
13+
{
14+
auto p = q.front();
15+
q.pop();
16+
ans.push_back(p);
17+
for (int k = 0; k < 4; ++k)
18+
{
19+
int x = p[0] + dirs[k], y = p[1] + dirs[k + 1];
20+
if (x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y])
21+
{
22+
q.push({x, y});
23+
vis[x][y] = true;
24+
}
25+
}
26+
}
27+
}
28+
return ans;
29+
}
30+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func allCellsDistOrder(rows int, cols int, rCenter int, cCenter int) [][]int {
2+
q := [][]int{{rCenter, cCenter}}
3+
vis := make([][]bool, rows)
4+
for i := range vis {
5+
vis[i] = make([]bool, cols)
6+
}
7+
vis[rCenter][cCenter] = true
8+
var ans [][]int
9+
dirs := []int{-1, 0, 1, 0, -1}
10+
for len(q) > 0 {
11+
for n := len(q); n > 0; n-- {
12+
p := q[0]
13+
q = q[1:]
14+
ans = append(ans, p)
15+
for k := 0; k < 4; k++ {
16+
x, y := p[0]+dirs[k], p[1]+dirs[k+1]
17+
if x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y] {
18+
q = append(q, []int{x, y})
19+
vis[x][y] = true
20+
}
21+
}
22+
}
23+
}
24+
return ans
25+
}

0 commit comments

Comments
 (0)