mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
@ -192,6 +192,56 @@ int main() {
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
from collections import deque
|
||||
|
||||
# 四个方向
|
||||
position = [[0, 1], [1, 0], [0, -1], [-1, 0]]
|
||||
|
||||
|
||||
def bfs(grid, visited, x, y):
|
||||
"""
|
||||
广度优先搜索对陆地进行标记
|
||||
"""
|
||||
|
||||
que = deque() # 创建队列
|
||||
|
||||
# 标记当前节点并加入队列
|
||||
visited[x][y] = True
|
||||
que.append([x, y])
|
||||
|
||||
while que:
|
||||
cur_x, cur_y = que.popleft() # 取出队首节点
|
||||
for i, j in position:
|
||||
next_x = cur_x + i
|
||||
next_y = cur_y + j
|
||||
# 下一节点下标越界,跳过
|
||||
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
|
||||
continue
|
||||
# 下一节点是陆地且未被访问,标记节点并加入队列
|
||||
if grid[next_x][next_y] == 1 and not visited[next_x][next_y]:
|
||||
visited[next_x][next_y] = True
|
||||
que.append([next_x, next_y])
|
||||
|
||||
|
||||
n, m = map(int, input().split())
|
||||
# 邻接矩阵
|
||||
grid = []
|
||||
for i in range(n):
|
||||
grid.append(list(map(int, input().split())))
|
||||
|
||||
visited = [[False] * m for _ in range(n)] # 访问表
|
||||
|
||||
res = 0
|
||||
for i in range(n):
|
||||
for j in range(m):
|
||||
if grid[i][j] == 1 and not visited[i][j]:
|
||||
res += 1
|
||||
bfs(grid, visited, i, j)
|
||||
|
||||
print(res)
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
### Rust
|
||||
|
@ -226,6 +226,100 @@ public:
|
||||
|
||||
### Python
|
||||
|
||||
DFS
|
||||
|
||||
```python
|
||||
# 四个方向
|
||||
position = [[0, 1], [1, 0], [0, -1], [-1, 0]]
|
||||
count = 0
|
||||
|
||||
|
||||
def dfs(grid, visited, x, y):
|
||||
"""
|
||||
深度优先搜索,对一整块陆地进行标记
|
||||
"""
|
||||
global count # 定义全局变量,便于传递count值
|
||||
for i, j in position:
|
||||
cur_x = x + i
|
||||
cur_y = y + j
|
||||
# 下标越界,跳过
|
||||
if cur_x < 0 or cur_x >= len(grid) or cur_y < 0 or cur_y >= len(grid[0]):
|
||||
continue
|
||||
if not visited[cur_x][cur_y] and grid[cur_x][cur_y] == 1:
|
||||
visited[cur_x][cur_y] = True
|
||||
count += 1
|
||||
dfs(grid, visited, cur_x, cur_y)
|
||||
|
||||
|
||||
n, m = map(int, input().split())
|
||||
# 邻接矩阵
|
||||
grid = []
|
||||
for i in range(n):
|
||||
grid.append(list(map(int, input().split())))
|
||||
# 访问表
|
||||
visited = [[False] * m for _ in range(n)]
|
||||
|
||||
result = 0 # 记录最终结果
|
||||
for i in range(n):
|
||||
for j in range(m):
|
||||
if grid[i][j] == 1 and not visited[i][j]:
|
||||
count = 1
|
||||
visited[i][j] = True
|
||||
dfs(grid, visited, i, j)
|
||||
result = max(count, result)
|
||||
|
||||
print(result)
|
||||
```
|
||||
|
||||
BFS
|
||||
|
||||
```python
|
||||
from collections import deque
|
||||
|
||||
position = [[0, 1], [1, 0], [0, -1], [-1, 0]] # 四个方向
|
||||
count = 0
|
||||
|
||||
|
||||
def bfs(grid, visited, x, y):
|
||||
"""
|
||||
广度优先搜索对陆地进行标记
|
||||
"""
|
||||
global count # 声明全局变量
|
||||
que = deque()
|
||||
que.append([x, y])
|
||||
while que:
|
||||
cur_x, cur_y = que.popleft()
|
||||
for i, j in position:
|
||||
next_x = cur_x + i
|
||||
next_y = cur_y + j
|
||||
# 下标越界,跳过
|
||||
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
|
||||
continue
|
||||
if grid[next_x][next_y] == 1 and not visited[next_x][next_y]:
|
||||
visited[next_x][next_y] = True
|
||||
count += 1
|
||||
que.append([next_x, next_y])
|
||||
|
||||
|
||||
n, m = map(int, input().split())
|
||||
# 邻接矩阵
|
||||
grid = []
|
||||
for i in range(n):
|
||||
grid.append(list(map(int, input().split())))
|
||||
visited = [[False] * m for _ in range(n)] # 访问表
|
||||
|
||||
result = 0 # 记录最终结果
|
||||
for i in range(n):
|
||||
for j in range(m):
|
||||
if grid[i][j] == 1 and not visited[i][j]:
|
||||
count = 1
|
||||
visited[i][j] = True
|
||||
bfs(grid, visited, i, j)
|
||||
res = max(result, count)
|
||||
|
||||
print(result)
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
### Rust
|
||||
|
Reference in New Issue
Block a user