蓝桥杯-广度优先搜索bfs

  1. 全球变暖-178
    python标准库-queue
from queue import Queue

n = int(input())
ph = []
for i in range(n):
    ph.append(input())
book = [[0]*n for _ in range(n)]
drct = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(x, y):
    global flag
    q = Queue()
    q.put((x, y))
    book[x][y] = 1
    while not q.empty():
        t = q.get()
        tx, ty = t[0], t[1]
        # 不可以写成tx, ty = q.get()[0], q.get()[1](执行了两次q.get()操作)
        if ph[tx+1][ty] == '#' and ph[tx-1][ty] == '#' and ph[tx][ty+1] == '#' and ph[tx][ty-1] == '#':
            flag = 1
        for i in range(4):
            nx = tx + drct[i][0]
            ny = ty + drct[i][1]
            if book[nx][ny] == 0 and ph[nx][ny] == '#':
                q.put((nx, ny))
                book[nx][ny] = 1

ans = 0
for i in range(n):
    for j in range(n):
        if book[i][j] == 0 and ph[i][j] == '#':
            flag = 0
            bfs(i, j)
            if flag == 0:
                ans += 1
print(ans)

在此题基础上改一改:有多少个独立小岛?(淹没之前)

from queue import Queue
n = int(input())
ph = []
for i in range(n):
    ph.append(input())
book = [[0]*n for _ in range(n)]
drct = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(x, y):
    q = Queue()
    q.put((x, y))
    while not q.empty():
        t = q.get()

        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]
            if book[nx][ny] == 0 and ph[nx][ny] == '#':
                q.put((nx, ny))
                book[nx][ny] = 1

ans = 0
for i in range(n):
    for j in range(n):
        if book[i][j] == 0 and ph[i][j] == '#':
            bfs(i, j)
            ans += 1
print(ans)

2.迷宫-602

from queue import Queue
n, m = 30, 50 #30行50列
maze = []
for i in range(n):
    maze.append(input())
book = [[0]*m for i in range(n)]
drct = [[1, 0], [0, -1], [0, 1], [-1, 0]]
drct_s = 'DLRU'

def bfs(x, y, z, w): #x, y, step, path
    q = Queue()
    if maze[x][y] != '1' and book[x][y] == 0:
        q.put((x, y, z, w))
        book[x][y] = 1
    while not q.empty():
        t = q.get()
        if t[0] == n-1 and t[1] == m-1:
            print(t[3])
            break
        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]

            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if book[nx][ny] == 1:
                continue
            if maze[nx][ny] == '1':
                continue

            nstep = t[2] + 1
            npath = t[3] + drct_s[i]

            q.put((nx, ny, nstep, npath))
            book[nx][ny] = 1

bfs(0, 0, 0, '')
  1. 穿越雷区-141
from queue import Queue
n = int(input())
mp = []
for i in range(n):
    a = input().split()
    mp.append(a)
    if 'A' in a:
        aa = [i, a.index('A')]
    if 'B' in a:
        bb = [i, a.index('B')]    
book = [[0]*n for _ in range(n)]
drct = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def bfs(x, y, z):  # x, y, step
    q = Queue()
    q.put((x, y, z))
    book[x][y] = 1
    while not q.empty():
        t = q.get()
        if t[0] == bb[0] and t[1] == bb[1]:
            print(t[2])
            break
        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]

            if nx < 0 or nx >= n or ny < 0 or ny >= n:
                continue
            if book[nx][ny] == 1:
                continue
            if mp[nx][ny] == mp[t[0]][t[1]]:
                continue

            nstep = t[2] + 1
            q.put((nx, ny, nstep))
            book[nx][ny] = 1

bfs(aa[0], aa[1], 0)   
  1. 灌溉-551
    感觉这题用bfs麻烦了些,应该有更好的方法。
from queue import Queue
n, m = map(int, input().split()) # n 行 m 列
t = int(input()) # 出水管数量
pos = [] # 出水管位置
book = [[0]*m for _ in range(n)]
for i in range(t):
    a, b = map(int, input().split())
    book[a-1][b-1] = 1
    l = [a-1, b-1]
    pos.append(l)
k = int(input()) # k分钟后,有多少个方格被灌溉好
drct = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(x, y, minu):
    q = Queue()
    q.put((x, y, minu))
    book[x][y] = 1
    while not q.empty():
        t = q.get()
        if t[2] == k:
            break
        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]

            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if book[nx][ny] == 1:
                continue
            q.put((nx, ny, minu+1))
            book[nx][ny] = 1

for i in pos:
    bfs(i[0], i[1], 0)

ans = 0
for i in range(n):
    for j in range(m):
        if book[i][j] == 1:
            ans += 1

print(ans)
  1. 宝岛探险
from queue import Queue

n, m, sx, sy = map(int, input().split())  #n行m列, startx, starty
sx -= 1
sy -= 1
mp = []
for i in range(n):
    a = list(map(int, input().split()))
    assert len(a) == m
    mp.append(a)
assert len(mp) == n
book = [[0]*m for _ in range(n)]
drct = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(x, y):
    q = Queue()
    q.put((x, y))
    book[x][y] = 1
    while not q.empty():
        t = q.get()

        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]

            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if book[nx][ny] == 1:
                continue
            if mp[nx][ny] == 0:
                continue

            q.put((nx, ny))
            book[nx][ny] = 1

bfs(sx, sy)

ans = 0
for i in range(n):
    for j in range(m):
        if book[i][j] == 1:
            ans += 1
print(ans)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值