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

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

卡码网题目链接
https://kamacoder.com/problempage.php?pid=1173
https://kamacoder.com/problempage.php?pid=1174
https://kamacoder.com/problempage.php?pid=1175
https://kamacoder.com/problempage.php?pid=1176

题解
101.孤岛的总面积
怎么不计算有边缘面积的岛屿呢?
在这里插入图片描述
最后只要重新遍历一遍地图就可以,因为是求面积。
在dfs遍历中,遇到海洋要跳过,这是一个错误点。
bfs很多语法不太熟悉,要照着写。

102.沉没孤岛
能不能把上一题用到的graph留下,对照着原始的graph将其孤岛变为0,再输出,这样的话代码几乎和上题一样。看看题解有没有什么更好的方法。
在这里插入图片描述
果然有更好的方法,就是标记2。

103.水流问题
遍历每一个点,看是否能到达第一组边界和第二组边界,同时每次遍历它周边的点时要看是否是坡度相等或更低的地形。还要一个visited数组,每次遍历时重新初始化为全False,避免重复遍历同一个点。
看了下题解,直接用visited的true痕迹记录到达第一边界和第二边界与否。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

104.建造最大岛屿
暴力法,对每个点变成1,然后从该点开始bfs遍历计算当前的岛屿面积是多少,总的取最大值就行,但会不会和上一题一样是超时?看题解知道的,记住这样的算法时间复杂度是n^4。
优化方法看着有些难,c++和python的实现都是。题解的代码看不懂,建议多看。

代码

#101.孤岛的总面积
#dfs法
point = [[1, 0], [0, 1], [-1, 0], [0, -1]]
def dfs(graph, x, y):graph[x][y] = 0for i in range(4):nextx = x + point[i][0]nexty = y + point[i][1]if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):continueif graph[nextx][nexty] == 0: continue;dfs(graph, nextx, nexty)if __name__ == "__main__":n, m = map(int, input().split())graph = []for i in range(n):graph.append(list(map(int, input().split())))for i in range(n):if graph[i][0] == 1:dfs(graph, i, 0)if graph[i][m - 1] == 1:dfs(graph, i, m - 1)for j in range(m):if graph[0][j] == 1:dfs(graph, 0, j)if graph[n - 1][j] == 1:dfs(graph, n - 1, j)result = 0for i in range(n):for j in range(m):if graph[i][j] == 1:result += 1print(result)#bfs法
from collections import deque
point = [[1, 0], [0, 1], [-1, 0], [0, -1]]
def bfs(graph, x, y):queue = deque()queue.append(x)queue.append(y)while queue:i = queue.popleft()j = queue.popleft()graph[i][j] = 0for t in range(4):nextx = i + point[t][0]nexty = j + point[t][1]if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):continueif graph[nextx][nexty] == 0: continue;queue.append(nextx)queue.append(nexty)if __name__ == "__main__":n, m = map(int, input().split())graph = []for i in range(n):graph.append(list(map(int, input().split())))for i in range(n):if graph[i][0] == 1:bfs(graph, i, 0)if graph[i][m - 1] == 1:bfs(graph, i, m - 1)for j in range(m):if graph[0][j] == 1:bfs(graph, 0, j)if graph[n - 1][j] == 1:bfs(graph, n - 1, j)result = 0for i in range(n):for j in range(m):if graph[i][j] == 1:result += 1print(result)
#102.沉没孤岛
#dfs法
point = [[0, 1], [1, 0], [0, -1], [-1, 0]]
def dfs(graph, x, y):graph[x][y] = 2for i in range(4):nextx = x + point[i][0]nexty = y + point[i][1]if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):continueif graph[nextx][nexty] == 1:dfs(graph, nextx, nexty)if __name__ == "__main__":n, m = map(int, input().split())graph = []for i in range(n):graph.append(list(map(int, input().split())))for i in range(n):if graph[i][0] == 1:dfs(graph, i, 0)if graph[i][m - 1] == 1:dfs(graph, i, m - 1)for j in range(m):if graph[0][j] == 1:dfs(graph, 0, j)if graph[n - 1][j] == 1:dfs(graph, n - 1, j)for i in range(n):for j in range(m):if graph[i][j] == 1:graph[i][j] = 0for i in range(n):for j in range(m):if graph[i][j] == 2:graph[i][j] = 1for i in range(n):for j in range(m - 1):print(f"{graph[i][j]} ", end = '')print(graph[i][m - 1])#bfs法
from collections import deque
point = [[0, 1], [1, 0], [0, -1], [-1, 0]]
def bfs(graph, x, y):queue = deque()queue.append(x)queue.append(y)while queue:i = queue.popleft()j = queue.popleft()graph[i][j] = 2for t in range(4):nextx = i + point[t][0]nexty = j + point[t][1]if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):continueif graph[nextx][nexty] == 1:queue.append(nextx)queue.append(nexty)if __name__ == "__main__":n, m = map(int, input().split())graph = []for i in range(n):graph.append(list(map(int, input().split())))for i in range(n):if graph[i][0] == 1:bfs(graph, i, 0)if graph[i][m - 1] == 1:bfs(graph, i, m - 1)for j in range(m):if graph[0][j] == 1:bfs(graph, 0, j)if graph[n - 1][j] == 1:bfs(graph, n - 1, j)for i in range(n):for j in range(m):if graph[i][j] == 1:graph[i][j] = 0for i in range(n):for j in range(m):if graph[i][j] == 2:graph[i][j] = 1for i in range(n):for j in range(m - 1):print(f"{graph[i][j]} ", end = '')print(graph[i][m - 1])
#103.水流问题
#我写的不对的代码
point = [[0, 1], [1, 0], [-1, 0], [0, -1]]
def dfs(graph, visited, x, y, local_contact):visited[x][y] = Truefor i in range(4):nextx = x + point[i][0]nexty = y + point[i][1]if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):continueif graph[nextx][nexty] <= graph[x][y] and visited[nextx][nexty] == False:if nextx == 0 or nexty == 0:local_contact[x][y][0] = Trueif nextx == n - 1 or nexty == m - 1:local_contact[x][y][1] = Truevisited[nextx][nexty] = Truedfs(graph, visited, nextx, nexty, local_contact)if __name__ == "__main__":n, m = map(int, input().split())graph = []for i in range(n):graph.append(list(map(int, input().split())))result = []local_contact = [[[False] * 2 for _ in range(m)] for _ in range(n)]for i in range(n):for j in range(m):visited = [[False] * m for _ in range(n)]dfs(graph, visited, i, j, local_contact)for i in range(n):for j in range(m):if local_contact[i][j][0] == True and local_contact[i][j][1] == True:result.append([i, j])for i in range(len(result)):print(f"{result[i][0]} {result[i][1]}")
#题解的暴力法,跟我的思路一样,实现不同,具体去看题解的代码,因为是c++不好放上来,而且这个思路超时了,时间复杂度是O(M^2*N*2)
#代码略
#优化代码
point = [[0, 1], [1, 0], [-1, 0], [0, -1]]
def dfs(graph, visited, x, y):visited[x][y] = Truefor i in range(4):nextx = x + point[i][0]nexty = y + point[i][1]if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):continueif graph[nextx][nexty] >= graph[x][y] and visited[nextx][nexty] == False:#visited[nextx][nexty] = Truedfs(graph, visited, nextx, nexty)if __name__ == "__main__":n, m = map(int, input().split())graph = []for i in range(n):graph.append(list(map(int, input().split())))result = []firstBorder = [[False] * m for _ in range(n)]secondBorder = [[False] * m for _ in range(n)]for i in range(n):dfs(graph, firstBorder, i, 0)dfs(graph, secondBorder, i, m - 1)for j in range(m):dfs(graph, firstBorder, 0, j)dfs(graph, secondBorder, n - 1, j)for i in range(n):for j in range(m):if firstBorder[i][j] == True and secondBorder[i][j] == True:result.append([i, j])for i in range(len(result)):print(f"{result[i][0]} {result[i][1]}")
#104.建造最大岛屿

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://www.dtcms.com/a/550306.html

