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

day51 第十一章:图论part02

99. 岛屿数量 深搜

每一块的上下左右都遍历过了之后,这块陆地就遍历完了。是深搜,不是广搜

深搜:递归

def dfs():

        if .....:

                终止条件

        dfs(子节点)

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

def dfs(grid, visited, x, y):
    if grid[x][y] == 0 or visited[x][y]:
        return
    visited[x][y] = True
    for i in range(4):
        next_x = x + directions[i][0]
        next_y = y + directions[i][1]
        if next_x < 0 or next_x >=n or next_y < 0 or next_y >= m:
            continue
        dfs(grid, visited, next_x, next_y)
    
    
if __name__ == '__main__':
    n, m = map(int, input().split())
    
    grid = []
    for _ in range(n):
        grid.append(list(map(int, input().split())))
        
    visited = [[False]*m for _ in range(n)]
    res = 0
    
    for i in range(n):
        for j in range(m):
            if grid[i][j] == 1 and not visited[i][j]:
                res += 1 
                dfs(grid, visited, i, j)
    
    print(res)
    

99. 岛屿数量 广搜

广搜用队列(deque),

先加进去第一个节点

while que:

        cur = que.popleft()

        for cur_child in cur_children:

                que.append(cur_child)

        

from collections import deque
directions = [[0,1],[1,0],[0,-1],[-1,0]]

def bfs(grid, visited, x, y):
    que = deque([])
    que.append([x,y])
    visited[x][y] = True
    while que:
        cur_x, cur_y = que.popleft()
        for i in range(4):
            next_x = cur_x + directions[i][0]
            next_y = cur_y + directions[i][1]
            if next_x < 0 or next_x >=n or next_y < 0 or next_y >= m:
                continue
            elif grid[next_x][next_y] == 1 and not visited[next_x][next_y]:
                que.append([next_x, next_y])
                visited[next_x][next_y] = True
            
    
if __name__ == '__main__':
    n, m = map(int, input().split())
    
    grid = []
    for _ in range(n):
        grid.append(list(map(int, input().split())))
        
    visited = [[False]*m for _ in range(n)]
    res = 0
    
    for i in range(n):
        for j in range(m):
            if grid[i][j] == 1 and not visited[i][j]:
                res += 1 
                bfs(grid, visited, i, j)
    
    print(res)
    

100. 岛屿的最大面积

dfs

# dfs,bfs都行
# dfs
directions = [[0,1],[1,0],[0,-1],[-1,0]]
max_area = 0
area = 0


def dfs(grid, visited, x, y):
    global area
    global max_area

    if grid[x][y] == 0 or visited[x][y] == True:
        return
    visited[x][y] = True
    area += 1
    max_area = max(max_area, area)
    # print("area=", area)
    # print("max_area=", max_area)
    for i in range(4):
        next_x = x + directions[i][0]
        next_y = y + directions[i][1]
        if next_x < 0 or next_x >= n or next_y < 0 or next_y >= m:
            continue
        dfs(grid, visited, next_x, next_y)
        
if __name__ == "__main__":
    n,m = map(int, input().split())

    grid = []
    for _ in range(n):
        grid.append(list(map(int, input().split())))

    # n,m = 4, 5
    # grid = [[1,1,0,0,0],[1,1,0,0,0],[0,1,1,0,0],[0,0,0,1,1]]

    visited = [[False]*m for _ in range(n)]

    for i in range(n):
        for j in range(m):
            if grid[i][j] == 1 and not visited[i][j]:
                area = 0
                dfs(grid, visited, i, j)
    
    print(max_area)
    

bfs

# dfs,bfs都行
# bfs
from collections import deque
directions = [[0,1],[1,0],[0,-1],[-1,0]]

def bfs(grid, visited, x, y):
    area = 0
    que = deque()
    que.append([x,y])
    while que:
        cur_x, cur_y = que.popleft()
        if grid[cur_x][cur_y] == 1 and not visited[cur_x][cur_y]:
            area += 1
            visited[cur_x][cur_y] = True
            for i in range(4):
                next_x = cur_x + directions[i][0]
                next_y = cur_y + directions[i][1]
                if next_x < 0 or next_x >= n or next_y < 0 or next_y >=m:
                    continue
                # if grid[next_x][next_y] == 1 and not visited[next_x][next_y]:
                que.append([next_x, next_y])
            
    return area


       
if __name__ == "__main__":
    n,m = map(int, input().split())

    grid = []
    for _ in range(n):
        grid.append(list(map(int, input().split())))

    visited = [[False]*m for _ in range(n)]
    max_area = 0

    for i in range(n):
        for j in range(m):
            if grid[i][j] == 1 and not visited[i][j]:
                # area = 0
                # global max_area
                max_area = max(max_area, bfs(grid, visited, i, j))
    
    print(max_area)
    


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

相关文章:

  • M4Pro基于homebrew安装Redis踩坑记录
  • 前端骨架怎样实现
  • Oracle DG运维概要及详细操作手册
  • Docker 入门与实战:从安装到容器管理的完整指南
  • Ubuntu18 将脚本设置成自启动的几种方法
  • ES分词技术
  • kkFileView二开之pdf转图片接口
  • Kafka 为什么会丢消息?如何保证消息不丢失?
  • Java GC 基础知识快速回顾
  • TK矩阵系统:全面提升TikTok运营效率的智能化工具
  • 【Vue3 入门到实战】16. Vue3 非兼容性改变
  • 最新智能优化算法:牛优化( Ox Optimizer,OX)算法求解经典23个函数测试集,MATLAB代码
  • 缓存穿透、缓存击穿、缓存雪崩的区别与解决方案
  • 【transformers.Trainer填坑】在自定义compute_metrics时logits和labels数据维度不一致问题
  • 基于LSTM的情感分析
  • Dockerfile 编写推荐
  • SQL-leetcode—1683. 无效的推文
  • Linux Mem -- AArch64 MTE功能Tag寄存器
  • Redis五种用途
  • 【LeetCode】15.三数之和