Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3820536

Browse files
authored
Update 0101.孤岛的总面积.md of other languages
Update 0101.孤岛的总面积.md of other languages
1 parent f844fb2 commit 3820536

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

‎problems/kamacoder/0101.孤岛的总面积.md‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,77 @@ int main() {
185185

186186
### Java
187187

188+
``` java
189+
190+
import java.util.*;
191+
192+
public class Main {
193+
private static int count = 0;
194+
private static final int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; // 四个方向
195+
196+
private static void bfs(int[][] grid, int x, int y) {
197+
Queue<int[]> que = new LinkedList<>();
198+
que.add(new int[]{x, y});
199+
grid[x][y] = 0; // 只要加入队列,立刻标记
200+
count++;
201+
while (!que.isEmpty()) {
202+
int[] cur = que.poll();
203+
int curx = cur[0];
204+
int cury = cur[1];
205+
for (int i = 0; i < 4; i++) {
206+
int nextx = curx + dir[i][0];
207+
int nexty = cury + dir[i][1];
208+
if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 越界了,直接跳过
209+
if (grid[nextx][nexty] == 1) {
210+
que.add(new int[]{nextx, nexty});
211+
count++;
212+
grid[nextx][nexty] = 0; // 只要加入队列立刻标记
213+
}
214+
}
215+
}
216+
}
217+
218+
public static void main(String[] args) {
219+
Scanner scanner = new Scanner(System.in);
220+
int n = scanner.nextInt();
221+
int m = scanner.nextInt();
222+
int[][] grid = new int[n][m];
223+
224+
// 读取网格
225+
for (int i = 0; i < n; i++) {
226+
for (int j = 0; j < m; j++) {
227+
grid[i][j] = scanner.nextInt();
228+
}
229+
}
230+
231+
// 从左侧边,和右侧边向中间遍历
232+
for (int i = 0; i < n; i++) {
233+
if (grid[i][0] == 1) bfs(grid, i, 0);
234+
if (grid[i][m - 1] == 1) bfs(grid, i, m - 1);
235+
}
236+
237+
// 从上边和下边向中间遍历
238+
for (int j = 0; j < m; j++) {
239+
if (grid[0][j] == 1) bfs(grid, 0, j);
240+
if (grid[n - 1][j] == 1) bfs(grid, n - 1, j);
241+
}
242+
243+
count = 0;
244+
for (int i = 0; i < n; i++) {
245+
for (int j = 0; j < m; j++) {
246+
if (grid[i][j] == 1) bfs(grid, i, j);
247+
}
248+
}
249+
250+
System.out.println(count);
251+
}
252+
}
253+
254+
255+
256+
```
257+
258+
188259
### Python
189260
```python
190261
from collections import deque
@@ -238,6 +309,97 @@ print(count)
238309
```
239310
### Go
240311

312+
``` go
313+
314+
package main
315+
316+
import (
317+
"fmt"
318+
)
319+
320+
var count int
321+
var dir = [4][2]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向
322+
323+
func bfs(grid [][]int, x, y int) {
324+
queue := [][2]int{{x, y}}
325+
grid[x][y] = 0 // 只要加入队列,立刻标记
326+
count++
327+
328+
for len(queue) > 0 {
329+
cur := queue[0]
330+
queue = queue[1:]
331+
curx, cury := cur[0], cur[1]
332+
333+
for i := 0; i < 4; i++ {
334+
nextx := curx + dir[i][0]
335+
nexty := cury + dir[i][1]
336+
337+
if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) {
338+
continue // 越界了,直接跳过
339+
}
340+
341+
if grid[nextx][nexty] == 1 {
342+
queue = append(queue, [2]int{nextx, nexty})
343+
count++
344+
grid[nextx][nexty] = 0 // 只要加入队列立刻标记
345+
}
346+
}
347+
}
348+
}
349+
350+
func main() {
351+
var n, m int
352+
fmt.Scan(&n, &m)
353+
354+
grid := make([][]int, n)
355+
for i := range grid {
356+
grid[i] = make([]int, m)
357+
}
358+
359+
for i := 0; i < n; i++ {
360+
for j := 0; j < m; j++ {
361+
fmt.Scan(&grid[i][j])
362+
}
363+
}
364+
365+
// 从左侧边,和右侧边向中间遍历
366+
for i := 0; i < n; i++ {
367+
if grid[i][0] == 1 {
368+
bfs(grid, i, 0)
369+
}
370+
if grid[i][m-1] == 1 {
371+
bfs(grid, i, m-1)
372+
}
373+
}
374+
375+
// 从上边和下边向中间遍历
376+
for j := 0; j < m; j++ {
377+
if grid[0][j] == 1 {
378+
bfs(grid, 0, j)
379+
}
380+
if grid[n-1][j] == 1 {
381+
bfs(grid, n-1, j)
382+
}
383+
}
384+
385+
// 清空之前的计数
386+
count = 0
387+
388+
// 遍历所有位置
389+
for i := 0; i < n; i++ {
390+
for j := 0; j < m; j++ {
391+
if grid[i][j] == 1 {
392+
bfs(grid, i, j)
393+
}
394+
}
395+
}
396+
397+
fmt.Println(count)
398+
}
399+
400+
401+
```
402+
241403
### Rust
242404

243405
### Javascript

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /