53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
- BFS。
56
+ ** 方法一:BFS**
57
+
58
+ 我们可以将所有陆地单元格加入队列 $q$ 中,如果队列为空,或者队列中元素个数等于网格中的单元格个数,则说明网格中只有陆地或者海洋,返回 $-1$。
59
+
60
+ 否则,我们从陆地单元格开始进行广度优先搜索。定义初始步数 $ans=-1$。
61
+
62
+ 在每一轮搜索中,我们将队列中的所有单元格向四个方向扩散,若单元格是海洋单元格,则将其标记为陆地单元格,并加入队列。在一轮扩散完成后,我们将步数加 $1$。重复这一过程,直到队列为空。
63
+
64
+ 最后,我们返回步数 $ans$。
65
+
66
+ 时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是网格的边长。
57
67
58
68
<!-- tabs:start -->
59
69
@@ -89,7 +99,7 @@ class Solution:
89
99
class Solution {
90
100
public int maxDistance (int [][] grid ) {
91
101
int n = grid. length;
92
- Deque<int[]> q = new LinkedList <> ();
102
+ Deque<int[]> q = new ArrayDeque <> ();
93
103
for (int i = 0 ; i < n; ++ i) {
94
104
for (int j = 0 ; j < n; ++ j) {
95
105
if (grid[i][j] == 1 ) {
@@ -98,65 +108,25 @@ class Solution {
98
108
}
99
109
}
100
110
int ans = - 1 ;
101
- boolean valid = false ;
111
+ if (q. isEmpty() || q. size() == n * n) {
112
+ return ans;
113
+ }
102
114
int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
103
115
while (! q. isEmpty()) {
104
- ++ ans;
105
- for (int k = q. size(); k > 0 ; -- k) {
116
+ for (int i = q. size(); i > 0 ; -- i) {
106
117
int [] p = q. poll();
107
- for (int i = 0 ; i < 4 ; ++ i) {
108
- int x = p[0 ] + dirs[i];
109
- int y = p[1 ] + dirs[i + 1 ];
118
+ for (int k = 0 ; k < 4 ; ++ k) {
119
+ int x = p[0 ] + dirs[k], y = p[1 ] + dirs[k + 1 ];
110
120
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0 ) {
111
- valid = true ;
112
121
grid[x][y] = 1 ;
113
122
q. offer(new int [] {x, y});
114
123
}
115
124
}
116
125
}
126
+ ++ ans;
117
127
}
118
- return valid ? ans : - 1 ;
119
- }
120
- }
121
- ```
122
-
123
- ### ** TypeScript**
124
-
125
- ``` ts
126
- function maxDistance(grid : number [][]): number {
127
- const m = grid .length ,
128
- n = grid [0 ].length ;
129
- let queue: Array <Array <number >> = [];
130
- for (let i = 0 ; i < m ; i ++ ) {
131
- for (let j = 0 ; j < n ; j ++ ) {
132
- if (grid [i ][j ]) {
133
- queue .push ([i , j ]);
134
- }
135
- }
136
- }
137
- if ([0 , m * n ].includes (queue .length )) return - 1 ;
138
- const directions = [
139
- [0 , 1 ],
140
- [0 , - 1 ],
141
- [1 , 0 ],
142
- [- 1 , 0 ],
143
- ];
144
- let depth = 1 ;
145
- while (queue .length ) {
146
- depth += 1 ;
147
- let nextLevel: Array <Array <number >> = [];
148
- for (let [x, y] of queue ) {
149
- for (let [dx, dy] of directions ) {
150
- const [i, j] = [x + dx , y + dy ];
151
- if (i >= 0 && i < m && j >= 0 && j < n && ! grid [i ][j ]) {
152
- grid [i ][j ] = depth ;
153
- nextLevel .push ([i , j ]);
154
- }
155
- }
156
- }
157
- queue = nextLevel ;
128
+ return ans;
158
129
}
159
- return depth - 2 ;
160
130
}
161
131
```
162
132
@@ -167,32 +137,34 @@ class Solution {
167
137
public:
168
138
int maxDistance(vector<vector<int >>& grid) {
169
139
int n = grid.size();
170
- typedef pair<int, int> pii;
171
- queue<pii > q;
172
- for (int i = 0; i < n; ++i)
173
- for (int j = 0; j < n; ++j)
174
- if (grid[ i] [ j ] == 1)
175
- q.push({i, j});
140
+ queue<pair<int, int>> q;
141
+ for (int i = 0; i < n; ++i) {
142
+ for (int j = 0; j < n; ++j) {
143
+ if (grid[ i] [ j ] ) {
144
+ q.emplace(i, j);
145
+ }
146
+ }
147
+ }
176
148
int ans = -1;
177
- bool valid = false;
178
- vector<int > dirs = {-1, 0, 1, 0, -1};
149
+ if (q.empty() || q.size() == n * n) {
150
+ return ans;
151
+ }
152
+ int dirs[ 5] = {-1, 0, 1, 0, -1};
179
153
while (!q.empty()) {
180
- ++ans;
181
- for (int k = q.size(); k > 0; --k) {
182
- pii p = q.front();
154
+ for (int m = q.size(); m; --m) {
155
+ auto [ i, j] = q.front();
183
156
q.pop();
184
- for (int i = 0; i < 4; ++i) {
185
- int x = p.first + dirs[ i] ;
186
- int y = p.second + dirs[ i + 1] ;
187
- if (x >= 0 && x < n && y >= 0 && y < n && grid[ x] [ y ] == 0) {
188
- valid = true;
157
+ for (int k = 0; k < 4; ++k) {
158
+ int x = i + dirs[ k] , y = j + dirs[ k + 1] ;
159
+ if (x >= 0 && x < n && y >= 0 && y < n && !grid[ x] [ y ] ) {
189
160
grid[ x] [ y ] = 1;
190
- q.push({ x, y} );
161
+ q.emplace( x, y);
191
162
}
192
163
}
193
164
}
165
+ ++ans;
194
166
}
195
- return valid ? ans : -1 ;
167
+ return ans;
196
168
}
197
169
};
198
170
```
@@ -202,36 +174,70 @@ public:
202
174
```go
203
175
func maxDistance(grid [][]int) int {
204
176
n := len(grid)
205
- var q [][]int
206
- for i := 0; i < n; i++ {
207
- for j := 0; j < n; j++ {
208
- if grid[i][j] == 1 {
209
- q = append(q, []int{i, j})
177
+ q := [][2 ]int{}
178
+ for i, row := range grid {
179
+ for j, v := range row {
180
+ if v == 1 {
181
+ q = append(q, [2 ]int{i, j})
210
182
}
211
183
}
212
184
}
213
185
ans := -1
214
- valid := false
215
- dirs := []int{-1, 0, 1, 0, -1}
186
+ if len(q) == 0 || len(q) == n*n {
187
+ return ans
188
+ }
189
+ dirs := [5]int{-1, 0, 1, 0, -1}
216
190
for len(q) > 0 {
217
- ans++
218
- for k := len(q); k > 0; k-- {
191
+ for i := len(q); i > 0; i-- {
219
192
p := q[0]
220
193
q = q[1:]
221
- for i := 0; i < 4; i ++ {
222
- x, y := p[0]+dirs[i ], p[1]+dirs[i +1]
194
+ for k := 0; k < 4; k ++ {
195
+ x, y := p[0]+dirs[k ], p[1]+dirs[k +1]
223
196
if x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0 {
224
- valid = true
225
197
grid[x][y] = 1
226
- q = append(q, []int{x, y})
198
+ q = append(q, [2 ]int{x, y})
227
199
}
228
200
}
229
201
}
202
+ ans++
230
203
}
231
- if valid {
232
- return ans
233
- }
234
- return -1
204
+ return ans
205
+ }
206
+ ```
207
+
208
+ ### ** TypeScript**
209
+
210
+ ``` ts
211
+ function maxDistance(grid : number [][]): number {
212
+ const n = grid .length ;
213
+ const q: [number , number ][] = [];
214
+ for (let i = 0 ; i < n ; ++ i ) {
215
+ for (let j = 0 ; j < n ; ++ j ) {
216
+ if (grid [i ][j ] === 1 ) {
217
+ q .push ([i , j ]);
218
+ }
219
+ }
220
+ }
221
+ let ans = - 1 ;
222
+ if (q .length === 0 || q .length === n * n ) {
223
+ return ans ;
224
+ }
225
+ const dirs: number [] = [- 1 , 0 , 1 , 0 , - 1 ];
226
+ while (q .length > 0 ) {
227
+ for (let m = q .length ; m ; -- m ) {
228
+ const [i, j] = q .shift ()! ;
229
+ for (let k = 0 ; k < 4 ; ++ k ) {
230
+ const x = i + dirs [k ];
231
+ const y = j + dirs [k + 1 ];
232
+ if (x >= 0 && x < n && y >= 0 && y < n && grid [x ][y ] === 0 ) {
233
+ grid [x ][y ] = 1 ;
234
+ q .push ([x , y ]);
235
+ }
236
+ }
237
+ }
238
+ ++ ans ;
239
+ }
240
+ return ans ;
235
241
}
236
242
```
237
243
0 commit comments