【代码随想录算法训练营——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代码怎么改都不对,不知道问题在哪里。

终于找到原因了,原来是双层循环没有跳出。

代码

python 复制代码
#110.字符串接龙


python 复制代码
#105.有向图的完全可达性
def dfs(graph, visited, y):
    visited[y] = True
    for 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] = 1
    visited = [False] * (n + 1)
    dfs(graph, visited, 1)
    flag = True
    for i in range(1, n + 1):
        if visited[i] == False:
            flag = False
    if flag:
        print(1)
    else:
        print(-1)
python 复制代码
#106.岛屿的周长
#错误代码
point = [[1, 0], [0, 1], [-1, 0], [0, -1]]
result = 0
def dfs(graph, visited, x, y):
    global result
    visited[x][y] = True
    for 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 += 1
        elif graph[nextx][nexty] == 0:
            result += 1
        elif 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 = True
    for i in range(n):
        for j in range(m):
            if graph[i][j] == 1:
                dfs(graph, visited, i, j)
                flag = False
                break
        if not flag:
            break
    print(result)

#deeoseek加调试代码
point = [[1, 0], [0, 1], [-1, 0], [0, -1]]
result = 0

def dfs(graph, visited, x, y):
    global result
    visited[x][y] = True
    print(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 += 1
            print(f"  {direction}方向: 地图边界,周长+1 → {result}")
        elif graph[nextx][nexty] == 0:
            result += 1
            print(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, 5
    graph = [
        [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}")
相关推荐
坚持就完事了10 小时前
蓝桥杯中Python常用的库与模块
python·算法
立志成为大牛的小牛10 小时前
数据结构——四十四、平衡二叉树的删除操作(王道408)
数据结构·学习·程序人生·考研·算法
Suckerbin11 小时前
一次LeeCode刷题记录:接雨水
算法
Blossom.11811 小时前
RLHF的“炼狱“突围:从PPO到DPO的工业级对齐实战
大数据·人工智能·分布式·python·算法·机器学习·边缘计算
MobotStone12 小时前
从问答到决策:Agentic AI如何重新定义AI智能体的未来
人工智能·算法
Shemol13 小时前
二叉树的三种迭代遍历(无栈版本)-- 我在马克思主义课上的一些巧思
算法
胖咕噜的稞达鸭13 小时前
进程状态,孤儿进程僵尸进程,Linux真实调度算法,进程切换
linux·运维·算法
RTC老炮13 小时前
webrtc降噪-WienerFilter源码分析与算法原理
算法·webrtc
hweiyu0014 小时前
数据结构:数组
数据结构·算法
无限进步_14 小时前
C语言单向链表实现详解:从基础操作到完整测试
c语言·开发语言·数据结构·c++·算法·链表·visual studio