<代码随想录> 算法训练营-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()
相关推荐
数智工坊1 分钟前
周志华《Machine Learning》学习笔记--第六章--支持向量机
笔记·神经网络·学习·算法·机器学习·支持向量机
casual~3 分钟前
【学习记录】
学习·算法
社交怪人6 分钟前
【奇偶ASCII值】信息学奥赛一本通C语言解法(题号1042)
算法
小欣加油24 分钟前
leetcode3635 最早完成陆地和水上游乐设施的时间II
数据结构·c++·算法·leetcode
GUO_PP1 小时前
win11英雄联盟打开以后,自动改变音效,开启免提模式的问题修正
人工智能·算法
变量未定义~1 小时前
排列数字、 n-皇后问题
数据结构·算法
BirdenT1 小时前
20260604紫题训练
c++·算法
元启数宇1 小时前
疏散指示AI实战:规范布点与路径推演全流程
人工智能·算法
tg:;1 小时前
Catkin 常用命令
开发语言·c++·算法
暖阳华笺2 小时前
【高频考点】回溯(暴力搜索)
数据结构·c++·算法·回溯法