【代码随想录算法训练营——Day51】图论——99.计数孤岛、100.最大岛屿的面积

卡码网题目链接

https://kamacoder.com/problempage.php?pid=1171

https://kamacoder.com/problempage.php?pid=1172

题解
99.计数孤岛

只要遇到一个1,就使用深搜或广搜,把所有相邻为1的位置都标记为已访问。深搜,怎么样把上下左右四个点都遍历递归?用一个循环来写,把递归放到循环里,遍历4次。把result的控制放在dfs外面,不放在dfs里面。我写成了在main中直接调用dfs,其实不对。

100.最大岛屿的面积

这一题还用深搜可以解决,每次到一个岛屿时递归计算遇到1时总加上面积数目,等这一轮遍历完后比较最大结果的大小。


bfs的写法要记住。

代码

python 复制代码
#99.计数孤岛
#dfs法
point = [1, 0, 0, 1, -1, 0, 0, -1]
result  = 0
def dfs(i, j, visited, graph):
    for x, y in zip(point[::2], point[1::2]):
        nextx = i + x
        nexty = j + y
        if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):
            continue
        if graph[nextx][nexty] == 1 and visited[nextx][nexty] == False:
            visited[nextx][nexty] = True
            dfs(nextx, nexty, visited, graph)

if __name__ == "__main__":
    n, m = map(int, input().split())
    graph = []
    for i in range(n):
        row = list(map(int, input().split()))
        graph.append(row)
    visited = [[False] * m for _ in range(n)]
    for i in range(n):
        for j in range(m):
            if graph[i][j] == 1 and visited[i][j] == False:
                result += 1
                visited[i][j] = True
                dfs(i, j, visited, graph)
    print(result)
python 复制代码
#100.最大岛屿的面积
#dfs法
point = [[1, 0], [-1, 0], [0, 1], [0, -1]]
count = 0
def dfs(i, j, visited, graph):
    global count
    for x in range(len(point)):
        nextx = i + point[x][0]
        nexty = j + point[x][1]
        if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):
            continue
        if graph[nextx][nexty] == 1 and visited[nextx][nexty] == False:
            count += 1
            visited[nextx][nexty] = True
            dfs(nextx, nexty, visited, graph)
if __name__ == "__main__":
    n, m = map(int, input().split())
    graph = []
    for i in range(n):
        row = list(map(int, input().split()))
        graph.append(row)
    visited = [[False] * m for _ in range(n)]
    ans = 0
    for i in range(n):
        for j in range(m):
            if graph[i][j] == 1 and visited[i][j] == False:
                count = 1
                visited[i][j] = True
                dfs(i, j, visited, graph)
                ans = max(ans, count)
    print(ans)

#bfs法
from collections import deque
point = [[1, 0], [-1, 0], [0, 1], [0, -1]]
count = 0
def bfs(i, j, visited, graph):
    global count
    queue = deque()
    queue.append(i)
    queue.append(j)

    while queue:
        x = queue.popleft()
        y = queue.popleft()
        for t in range(len(point)):
            nextx = x + point[t][0]
            nexty = y + point[t][1]
            if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):
                continue
            if graph[nextx][nexty] == 1 and visited[nextx][nexty] == False:
                count += 1
                visited[nextx][nexty] = True
                queue.append(nextx)
                queue.append(nexty)
            
if __name__ == "__main__":
    n, m = map(int, input().split())
    graph = []
    for i in range(n):
        row = list(map(int, input().split()))
        graph.append(row)
    visited = [[False] * m for _ in range(n)]
    ans = 0
    for i in range(n):
        for j in range(m):
            if graph[i][j] == 1 and visited[i][j] == False:
                count = 1
                visited[i][j] = True
                bfs(i, j, visited, graph)
                ans = max(ans, count)
    print(ans)
相关推荐
hetao173383713 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
鲨莎分不晴14 小时前
强化学习第五课 —— A2C & A3C:并行化是如何杀死经验回放
网络·算法·机器学习
搞科研的小刘选手15 小时前
【ISSN/ISBN双刊号】第三届电力电子与人工智能国际学术会议(PEAI 2026)
图像处理·人工智能·算法·电力电子·学术会议
拉姆哥的小屋15 小时前
从混沌到秩序:条件扩散模型在图像转换中的哲学与技术革命
人工智能·算法·机器学习
Sammyyyyy15 小时前
DeepSeek v3.2 正式发布,对标 GPT-5
开发语言·人工智能·gpt·算法·servbay
sin_hielo15 小时前
leetcode 2110
数据结构·算法·leetcode
Jay200211116 小时前
【机器学习】33 强化学习 - 连续状态空间(DQN算法)
人工智能·算法·机器学习
panzer_maus16 小时前
归并排序的简单介绍
java·数据结构·算法
cici1587416 小时前
二值化断裂裂缝的智能拼接算法
人工智能·算法·计算机视觉
麦格芬23017 小时前
LeetCode 763 划分字母区间
算法·leetcode·职场和发展