题目链接:99. 岛屿数量 (kamacoder.com)
文章讲解:代码随想录 (programmercarl.com)——99. 岛屿数量 深搜
思路:每遇到一个陆地(1),从当前位置开始深搜,如果访问过则标记为“True”,即表示访问过,每进行一次深搜,岛屿数量+1。
# 定义四个方向,上右下左
dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]
# 1. 确定递归函数和参数
# grid: 地图表格
# visited: 记录当前位置是否被访问过,布尔类型
# x: 当前节点横坐标
# y: 当前节点纵坐标
def dfs(grid, visited, x, y):
# 2. 确定终止条件
if visited[x][y] or grid[x][y] == 0:
return
visited[x][y] = True
# 3. 处理目前搜索节点出发的路径
for i, j in dir:
next_x = x + i
next_y = y + j
# 如果下标越界,则跳过
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
continue
# 对下一个位置递归调用dfs
dfs(grid, visited, next_x, next_y)
if __name__ == '__main__':
n, m = map(int, input().split())
# 构建邻接矩阵
grid = []
for i in range(n):
grid.append(list(map(int, input().split())))
# 创建是否访问的表格,全部初始化为False
visited = [[False] * m for _ in range(n)]
result = 0
for i in range(n):
for j in range(m):
# 判断,如果当前节点是陆地且没被访问,结果+1,并标记为访问过该节点,dfs标记相邻陆地
if grid[i][j] == 1 and visited[i][j] == False:
result += 1
dfs(grid, visited, i, j)
print(result)
题目链接:99. 岛屿数量 (kamacoder.com)
文章讲解:代码随想录 (programmercarl.com)——99. 岛屿数量 广搜
from collections import deque
dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]
def bfs(grid, visited, x, y):
# 创建一个队列,并将初始 x, y 加入队列中
que = deque()
que.append([x, y])
# 当队列不为空时
while que:
# 在队列左侧弹出元素,得到当前坐标
cur_x, cur_y = que.popleft()
for i, j in dir:
next_x = cur_x + i
next_y = cur_y + j
# 如果不越界
if 0 <= next_x < len(grid) and 0 <= next_y < len(grid[0]):
# 如果没有访问过,且为陆地
if not visited[next_x][next_y] and grid[next_x][next_y] == 1:
visited[next_x][next_y] = True
# 未被访问过的节点进队
que.append([next_x, next_y])
if __name__ == '__main__':
n, m = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(n)]
visited = [[False] * m for _ in range(n)]
result = 0
for i in range(n):
for j in range(m):
if not visited[i][j] and grid[i][j] == 1:
visited[i][j] = True
result += 1
bfs(grid, visited, i, j)
print(result)
题目链接:100. 岛屿的最大面积 (kamacoder.com)
文章讲解:代码随想录 (programmercarl.com)——100. 岛屿的最大面积
"""
DFS
"""
# 定义四个方向,上右下左
dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]
count = 0
def dfs(grid, visited, x, y):
# 定义全局变量
global count
for i, j in dir:
next_x = x + i
next_y = y + j
# 如果下标越界,则跳过
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
continue
# 如果没有访问过,同时为陆地
if not visited[next_x][next_y] and grid[next_x][next_y] == 1:
# 标记为访问过
visited[next_x][next_y] = True
# 面积+1
count += 1
# 递归调用dfs
dfs(grid, visited, next_x, next_y)
if __name__ == '__main__':
n, m = map(int, input().split())
# 创建邻接矩阵
grid = [list(map(int, input().split())) for _ in range(n)]
# 创建访问列表
visited = [[False] * m for _ in range(n)]
result = 0
for i in range(n):
for j in range(m):
# 如果没访问过,且为陆地
if not visited[i][j] and grid[i][j] == 1:
# 面积+1
count += 1
# 标记为访问过
visited[i][j] = True
# 递归调用dfs
dfs(grid, visited, i, j)
# 记录最大面积
result = max(result, count)
print(result)
"""
BFS
"""
from collections import deque
dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]
count = 0
def bfs(grid, visited, x, y):
global count
# 创建一个队列
que = deque()
# 将初始 x, y 坐标加入队列
que.append([x, y])
# 当队列不为空时
while que:
# 在队列左侧弹出元素,得到当前坐标
cur_x, cur_y = que.popleft()
# 遍历所有方向
for i, j in dir:
next_x = cur_x + i
next_y = cur_y + j
# 如果不越界
if 0 <= next_x < len(grid) and 0 <= next_y < len(grid[0]):
# 没有访问过,同时是陆地
if not visited[next_x][next_y] and grid[next_x][next_y] == 1:
# 标记当前坐标已访问
visited[next_x][next_y] = True
count += 1
# 未访问过的节点进入队列
que.append([next_x, next_y])
if __name__ == '__main__':
n, m = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(n)]
visited = [[False] * m for _ in range(n)]
result = 0
for i in range(n):
for j in range(m):
if not visited[i][j] and grid[i][j] == 1:
visited[i][j] = True
count = 1
bfs(grid, visited, i, j)
result = max(result, count)
print(result)