本次题目都是卡码网上的
99. 岛屿数量 深搜
dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)]
def dfs(grid, visited, x, y):
for i in range(4):
nextx = x + dirs[i][0]
nexty = y + dirs[i][1]
# 越界了,直接跳过
if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]):
continue
# 没有访问过的,同时是陆地的
if not visited[nextx][nexty] and grid[nextx][nexty] == 1:
visited[nextx][nexty] = True
dfs(grid, visited, nextx, nexty)
if __name__ == "__main__":
n, m = map(int, input().strip().split())
grid = [[i for i in map(int, input().split())] for j 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
dfs(grid, visited, i, j)
print(result)
99. 岛屿数量 广搜
使用的队列,也可以用栈、数组,没有影响
import collections
dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)] # 四个方向
def bfs(grid, visited, x, y):
que = collections.deque()
que.append((x, y))
visited[x][y] = True # 只要加入队列,立刻标记
while que:
curx, cury = que.popleft()
for i in range(4):
nextx = curx + dirs[i][0]
nexty = cury + dirs[i][1]
if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]):
continue
# 没有访问过的,同时是陆地的
if not visited[nextx][nexty] and grid[nextx][nexty] == 1:
visited[nextx][nexty] = True # 只要加入队列,立刻标记
que.append((nextx, nexty))
if __name__ == "__main__":
n, m = map(int, input().strip().split())
grid = [[i for i in map(int, input().strip().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:
result += 1
bfs(grid, visited, i, j)
print(result)
使用栈也行
dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)] # 四个方向
def bfs(grid, visited, x, y):
stack = [(x, y)]
visited[x][y] = True # 只要加入队列,立刻标记
while stack:
curx, cury = stack.pop()
for i in range(4):
nextx = curx + dirs[i][0]
nexty = cury + dirs[i][1]
if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]):
continue
# 没有访问过的,同时是陆地的
if not visited[nextx][nexty] and grid[nextx][nexty] == 1:
visited[nextx][nexty] = True # 只要加入队列,立刻标记
stack.append((nextx, nexty))
if __name__ == "__main__":
n, m = map(int, input().strip().split())
grid = [[i for i in map(int, input().strip().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:
result += 1
bfs(grid, visited, i, j)
print(result)
100. 岛屿的最大面积
关于Python中global的使用 https://www.freecodecamp.org/chinese/news/python-global-variables-examples/
count定义在最外面,是全局变量,但是不能在函数中修改(可以查看),修改前需要先声明global count
广搜代码
dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)] # 四个方向
count = 0
def bfs(grid, visited, x, y):
stack = [(x, y)]
visited[x][y] = True # 只要加入队列,立刻标记
global count
count = 1
while stack:
curx, cury = stack.pop()
for i in range(4):
nextx = curx + dirs[i][0]
nexty = cury + dirs[i][1]
if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]):
continue
# 没有访问过的,同时是陆地的
if not visited[nextx][nexty] and grid[nextx][nexty] == 1:
visited[nextx][nexty] = True # 只要加入队列,立刻标记
stack.append((nextx, nexty))
count += 1
if __name__ == "__main__":
n, m = map(int, input().strip().split())
grid = [[i for i in map(int, input().strip().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:
bfs(grid, visited, i, j)
result = max(result, count)
print(result)
深搜代码
dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)]
count = 0
def dfs(grid, visited, x, y):
for i in range(4):
nextx = x + dirs[i][0]
nexty = y + dirs[i][1]
# 越界了,直接跳过
if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]):
continue
# 没有访问过的,同时是陆地的
if not visited[nextx][nexty] and grid[nextx][nexty] == 1:
visited[nextx][nexty] = True
global count
count += 1
dfs(grid, visited, nextx, nexty)
if __name__ == "__main__":
n, m = map(int, input().strip().split())
grid = [[i for i in map(int, input().split())] for j 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
dfs(grid, visited, i, j)
result = max(result, count)
print(result)