Day50-图论

99. 岛屿数量

99. 计数孤岛

深搜
python 复制代码
DIR = [[0,1],[1,0],[0,-1],[-1,0]]  

def dfs1(graph,visited,x,y):
    for i in range(4):
        nextx = x+DIR[i][0]
        nexty = y+DIR[i][1]
        if nextx<0 or nexty<0 or nextx>=len(graph) or nexty>=len(graph[0]):
            continue
        if not visited[nextx][nexty] and graph[nextx][nexty]:
            visited[nextx][nexty] = 1
            dfs1(graph,visited,nextx,nexty)


def main():
    n,m = map(int,input().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int,input().split())))
    visited = [[0]*m for _ in range(n)]
    res = 0
    for i in range(n):
        for j in range(m):
            if graph[i][j] and not visited[i][j]:
                # 发现新大陆
                res +=1
                visited[i][j] = 1
                # 查看周围的领土
                dfs1(graph,visited,i,j)
    print(res)

if __name__ == '__main__':
    main()
python 复制代码
DIR = [[0,1],[1,0],[0,-1],[-1,0]]  

def dfs2(graph,visited,x,y):
    if visited[x][y] or not graph[x][y]:  
        # 海洋或者已经处理过的陆地,不重复访问
        return
    visited[x][y] = 1
    for i in range(4):
        nextx = x+DIR[i][0]
        nexty = y+DIR[i][1]
        if nextx<0 or nexty<0 or nextx>=len(graph) or nexty>=len(graph[0]):
            continue
        dfs2(graph,visited,nextx,nexty)


def main():
    n,m = map(int,input().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int,input().split())))
    visited = [[0]*m for _ in range(n)]
    res = 0
    for i in range(n):
        for j in range(m):
            if graph[i][j] and not visited[i][j]:
                # 发现新大陆
                res +=1
                # 查看周围的领土
                dfs2(graph,visited,i,j)
    print(res)

if __name__ == '__main__':
    main()
广搜
python 复制代码
DIR = [[0,1],[1,0],[0,-1],[-1,0]]  

def bfs(graph,visited,x,y):
    # 坑:入队列就要变成访问过;如果取出的时候才标记访问过,会有大量重复节点被标记
    queue = []
    queue.append([x,y])
    visited[x][y] = 1
    while queue:
        curx,cury = queue.pop(0)
        for i in range(4):
            nextx = curx+DIR[i][0]
            nexty = cury+DIR[i][1]
            if nextx<0 or nexty<0 or nextx>=len(graph) or nexty>=len(graph[0]):
                continue
            if not visited[nextx][nexty] and graph[nextx][nexty]:
                queue.append([nextx,nexty])
                visited[nextx][nexty] = 1

def main():
    n,m = map(int,input().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int,input().split())))
    visited = [[0]*m for _ in range(n)]
    res = 0
    for i in range(n):
        for j in range(m):
            if graph[i][j] and not visited[i][j]:
                # 发现新大陆
                res +=1
                # 查看周围的领土
                bfs(graph,visited,i,j)
    print(res)

if __name__ == '__main__':
    main()

100. 岛屿的最大面积

100. 最大岛屿的面积

深搜
python 复制代码
DIR = [[0,1],[1,0],[0,-1],[-1,0]] 
count = 0  # 全局,因为dfs可能拿不到返回值【第一种写法】

def dfs1(graph,visited,x,y):
    global count
    for i in range(4):
        nextx = x+DIR[i][0]
        nexty = y+DIR[i][1]
        if nextx<0 or nexty<0 or nextx>=len(graph) or nexty>=len(graph[0]):
            continue
        if not visited[nextx][nexty] and graph[nextx][nexty]:
            visited[nextx][nexty] = 1
            count +=1
            dfs1(graph,visited,nextx,nexty)


def main():
    n,m = map(int,input().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int,input().split())))
    visited = [[0]*m for _ in range(n)]
    res = 0
    global count
    for i in range(n):
        for j in range(m):
            if graph[i][j] and not visited[i][j]:
                # 发现新大陆,陆地面积置为1
                count = 1
                visited[i][j] = 1
                # 查看周围的领土
                dfs1(graph,visited,i,j)
                # 全局的count在遍历中得到了更新
                res = max(res,count)
    print(res)

if __name__ == '__main__':
    main()
python 复制代码
DIR = [[0,1],[1,0],[0,-1],[-1,0]] 
count = 0  # 全局,因为dfs可能拿不到返回值【第一种写法】

def dfs2(graph,visited,x,y):
    global count
    if not graph[x][y] or visited[x][y]:
        return
    visited[x][y] = 1
    count +=1
    for i in range(4):
        nextx = x+DIR[i][0]
        nexty = y+DIR[i][1]
        if nextx<0 or nexty<0 or nextx>=len(graph) or nexty>=len(graph[0]):
            continue
        dfs2(graph,visited,nextx,nexty)

def main():
    n,m = map(int,input().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int,input().split())))
    visited = [[0]*m for _ in range(n)]
    res = 0
    global count
    for i in range(n):
        for j in range(m):
            if graph[i][j] and not visited[i][j]:
                # 发现新大陆,陆地面积置为1
                count = 0
                # 查看周围的领土
                dfs2(graph,visited,i,j)
                # 全局的count在遍历中得到了更新
                res = max(res,count)
    print(res)

if __name__ == '__main__':
    main()
广搜
python 复制代码
DIR = [[0,1],[1,0],[0,-1],[-1,0]] 
count = 0  # 全局,因为dfs可能拿不到返回值【第一种写法】


def bfs(graph,visited,x,y):
    queue = []
    queue.append([x,y])
    visited[x][y] = 1
    global count
    count = 1
    while queue:
        curx,cury = queue.pop(0)
        for i in range(4):
            nextx = curx+DIR[i][0]
            nexty = cury+DIR[i][1]
            if nextx<0 or nexty<0 or nextx>=len(graph) or nexty>=len(graph[0]):
                continue
            if not visited[nextx][nexty] and graph[nextx][nexty]:
                queue.append([nextx,nexty])
                visited[nextx][nexty] = 1
                count +=1

def main():
    n,m = map(int,input().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int,input().split())))
    visited = [[0]*m for _ in range(n)]
    res = 0
    global count
    for i in range(n):
        for j in range(m):
            if graph[i][j] and not visited[i][j]:
                # 发现新大陆,陆地面积置为1
                visited[i][j] = 1
                # 查看周围的领土
                bfs(graph,visited,i,j)
                # 全局的count在遍历中得到了更新
                res = max(res,count)
    print(res)

if __name__ == '__main__':
    main()
相关推荐
小狗丹尼40040 分钟前
JSON 基础认知、数据转换与 Flask 前后端交互全解
python·flask·json
1104.北光c°1 小时前
滑动窗口HotKey探测机制:让你的缓存TTL更智能
java·开发语言·笔记·程序人生·算法·滑动窗口·hotkey
zm-v-159304339863 小时前
Python 数据挖掘从入门到精通:回归 / 分类 / 聚类 / 关联分析完整教程
python·数据挖掘·回归
仰泳的熊猫5 小时前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
qq_417695058 小时前
机器学习与人工智能
jvm·数据库·python
无极低码8 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
漫随流水8 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
软件算法开发8 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
罗超驿9 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist