mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Update 0102.沉没孤岛.md
Update 0102.沉没孤岛.md
This commit is contained in:
@ -138,8 +138,128 @@ int main() {
|
||||
|
||||
### Java
|
||||
|
||||
```JAVA
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
static int[][] dir = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} }; // 保存四个方向
|
||||
|
||||
public static void dfs(int[][] grid, int x, int y) {
|
||||
grid[x][y] = 2;
|
||||
for (int[] d : dir) {
|
||||
int nextX = x + d[0];
|
||||
int nextY = y + d[1];
|
||||
// 超过边界
|
||||
if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
|
||||
// 不符合条件,不继续遍历
|
||||
if (grid[nextX][nextY] == 0 || grid[nextX][nextY] == 2) continue;
|
||||
dfs(grid, nextX, nextY);
|
||||
}
|
||||
}
|
||||
|
||||
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) dfs(grid, i, 0);
|
||||
if (grid[i][m - 1] == 1) dfs(grid, i, m - 1);
|
||||
}
|
||||
|
||||
// 从上边和下边 向中间遍历
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (grid[0][j] == 1) dfs(grid, 0, j);
|
||||
if (grid[n - 1][j] == 1) dfs(grid, n - 1, j);
|
||||
}
|
||||
|
||||
// 步骤二、步骤三
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (grid[i][j] == 1) grid[i][j] = 0;
|
||||
if (grid[i][j] == 2) grid[i][j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
System.out.print(grid[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
|
||||
def dfs(grid, x, y):
|
||||
grid[x][y] = 2
|
||||
directions = [(-1, 0), (0, -1), (1, 0), (0, 1)] # 四个方向
|
||||
for dx, dy in directions:
|
||||
nextx, nexty = x + dx, y + dy
|
||||
# 超过边界
|
||||
if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]):
|
||||
continue
|
||||
# 不符合条件,不继续遍历
|
||||
if grid[nextx][nexty] == 0 or grid[nextx][nexty] == 2:
|
||||
continue
|
||||
dfs(grid, nextx, nexty)
|
||||
|
||||
def main():
|
||||
n, m = map(int, input().split())
|
||||
grid = [[int(x) for x in input().split()] for _ in range(n)]
|
||||
|
||||
# 步骤一:
|
||||
# 从左侧边,和右侧边 向中间遍历
|
||||
for i in range(n):
|
||||
if grid[i][0] == 1:
|
||||
dfs(grid, i, 0)
|
||||
if grid[i][m - 1] == 1:
|
||||
dfs(grid, i, m - 1)
|
||||
|
||||
# 从上边和下边 向中间遍历
|
||||
for j in range(m):
|
||||
if grid[0][j] == 1:
|
||||
dfs(grid, 0, j)
|
||||
if grid[n - 1][j] == 1:
|
||||
dfs(grid, n - 1, j)
|
||||
|
||||
# 步骤二、步骤三
|
||||
for i in range(n):
|
||||
for j in range(m):
|
||||
if grid[i][j] == 1:
|
||||
grid[i][j] = 0
|
||||
if grid[i][j] == 2:
|
||||
grid[i][j] = 1
|
||||
|
||||
# 打印结果
|
||||
for row in grid:
|
||||
print(' '.join(map(str, row)))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Go
|
||||
|
||||
### Rust
|
||||
|
Reference in New Issue
Block a user