当前位置: 首页 > article >正文

211、【图论】建造最大岛屿(Python)

题目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

思路

  1. 将每个岛屿分编号,并且对每个编号的岛屿记录面积
  2. 尝试在0处添加岛屿,并且判定周围是否有可连接的岛屿计算面积。

代码实现

import collections

directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(i, j, num, area):
    graph[i][j] = num
    visited[i][j] = True
    deque = collections.deque()
    deque.append([i, j])
    area += 1

    while deque:
        x, y = deque.popleft()
        for direction in directions:
            next_x, next_y = direction[0], direction[1]
            move_x, move_y = x + next_x, y + next_y
            if move_x < 0 or move_x >= n or move_y < 0 or move_y >= m:
                continue
            if graph[move_x][move_y] == 1 and not visited[move_x][move_y]:
                graph[move_x][move_y] = num
                deque.append([move_x, move_y])
                area += 1

    return area

n, m = list(map(int, input().split()))
graph = []
visited = [[False] * m for _ in range(n)]
for i in range(n):
    graph.append(list(map(int, input().split())))
record_area = collections.defaultdict(int)		# 记录每个编号岛屿的面积

num = 2					# 编号
is_all_grid = True		# 判定是否图中全为岛屿
# 给每个岛屿起编号,并且统计每个编号的面积
for i in range(n):
    for j in range(m):
        if graph[i][j] == 0:
            is_all_grid = False
        if graph[i][j] == 1:
            area = bfs(i, j, num, 0)
            record_area[num] = area            
            num += 1
# 如果全部为岛屿,则直接输出面积
if is_all_grid:
    print(n * m)
# 如果有海,则计算添加岛屿后的连接情况
else:
    res = 0						# 统计面积
    visited_graph = set()		# 让连接的岛屿不被重复统计
    for i in range(n):
        for j in range(m):
            count = 1				# 第一块0被变做岛屿
            visited_graph.clear()	# 每次清空
            if graph[i][j] == 0:            
                for direction in directions:
                    next_x, next_y = i + direction[0], j + direction[1]
                    # 保证不越界、四周探索到的为岛屿并且没有被统计过
                    if (
                        0 <= next_x < n and
                        0 <= next_y < m and 
                        graph[next_x][next_y] != 0 and 
                        graph[next_x][next_y] not in visited_graph                    
                    ):
                    	# 获取岛屿编号,连接岛屿计算面积,访问记录
                        num = graph[next_x][next_y]
                        count += record_area[num]                          
                        visited_graph.add(num)        
            # 每行后统计新情况
            res = max(res, count)

    print(res)


参考文章:建造最大岛屿


http://www.kler.cn/a/612161.html

相关文章:

  • 计算机网络——传输层(TCP)
  • 广东新政激发产业活力,凡拓数创以全场景AI3D方案领跑机器人赛道
  • Go File容器化部署方案:本地快速搭建与无公网IP远程传输文件指南
  • css的animation属性展示
  • 双周报Vol.68: Bytes模式匹配增强、函数别名上线、IDE体验优化...核心技术迎来多项更新升级!
  • 蓝桥杯python编程每日刷题 day 20
  • 关于我对接了deepseek之后部署到本地将数据存储到mysql的过程
  • Selenium基本使用(三)隐藏框、获取文本、断言、切换窗口
  • 【数据可视化艺术·进阶篇】热力图探秘:用色彩演绎场馆和景区的人流奥秘
  • Xcode 打开报错 / 解決 Could not open workspace file at xxx 问题
  • vue2自定义指令实现滚动动画-使用IntersectionObserver观察器
  • 从零构建大语言模型全栈开发指南:第三部分:训练与优化技术-3.1.3分布式数据加载与并行处理(PyTorch DataLoader优化)
  • nestjs 连接redis
  • 数据可视化(matplotlib)-------图表样式美化
  • 蓝桥杯第十届 数的分解
  • Linux——进程信号(1)(signal与sigaction)
  • java程序员实用英语学习总结
  • linux scp复制多层级文件夹到另一服务器免密及脚本配置
  • 【深度学习与实战】2.1、线性回归模型与梯度下降法先导
  • [250324] Kafka 4.0.0 版本发布:告别 ZooKeeper,拥抱 KRaft!| Wine 10.4 发布!