ITPub博客

首页 > IT职业 > IT生活 > 2021蓝桥杯(Python)骗分指南

2021蓝桥杯(Python)骗分指南

IT生活 作者:专注的阿熊 时间:2021-04-19 17:18:54 0 删除 编辑

Ascii 值转换

AA=ord('a')      # crd() 函数就是用来返回单个字符的 ascii 值( 0-255 )

BB=chr(65)       # chr() 函数是输入一个整数 [0 , 255] 返回其对应的 ascii 符号

>>>print(AA)

97

>>>print(BB)

'A'

BFS 基本例题

graph={'A':['B','C'],

       'B':['A','C'],

       'C':['B','A'],

       'D':['B','C','E'],

       'E':['D','C','B'],

       'F':['D']}

def BFS(graph,s):

    queue=[]

    queue.append(s)

    seen=[]

    seen.append(s)

    while len(queue)>0:

        vertex=queue.pop(0)

        nodes=graph[vertex]

        for i in nodes:

            if i not in seen:

                queue.append(i)

                seen.append(i)

        print(vertex)

BFS(graph,'E')

>>>

E

D

C

B

A

DFS 基本例题

graph={'A':['B','C'],

       'B':['A','C','D'],

       'C':['B','A','D','E'],

       'D':['B','C','E','F'],

       'E':['D','C'],

       'F':['D']}

def DFS(graph,s):

    stack=[]

    stack.append(s)

    seen=[]

    seen.append(s)

    while len(stack)>0:

        vertex=stack.pop()

        nodes=graph[vertex]

        for i in nodes:

            if i not in seen:

                stack.append(i)

                seen.append(i)

        print(vertex)

DFS(graph,'E')

>>>

E

C

A

B

D

F

BFS 最短路径问题

graph={'A':['B','C'],

       'B':['A','C','D'],

       'C':['B','A','D'],

       'D':['B','C','E','F'],

       'E':['D','C'],

       'F':['D']}

def BFS(graph,s):

    queue=[]

    queue.append(s)

    seen=[]

    seen.append(s)

    parent={s:None}  

    while len(queue)>0:

        vertex=queue.pop(0)

        nodes=graph[vertex]

        for w in nodes:

            if w not in seen:

                queue.append(w)

                seen.append(w)

                parent[w]=vertex

        #print(vertex)

    return parent    

parent=BFS(graph,'E')          #  E   终点

''' #外汇跟单gendan5.com 输出从 B 到 E 的最短路径

v='B'

while v!=None:

    print(v)

    v=parent[v]    

'''

v='B'             #  B     起点

way=[]

while v!=None:

    way.append(v)

    v=parent[v]

print(way,len(way))


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2768781/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Python每日笔记07
下一篇: 二、Java之万年历
请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    410
  • 访问量
    276814