本次题目都是卡码网上的
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] = Truedfs(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 = 0for i in range(n):for j in range(m):if not visited[i][j] and grid[i][j] == 1:visited[i][j] = Trueresult += 1dfs(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 = 0for i in range(n):for j in range(m):if not visited[i][j] and grid[i][j] == 1:result += 1bfs(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 = 0for i in range(n):for j in range(m):if not visited[i][j] and grid[i][j] == 1:result += 1bfs(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 = 0def bfs(grid, visited, x, y):stack = [(x, y)]visited[x][y] = True # 只要加入队列,立刻标记global countcount = 1while 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 += 1if __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 = 0for 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 = 0def 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] = Trueglobal countcount += 1dfs(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 = 0for i in range(n):for j in range(m):if not visited[i][j] and grid[i][j] == 1:visited[i][j] = Truecount = 1dfs(grid, visited, i, j)result = max(result, count)print(result)