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

代码随想录算法训练营DAY58|101.孤岛的总面积、102.沉没孤岛、103. 水流问题、104.建造最大岛屿

忙。。。写了好久。。。。慢慢补吧。

101.孤岛的总面积

  • 先把周边的岛屿变成水
  • dfs
def dfs(x, y, graph, s):if x<0 or x>=len(graph) or y<0 or y>=len(graph[0]) or graph[x][y]==0:return sgraph[x][y]=0s+=1s = dfs(x+1, y, graph, s)s = dfs(x-1, y, graph, s)s = dfs(x, y+1, graph, s)s = dfs(x, y-1, graph, s)return sif __name__=='__main__':n,m = map(int,input().split())graph=[]for i in range(n):row = list(map(int, input().split()))graph.append(row)for i in range(n):if graph[i][0]==1:dfs(i,0,graph,0)if graph[i][m-1]==1:dfs(i,m-1,graph,0)for j in range(m):if graph[0][j]==1:dfs(0,j,graph,0)if graph[n-1][j]==1:dfs(n-1,j,graph,0)s_t = 0for x in range(n):for y in range(m):if graph[x][y]==1:s=dfs(x,y,graph,0)s_t+=sprint(s_t)

102.沉没孤岛

from collections import dequedef bfs(x, y, graph):graph[x][y]=2que = deque([[x,y]])while que:x, y = que.popleft()directions = [[0,1],[0,-1],[1,0],[-1,0]]for k in range(4):x0, y0 = directions[k]new_x = x+x0new_y = y+y0if new_x>=0 and new_x<len(graph) and new_y>=0 and new_y<len(graph[0]) and graph[new_x][new_y]==1:que.append([new_x, new_y])graph[new_x][new_y]=2return     if __name__=='__main__':n,m = map(int,input().split())graph=[]for _ in range(n):row = list(map(int, input().split()))graph.append(row)    for i in range(n):if graph[i][0]==1:bfs(i,0,graph)if graph[i][m-1]==1:bfs(i,m-1,graph)for j in range(m):if graph[0][j]==1:bfs(0,j,graph)if graph[n-1][j]==1:bfs(n-1,j,graph)for x in range(n):for y in range(m):if graph[x][y]==2:graph[x][y]=1elif graph[x][y]==1:graph[x][y]=0for a in range(n):for b in range(m):print(str(graph[a][b])+' ',end = '')print()

103. 水流问题

  • 注意是从边界逆流而上,判断条件应该是graph[x][y]<=graph[new_x][new_y]:
def dfs(x,y,graph,bordermatrix):if bordermatrix[x][y]:returnbordermatrix[x][y]=1directions=[[0,1],[0,-1],[1,0],[-1,0]]for i in range(4):x0,y0=directions[i]new_x = x+x0new_y = y+y0if new_x<0 or new_x>=len(graph) or new_y<0 or new_y>=len(graph[0]):continueif graph[x][y]<=graph[new_x][new_y]:dfs(new_x,new_y,graph,bordermatrix)return if __name__=='__main__':n,m=map(int, input().split())graph=[]for _ in range(n):row = list(map(int, input().split()))graph.append(row)firstborder=[[0]*m for i in range(n)]secondborder=[[0]*m for i in range(n)]for i in range(n):dfs(i,0,graph,firstborder)dfs(i,m-1,graph,secondborder)for j in range(m):dfs(0,j,graph,firstborder)dfs(n-1,j,graph,secondborder)for k in range(n):for l in range(m):if firstborder[k][l] and secondborder[k][l]:print('{} {}'.format(str(k),str(l)))

104.建造最大岛屿

def dfs(x,y,graph,visited,mark,s):directions=[[0,1],[0,-1],[1,0],[-1,0]]if visited[x][y] or not graph[x][y]:return  svisited[x][y]=Truegraph[x][y]=marks+=1for i in range(4):x0,y0=directions[i]new_x = x+x0new_y = y+y0if new_x<0 or new_x>=len(graph) or new_y<0 or new_y>=len(graph[0]):continues = dfs(new_x,new_y,graph,visited,mark,s)return sif __name__=='__main__':n,m = map(int, input().split())graph = []for i in range(n):row = list(map(int, input().split()))graph.append(row)isallgrid=Truevisited=[[False]*m for i in range(n)]islands={}islands[0]=0idx=1for j in range(n):for k in range(m):if graph[j][k]==1:s = dfs(j,k,graph,visited,idx,0)islands[idx]=sidx+=1else:isallgrid=Falseif isallgrid:print(m*n)else:result = 0for j in range(n):for k in range(m):count = 1visitedgraph=[]if graph[j][k]==0:directions=[[0,1],[0,-1],[1,0],[-1,0]]for l in range(4):j0,k0 = directions[l]new_j = j+j0new_k = k+k0if new_j<0 or new_j>=len(graph) or new_k<0 or new_k>=len(graph[0]):continueif graph[new_j][new_k] in visitedgraph: continuecount+=islands[graph[new_j][new_k]]visitedgraph.append(graph[new_j][new_k])result = max(result, count)print(result)
http://www.lryc.cn/news/397116.html

相关文章:

  • 韦尔股份:深蹲起跳?
  • docs | 使用 sphinx 转化rst文件为html文档
  • 【ChatGPT 消费者偏好】第二弹:ChatGPT在日常生活中的使用—推文分享—2024-07-10
  • Webpack配置及工作流程
  • 华为ensp实现防火墙的区域管理与用户认证
  • 深入解析 Laravel 策略路由:提高应用安全性与灵活性的利器
  • Java | Leetcode Java题解之第228题汇总区间
  • 使用Simulink基于模型设计(三):建模并验证系统
  • 基于go 1.19的站点模板爬虫
  • 0基础学会在亚马逊云科技AWS上搭建生成式AI云原生Serverless问答QA机器人(含代码和步骤)
  • [PaddlePaddle飞桨] PaddleOCR图像小模型部署
  • C语言 | Leetcode C语言题解之第227题基本计算题II
  • kafka.common.KafkaException: Socket server failed to bind to xx:9092
  • 【JS+H5+CSS实现烟花特效】
  • uniapp小程序使用webview 嵌套 vue 项目
  • 命令模式在金融业务中的应用及其框架实现
  • WordPress的性能优化有哪些方法?
  • 【Python基础】代码如何打包成exe可执行文件
  • Golang | Leetcode Golang题解之第227题基本计算器II
  • 云端美味:iCloud中食谱与餐饮计划的智能存储方案
  • leetcode:1332. 删除回文子序列(python3解法)
  • 智慧交通的神经中枢:Transformer模型在智能交通系统中的应用
  • PCIe驱动开发(1)— 开发环境搭建
  • YOLOv10改进 | Conv篇 | CVPR2024最新DynamicConv替换下采样(解决低FLOPs陷阱)
  • 变革设计领域:Transformer模型在智能辅助设计中的革命性应用
  • Spring——配置说明
  • 禁用华为小米?微软中国免费送iPhone15
  • nginx初理解
  • FreeCAD源码分析:属性系统
  • C++入门 模仿mysql控制台输出表格