Merge pull request #2657 from sxdtywm/patch-3

Update 0101.孤岛的总面积.md of other languages
This commit is contained in:
程序员Carl
2024-07-31 10:22:15 +08:00
committed by GitHub

View File

@ -185,6 +185,77 @@ int main() {
### Java
``` java
import java.util.*;
public class Main {
private static int count = 0;
private static final int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; // 四个方向
private static void bfs(int[][] grid, int x, int y) {
Queue<int[]> que = new LinkedList<>();
que.add(new int[]{x, y});
grid[x][y] = 0; // 只要加入队列,立刻标记
count++;
while (!que.isEmpty()) {
int[] cur = que.poll();
int curx = cur[0];
int cury = cur[1];
for (int i = 0; i < 4; i++) {
int nextx = curx + dir[i][0];
int nexty = cury + dir[i][1];
if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 越界了,直接跳过
if (grid[nextx][nexty] == 1) {
que.add(new int[]{nextx, nexty});
count++;
grid[nextx][nexty] = 0; // 只要加入队列立刻标记
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] grid = new int[n][m];
// 读取网格
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
grid[i][j] = scanner.nextInt();
}
}
// 从左侧边,和右侧边向中间遍历
for (int i = 0; i < n; i++) {
if (grid[i][0] == 1) bfs(grid, i, 0);
if (grid[i][m - 1] == 1) bfs(grid, i, m - 1);
}
// 从上边和下边向中间遍历
for (int j = 0; j < m; j++) {
if (grid[0][j] == 1) bfs(grid, 0, j);
if (grid[n - 1][j] == 1) bfs(grid, n - 1, j);
}
count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1) bfs(grid, i, j);
}
}
System.out.println(count);
}
}
```
### Python
```python
from collections import deque
@ -238,6 +309,97 @@ print(count)
```
### Go
``` go
package main
import (
"fmt"
)
var count int
var dir = [4][2]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向
func bfs(grid [][]int, x, y int) {
queue := [][2]int{{x, y}}
grid[x][y] = 0 // 只要加入队列,立刻标记
count++
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
curx, cury := cur[0], cur[1]
for i := 0; i < 4; i++ {
nextx := curx + dir[i][0]
nexty := cury + dir[i][1]
if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) {
continue // 越界了,直接跳过
}
if grid[nextx][nexty] == 1 {
queue = append(queue, [2]int{nextx, nexty})
count++
grid[nextx][nexty] = 0 // 只要加入队列立刻标记
}
}
}
}
func main() {
var n, m int
fmt.Scan(&n, &m)
grid := make([][]int, n)
for i := range grid {
grid[i] = make([]int, m)
}
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
fmt.Scan(&grid[i][j])
}
}
// 从左侧边,和右侧边向中间遍历
for i := 0; i < n; i++ {
if grid[i][0] == 1 {
bfs(grid, i, 0)
}
if grid[i][m-1] == 1 {
bfs(grid, i, m-1)
}
}
// 从上边和下边向中间遍历
for j := 0; j < m; j++ {
if grid[0][j] == 1 {
bfs(grid, 0, j)
}
if grid[n-1][j] == 1 {
bfs(grid, n-1, j)
}
}
// 清空之前的计数
count = 0
// 遍历所有位置
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if grid[i][j] == 1 {
bfs(grid, i, j)
}
}
}
fmt.Println(count)
}
```
### Rust
### Javascript