代码随想录算法营Day57 | 孤岛的总面积,淹没孤岛,水流题目,制作最大岛屿

[复制链接]
发表于 2025-10-17 08:35:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
孤岛的总面积

这道题先将靠近边界限上的岛屿都放置为0,然后再用深度大概广度搜刮算法去盘算剩余的孤岛总面积
  1. count = 0
  2. position = [[1, 0], [0, 1], [-1, 0], [0, -1]]
  3. def dfs(matrix,x,y):
  4.     global count
  5.     matrix[x][y] = 0
  6.     count += 1
  7.     for i,j in position:
  8.         next_x = x + i
  9.         next_y = y + j
  10.         if next_x < 0 or next_y < 0 or next_x >= len(matrix) or next_y >= len(matrix[0]):
  11.             continue
  12.         if matrix[next_x][next_y] == 1:
  13.             dfs(matrix,next_x,next_y)
  14.    
  15. N,M = map(int,input().split())
  16. matrix = []
  17. for _ in range(N):
  18.     matrix.append(list(map(int,input().split())))
  19. for i in range(N):
  20.     if matrix[i][0] == 1:
  21.         dfs(matrix,i,0)
  22.     if matrix[i][M-1] == 1:
  23.         dfs(matrix,i,M-1)
  24.    
  25. for j in range(M):
  26.     if matrix[0][j] == 1:
  27.         dfs(matrix,0,j)
  28.     if matrix[N-1][j] == 1:
  29.         dfs(matrix,N-1,j)
  30. count=0
  31. for i in range(N):
  32.     for j in range(M):
  33.         if matrix[i][j] == 1:
  34.             dfs(matrix,i,j)
  35. print(count)
复制代码
淹没孤岛

这道题思绪和上面一样,不外这里必要将边界上的岛屿酿成2。那么此时矩阵当中就只有靠近边界上的2以及孤岛1。这时间只必要将全部数值为2大概为1的都减去1即可。
  1. position = [[1, 0], [0, 1], [-1, 0], [0, -1]]
  2. def dfs(matrix,x,y):
  3.     matrix[x][y] = 2
  4.     for i,j in position:
  5.         next_x = x + i
  6.         next_y = y + j
  7.         if next_x < 0 or next_y < 0 or next_x >= len(matrix) or next_y >= len(matrix[0]):
  8.             continue
  9.         if matrix[next_x][next_y] == 1:
  10.             dfs(matrix,next_x,next_y)
  11.    
  12. N,M = map(int,input().split())
  13. matrix = []
  14. for _ in range(N):
  15.     matrix.append(list(map(int,input().split())))
  16. for i in range(N):
  17.     if matrix[i][0] == 1:
  18.         dfs(matrix,i,0)
  19.     if matrix[i][M-1] == 1:
  20.         dfs(matrix,i,M-1)
  21.    
  22. for j in range(M):
  23.     if matrix[0][j] == 1:
  24.         dfs(matrix,0,j)
  25.     if matrix[N-1][j] == 1:
  26.         dfs(matrix,N-1,j)
  27. count=0
  28. for i in range(N):
  29.     for j in range(M):
  30.         if matrix[i][j] == 1 or matrix[i][j] == 2:
  31.             matrix[i][j] -= 1
  32.             
  33. for l in matrix:
  34.     print(" ".join(map(str,l)))
复制代码
水流题目

现有一个 N × M 的矩阵,每个单元格包罗一个数值,这个数值代表该位置的相对高度。矩阵的左边界和上边界被以为是第一组边界,而矩阵的右边界和下边界被视为第二组边界。
矩阵模拟了一个地形,当雨水落在上面时,水会根据地形的倾斜向低处活动,但只能从较高或等高的所在流向较低或等高而且相邻(上下左右方向)的所在。我们的目标是确定那些单元格,从这些单元格出发的水可以到达第一组边界和第二组边界。

这道题我们从各边界逆着往内去找可达的路径,然后取这两个可达路径的交集即可。
  1. N,M = map(int,input().split())
  2. matrix = []
  3. for _ in range(N):
  4.     matrix.append(list(map(int,input().split())))
  5. first = set()
  6. second = set()
  7. directions = [[-1, 0], [0, 1], [1, 0], [0, -1]]
  8. def dfs(i,j,grid,visited,side):
  9.     if visited[i][j]:
  10.         return
  11.     visited[i][j] = True
  12.     side.add((i,j))
  13.     for x,y in directions:
  14.         new_x = x + i
  15.         new_y = y + j
  16.         if 0<= new_x < N and 0<= new_y < M and grid[i][j] <= grid[new_x][new_y]:
  17.             dfs(new_x,new_y,grid,visited,side)
  18. visited = [[False] * M for _ in range(N)]
  19. for i in range(N):
  20.     dfs(i,0,matrix,visited,first)
  21. for j in range(M):
  22.     dfs(0,j,matrix,visited,first)
  23. visited = [[False] * M for _ in range(N)]
  24. for i in range(N):
  25.     dfs(i,M-1,matrix,visited,second)
  26. for j in range(M):
  27.     dfs(N-1,j,matrix,visited,second)
  28. res = first & second
  29. for x,y in res:
  30.     print(f"{x} {y}")
复制代码
制作最大岛屿

这道题先将各个岛用差别的数字标示出来,并盘算每个岛的面积,而后遍历每个海水记载可以由该海水块毗连起来的陆地的最大面积。
  1. N,M = map(int,input().split())
  2. matrix = []
  3. for _ in range(N):
  4.     matrix.append(list(map(int,input().split())))
  5. directions = [[-1, 0], [0, 1], [0, -1], [1, 0]]
  6. area = 0
  7. def dfs(i,j,grid,visited,num):
  8.     global area
  9.     if visited[i][j]:
  10.         return
  11.    
  12.     visited[i][j] = True
  13.     grid[i][j] = num
  14.     area += 1
  15.     for x, y in directions:
  16.         new_x = x + i
  17.         new_y = y + j
  18.         if 0 <= new_x < N and 0 <= new_y < M and grid[new_x][new_y] == 1:
  19.             dfs(new_x,new_y,grid,visited,num)
  20. cnt = 2
  21. visited = [[False] * M for _ in range(N)]
  22. rec = {}
  23. for i in range(N):
  24.     for j in range(M):
  25.         if matrix[i][j] == 1:
  26.             area = 0
  27.             dfs(i,j,matrix,visited,cnt)
  28.             rec[cnt] = area
  29.             cnt += 1
  30. res = 0
  31. for i in range(N):
  32.     for j in range(M):
  33.         if matrix[i][j] == 0:
  34.             max_island = 1
  35.             v = set()
  36.             for x, y in directions:
  37.                 new_x = x + i
  38.                 new_y = y + j
  39.                 if 0 <= new_x < N and 0 <= new_y < M and matrix[new_x][new_y] != 0 and matrix[new_x][new_y] not in v:
  40.                     max_island += rec[matrix[new_x][new_y]]
  41.                     v.add(matrix[new_x][new_y])
  42.             res = max(res,max_island)
  43. if res == 0:
  44.     print(max(rec.values()))
  45. else:
  46.     print(res)
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表