相关文章:

  • 设计最好的网站云企网站
  • 3网站建设wordpress设置版权信息
  • Vue + TSX 中使用 class 报错 解决方法
  • React 项目Source-Map-Explorer打包优化 CDN配置加快页面访问速度 包可视化 项目体积优化
  • 文昌网站 做炸饺子网络培训心得体会500字
  • 漳州做网站设计做网站网页需要什么软件
  • 义乌网站建设工作室网站建设前台后台设计
  • 网站搭建软件做门户网站需要什么条件
  • 开发网站做什么萨龙wordpress
  • 函数的嵌套调用求最大值
  • 2v1带您实战12nm高级数字后端
  • Linux之文本处理小工具+shell从入门到精通
  • 怎样提高自己网站排名nginx 网站建设
  • 基于node网站毕设代做广告设计与制作是干什么的
  • [论文笔记] windows wsl快乐试用cursor
  • 购物网站建设策划报告东莞松山湖邮编
  • 【科研】想学习的琐碎知识点-不断更新
  • (根号分治、sosdp)洛谷 P10408 Apple / P7842 探险者笔记 III 题解
  • SCADA软件GENESIS64:基于Web的集中式工程管理平台
  • 专注江苏网站建设五华网站建设 优帮云
  • 如何查询网站域名备案重庆微信网站建设多少钱
  • Apache Doris 4.0 版本正式发布:全面升级 AI 与搜索能力,强化离线计算
  • 苏州网站建设工作室主要的网站开发技术
  • 实现 “先排除数组中对象的 showTiltle: true 字段,再判断两个数组的对象内容是否完全相同(不考虑顺序、只关注对象值和增删和值的变化)”
  • 织梦cms做好的网站怎样上传到服务器做企业网站的步骤
  • 个人备案网站改企业备案网上卡片制作
  • h5游戏免费下载:打喷嚏的喷火龙
  • 网站开发中网页打印网站开发维护运维
  • C语言 了解一下回调函数(钩子函数)的使用
  • 网站制作内容在线网站制作系统源码