<代码随想录> 算法训练营-2025.01.04

110. 字符串接龙
python 复制代码
#邻接表记录 只差一个元素的列表
from collections import deque

#判断两个字符串是不是只相差一个字符
def distinguish(str1,str2):
    if len(str1)!=len(str2):
        return False
    flag=0
    for i in range(len(str1)):
        if str1[i]!=str2[i]:
            flag+=1
    return flag<=1

def assemble(strList):
    dict={}
    for i in range(len(strList)):
        temp=[]
        for j in range(len(strList)):
            if i==j:
                continue
            if distinguish(strList[i],strList[j]):
                temp.append(strList[j])
        dict[strList[i]]=temp
    
    return dict
    
    
def main():
    n=int(input())
    beginStr,endStr=input().split()
    strList=[]
    for i in range(n):
        strList.append(input())
    strList.append(beginStr)
    strList.append(endStr)
    adList=assemble(strList)
    res=1
    #从起始的邻接开始广度遍历
    dq=deque()
    dq.append(beginStr)
    visited=set()
    visited.add(beginStr)
    while len(dq)>0:
        res+=1
        for i in range(len(dq)):
            tempStr=dq.popleft()
            tempList=adList[tempStr]
            for j in range(len(tempList)):
                if tempList[j]==endStr:
                    print(res)
                    return
                if tempList[j] not in visited:
                    dq.append(tempList[j])
                    visited.add(tempList[j])
        
    
    print(0)
    

if __name__=="__main__":
    main()
[105. 有向图的完全可达性

](https://kamacoder.com/problempage.php?pid=1177)

思路:bfs判断节点是否全部可达

python 复制代码
from collections import defaultdict,deque

def main():
    n,k=map(int,input().split())
    dict=defaultdict(list)
    for i in range(k):
        s,t=map(int,input().split())
        dict[s].append(t)

    #把从1出发能到达的点都加入到visited中,判断大小是否为n
    dq=deque()
    dq.append(1)
    visited=set()
    visited.add(1)
    while len(dq)>0:
        temp=dq.popleft()
        tempList=dict[temp]
        for node in tempList:
            if node not in visited:
                dq.append(node)
                visited.add(node)
    res=1 if len(visited)==n else -1
    print(res)
        
        
        
if __name__=="__main__":
    main()
106. 岛屿的周长
python 复制代码
#每个1的四边有0则周长加1 越界也加1

direction=[[-1,0],[1,0],[0,-1],[0,1]]
def main():
    n,m=map(int,input().split())
    graph=[[0]* m for _ in range(n)]
    for i in range(n):
        graph[i]=list(map(int,input().split()))
        
    res=0
    for i in range(n):
        for j in range(m):
            if graph[i][j]==1:
                for d in direction:
                    x=i+d[0]
                    y=j+d[1]
                    if 0<=x<=n-1 and 0<=y<=m-1 :
                        if graph[x][y]==0:
                            res+=1
                    else:
                        res+=1
    print(res)                        
                        
if __name__=="__main__":
    main()
相关推荐
多米Domi01110 分钟前
0x3f 第25天 黑马web (145-167)hot100链表
数据结构·python·算法·leetcode·链表
LYFlied10 分钟前
【每日算法】LeetCode 207. 课程表
算法·leetcode·职场和发展
sali-tec12 分钟前
C# 基于OpenCv的视觉工作流-章7-膨胀
图像处理·人工智能·opencv·算法·计算机视觉
叫我:松哥15 分钟前
基于机器学习的地震风险评估与可视化系统,采用Flask后端与Bootstrap前端,系统集成DBSCAN空间聚类算法与随机森林算法
前端·算法·机器学习·flask·bootstrap·echarts·聚类
一起养小猫16 分钟前
LeetCode100天Day12-删除重复项与删除重复项II
java·数据结构·算法·leetcode
小O的算法实验室22 分钟前
2023年IEEE TITS SCI2区TOP,增强遗传算法+分布式随机多无人机协同区域搜索路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
Allen_LVyingbo30 分钟前
病历生成与质控编码的工程化范式研究:从模型驱动到系统治理的范式转变
前端·javascript·算法·前端框架·知识图谱·健康医疗·easyui
一起努力啊~30 分钟前
算法刷题--螺旋矩阵II+区间和+开发商购买土地
数据结构·算法·leetcode
Swift社区31 分钟前
LeetCode 470 用 Rand7() 实现 Rand10()
算法·leetcode·职场和发展
闻缺陷则喜何志丹33 分钟前
【图论 DFS 换根法】3772. 子图的最大得分|2235
c++·算法·深度优先·力扣·图论·换根法