bfs总结 bfs题单 最短路 python (奇怪的电梯 好奇怪的游戏 迷宫 马的遍历 [USACO08FEB]Meteor Shower S)

1 可以用来遍历所有的点
2 可以用来找最短路
3 多源最短路,开始时一次向队列放之多个点

#板子
"""
def bfs():
    1 起始点入队
    标记入队的点
    while not q.empty():
        出队
        if 判断是否到达终点 到达返回
        for 遍历可走方向的点
            判断当前点是否可行
            入队
            标记
"""

P1135 奇怪的电梯

https://www.luogu.com.cn/problem/P1135

import queue
n,a,b=map(int,input().split())
l=[0]+list(map(int,input().split()))
vis=[0]*(n+1)
def right(x):#判断当前点是否合法
    if x>=1 and x<=n and vis[x]==0:
        return  True
    else:
        return False
q=queue.Queue()
def bfs(x):
    q.put((x,0))
    vis[x]=1
    while not q.empty():
        now=q.get()
        x=now[0]
        if now[0]==b:
            print(now[1])
            return#一定是return
        x1=x+l[x]
        x2=x-l[x]
        if right(x1):
            q.put((x1,now[1]+1))
            vis[x1]=1
        if right(x2):
            q.put((x2,now[1]+1))
            vis[x2]=1
    print(-1)#没找到自动结束循环
bfs(a)

P1747 好奇怪的游戏

https://www.luogu.com.cn/problem/P1747

两次掉头用bfs

from queue import Queue
a,b=map(int,input().split())
a1,b1=map(int,input().split())
vis= [[0] * (21) for i in range(21)]
#12个可走方向
dir=[(-1,2),(-2,2),(-2,1),(-1,-2),(-2,-1),(-2,-2),(2,1),(2,2),(1,2),(2,-2),(2,-1),(1,-2)]
def check(x,y):
    if x>=1 and x<=20 and y>=1 and y<=20 and vis[x][y]==0:
        return True
    return False
def bfs(x,y):#不同的搜索,要开不同的队列
    q = Queue()#一定要放在里面
    q.put((x,y,0))
    vis[x][y]=1
    while not q.empty():
        now=q.get()
        x1=now[0]
        y1=now[1]
        step=now[2]
        if x1==1 and y1==1:
            print(step)
            return
        for l in dir:
            x2=x1+l[0]
            y2=y1+l[1]
            if check(x2,y2):
                q.put((x2,y2,step+1))
                vis[x2][y2]=1

bfs(a,b)
vis= [[0] * (21) for i in range(21)]#再次初始化
bfs(a1,b1)

P1605 迷宫

https://www.luogu.com.cn/problem/P1605

n,m,t=map(int,input().split())
a,b,a1,b1=map(int,input().split())
dir=[(0,1),(0,-1),(-1,0),(1,0)]
vis=[[0]*(m+2) for i in range(n+2)]
la=[[0]*(m+2) for i in range(n+2)]
for i in range(t):
    x,y=map(int,input().split())
    la[x][y]="1"
ans=0
def right(x,y):
    if x>=1 and y>=1 and x<=n and y<=m:
        return True
    return False
def dfs(x,y):
    global ans
    vis[x][y]=1
    if x==a1 and y==b1:
        ans=ans+1
        return
    for l in dir:
        x1=x+l[0]
        y1=y+l[1]
        if la[x1][y1]==0 and vis[x1][y1]==0 and right(x1,y1):
            dfs(x1,y1)
            vis[x1][y1]=0
dfs(a,b)
print(ans)

P1443 马的遍历

https://www.luogu.com.cn/problem/P1443

import queue
n,m,x,y=map(int,input().split())
la=[[-1]*(m) for i in range(n)]#-1表示未访问过

dir=[(1,2),(2,1),(1,-2),(2,-1),(-1,-2),(-2,-1),(-1,2),(-2,1)]
def right(x,y):
    if x>=0 and y>=0 and x<n and y<m and la[x][y]==-1:
        return True
    else:
        return False
q=queue.Queue()
def bfs(x,y):
    q.put((x-1,y-1,0))
    la[x-1][y-1]=0
    while not q.empty():
        now=q.get()
        x1=now[0]
        y1=now[1]
        step=now[2]
        for l in dir:
            x2=x1+l[0]
            y2=y1+l[1]
            if right(x2,y2):
                q.put((x2,y2,step+1))
                la[x2][y2]=step+1
bfs(x,y)
for i in range(n):
    for j in range(m):
        print("{: <5d}".format(la[i][j]),end="")
    print()

P2895 [USACO08FEB]Meteor Shower S

https://www.luogu.com.cn/problem/P2895

import queue
m=int(input())
vis=[[0]*(303) for i in range(303)]#是否被访问过
la=[[-1]*(303) for j in range(303)]#流星标记位置
dir1=[(-1,0),(1,0),(0,1),(0,-1),(0,0)]
dir=[(-1,0),(1,0),(0,1),(0,-1)]
def right(x,y):
    if x>=0 and y>=0 and x<=301 and y<=301:
        return True
    else:
        return False
def right2(x,y):
    if x>=0 and y>=0 and vis[x][y]==0:
        return True
    else:
        return False
for i in range(m):
    a,b,t=map(int,input().split())

    for l in dir1:
        x1=a+l[0]
        y1=b+l[1]
        if right(x1,y1) and (la[x1][y1]==-1 or t<la[x1][y1]):
            la[x1][y1]=t
q=queue.Queue()
def bfs(x,y):
    q.put((x,y,0))
    vis[x][y]=1
    while not q.empty():
        now=q.get()
        x1=now[0]
        y1=now[1]
        s=now[2]
        if la[x1][y1]==-1:#若到达一个安全位置
            print(s)
            return
        for l in dir:
            x2=x1+l[0]
            y2=y1+l[1]
            if right2(x2,y2) and (s+1<la[x2][y2] or la[x2][y2]==-1):#位置合法且流星还未落下或该地方不会落流星
                q.put((x2,y2,s+1))
                vis[x2][y2]=1

    print(-1)
bfs(0,0)
#要考虑的细节较多

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值