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

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)


参考文章:建造最大岛屿

相关文章:

  • 个人学习编程(3-24) 数据结构
  • vite中sass警告JS API过期
  • 智能语言交互,AI 对话引领生活变革
  • HarmonyOS Next~鸿蒙AI功能开发:Core Speech Kit与Core Vision Kit的技术解析与实践
  • el-select下拉框,搜索时,若是匹配后的数据有且只有一条,则当失去焦点时,默认选中该条数据
  • Vue2与Vue3中TS子组件传值给父组件
  • Chair Assembly Process
  • 服务器入门笔记
  • 2.5 微分
  • Apache Dubbo Pixiu打造微服务生态的轻量级 API 网关
  • 【测试工具】如何使用 burp pro 自定义一个拦截器插件
  • 3.23-libevent
  • Android Studio常见问题解决
  • 2024年认证杯SPSSPRO杯数学建模C题(第二阶段)云中的海盐全过程文档及程序
  • HTML云原生:概念、技术与应用的全面解析
  • STM32学习笔记之keil使用记录
  • 模式搜索+扩散模型:FlowMo重构图像Token化的技术革命
  • 2025年河北省第二届职业技能大赛网络安全项目 模块 B样题任务书
  • 运动仿真——phased.Platform
  • StarRocks vs Doris:深度剖析与选型分析
  • 比黄油年糕热量还高,这个火爆全网的甜品劝你慎吃
  • 百年传承,再启新程,参天中国迎来2.0时代
  • 从孔雀尾巴到蒙娜丽莎,一个鸟类学博士眼中的“美”
  • 普京与卢卡申科举行会晤,将扩大在飞机制造等领域合作
  • 西夏文残碑等文物来沪,见证一段神秘灿烂的历史
  • 宜昌打造“算力之都”:产业链快速延伸,追逐千亿级产值