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

【代码随想录算法训练营——Day53】图论——110.字符串接龙、105.有向图的完全可达性、106.岛屿的周长

卡码网题目链接
https://kamacoder.com/problempage.php?pid=1183
https://kamacoder.com/problempage.php?pid=1177
https://kamacoder.com/problempage.php?pid=1178

题解
110.字符串接龙
因为接连做两天的题,有点放弃治疗。感觉这题不会。
看题解,原来要画图。关于做题原理有很多细节,看题解。

105.有向图的完全可达性
用邻接矩阵存储图,从1开始出发用一次dfs,同时用visited数组记录是否可达,最后判断visited是否都为true即可。
在这里插入图片描述
在这里插入图片描述

106.岛屿的周长
相当于是统计相邻的1的个数。我这份dfs代码怎么改都不对,不知道问题在哪里。
在这里插入图片描述
终于找到原因了,原来是双层循环没有跳出。

代码

#110.字符串接龙

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

#105.有向图的完全可达性
def dfs(graph, visited, y):visited[y] = Truefor i in range(1, len(graph)):if graph[y][i] == 1 and visited[i] == False:dfs(graph, visited, i)if __name__ == "__main__":n, m = map(int, input().split())graph = [[0] * (n + 1) for _ in range(n + 1)]for i in range(m):s, t = map(int, input().split())graph[s][t] = 1visited = [False] * (n + 1)dfs(graph, visited, 1)flag = Truefor i in range(1, n + 1):if visited[i] == False:flag = Falseif flag:print(1)else:print(-1)
#106.岛屿的周长
#错误代码
point = [[1, 0], [0, 1], [-1, 0], [0, -1]]
result = 0
def dfs(graph, visited, x, y):global resultvisited[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]):result += 1elif graph[nextx][nexty] == 0:result += 1elif graph[nextx][nexty] == 1 and visited[nextx][nexty] == False:dfs(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())))visited = [[False] * m for _ in range(n)]flag = Truefor i in range(n):for j in range(m):if graph[i][j] == 1:dfs(graph, visited, i, j)flag = Falsebreakif not flag:breakprint(result)#deeoseek加调试代码
point = [[1, 0], [0, 1], [-1, 0], [0, -1]]
result = 0def dfs(graph, visited, x, y):global resultvisited[x][y] = Trueprint(f"访问格子({x},{y})")for i in range(4):nextx = x + point[i][0]nexty = y + point[i][1]direction = ["下", "右", "上", "左"][i]if nextx < 0 or nextx >= len(graph) or nexty < 0 or nexty >= len(graph[0]):result += 1print(f"  {direction}方向: 地图边界,周长+1 → {result}")elif graph[nextx][nexty] == 0:result += 1print(f"  {direction}方向: 遇到水域,周长+1 → {result}")elif graph[nextx][nexty] == 1 and not visited[nextx][nexty]:print(f"  {direction}方向: 递归到({nextx},{nexty})")dfs(graph, visited, nextx, nexty)if __name__ == "__main__":n, m = 5, 5graph = [[0, 0, 0, 0, 0],[0, 1, 0, 1, 0], [0, 1, 1, 1, 0],[0, 1, 1, 1, 0],[0, 0, 0, 0, 0]]visited = [[False] * m for _ in range(n)]# 从(1,1)开始dfs(graph, visited, 1, 1)print(f"最终结果: {result}")
http://www.dtcms.com/a/549667.html

相关文章:

  • 【代码审计】ECShop_V4.1.19 SQL注入漏洞 分析
  • 桐城市住房和城乡建设局网站广东网广东网站建设
  • 网站后台与前台家具网站开发设计任务书与执行方案
  • 网站的文案物流的网站模板
  • ESP32在arduino环境下的离线安装 -- 理论上多个版本都有效
  • Windows的mklink创建符号链使用方法
  • 小清新网站风格正规接单赚佣金的平台
  • 应用APP开发程序编辑中的数据加密和解密以及签名使用解释技巧
  • Spring Boot3零基础教程,Function 各种写法,笔记97
  • 好文与笔记分享 A Survey of Context Engineering for Large Language Models(上)
  • 贵阳营销型网站建设wordpress用思源黑体
  • ksycopg2实战:Python连接KingbaseES数据库的完整指南
  • mediwiki 做网站做网站有发展吗
  • 如何做网站服务器映射计算机专业学做网站吗
  • 拟人AI GoCap:用机器学习打造真实玩家体验
  • cocos里UV坐标显示取值变化异常问题
  • 【Python】包管理神器-uv
  • 设计模式——抽象工厂方法(abstract factory)
  • 追剧狂人网页入口 - 免费影视在线观看网站
  • 以太网多参量传感器:工业物联网时代的安全监测革新
  • wordpress整站模板php 怎么做视频网站
  • Rust 内存对齐与缓存友好设计
  • 百度快照 直接进网站wordpress小工具缓存
  • 【英飞凌TC364】点亮LED灯
  • LeetCode 3346.执行操作后元素的最高频率 I:滑动窗口(正好适合本题数据,II再另某他法)
  • 【STM32】FLASH闪存
  • 东莞网站关键词推广义乌百度推广公司
  • Spring远程调用与Web服务全解析
  • 手机站喝茶影视茂名市建设银行网站
  • 青岛做网站公司排名淄博网站建设yx718