栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 
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 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1025268.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号