此专栏保留BFS题目,适于保存复习
1.wyh的迷宫
思路:
模板,先找起点和终点,记住每一步要发生的书写
from collections import deque
def bfs():
dir = [(1, 0), (0, -1), (-1, 0), (0, 1)]
n, m = map(int, input().split())
start = []
end = []
list1 = []
for _ in range(n):
list1.append(list(input()))
for i in range(n):
for j in range(m):
if list1[i][j] == 's':
start = [i, j]
list1[i][j] = '.'
elif list1[i][j] == 't':
end = [i, j]
list1[i][j] = '.'
q = deque([start])
s = set()
s.add((start[0],start[1]))
while q:
x, y = q.popleft()
if x == end[0] and y == end[1]:
print('YES')
return
for i in dir:
nx, ny = x + i[0], y + i[1]
if 0 <= nx < n and 0 <= ny < m and (nx,ny) not in s and list1[nx][ny] == '.':
q.append((nx, ny))
s.add((nx,ny))
print('NO')
return
t = int(input())
for i in range(t):
bfs()
2.小红走矩阵
思路:
在q里多传了一个参数记录最小步长,注意必须走到和当前格子不同这个条件
from collections import deque
def bfs():
n, m = map(int, input().split())
list1 = []
for i in range(n):
list1.append(list(input()))
q = deque([(0, 0, 0)])
dir = [(1, 0), (-1, 0), (0, 1), (0, -1)]
s = set()
s.add((0,0))
while q:
x, y, step = q.popleft()
if x == n - 1 and y == m - 1:
print(step)
return
for i in dir:
nx, ny = x + i[0], y + i[1]
if 0 <= nx < n and 0 <= ny < m and (nx,ny) not in s and list1[x][y] != list1[nx][ny]:
q.append((nx, ny, step + 1))
s.add((nx,ny))
print(-1)
return
bfs()
3.魔法森林的蘑菇
思路:
注意
q = deque([(start[0],start[1],0,0),(start[0],start[1],1,0),(start[0],start[1],2,0),(start[0],start[1],3,0)])
这句话,是从四个方向搜索,
di , dj = dir[d]
代表每一行往前走。还要注意如何不回头
from collections import deque
N = int(input())
for _ in range(N):
m , n = list(map(int,input().split()))
list1 = []
for _ in range(m):
list1.append(input())
start , target = None , None
for i in range(m):
for j in range(n):
if list1[i][j] == 'S':
start = (i,j)
if list1[i][j] == 'T':
target = (i,j)
dir = [(0,1),(1,0),(0,-1),(-1,0)]
q = deque([(start[0],start[1],0,0),(start[0],start[1],1,0),(start[0],start[1],2,0),(start[0],start[1],3,0)])
s = set()
for x in q:
s.add(x)
label = False
while q:
for _ in range(len(q)):
i , j , d , step = q.popleft()
if (i,j) == target:
print(step)
q = None
label = True
break
di , dj = dir[d]
ni , nj = i + di , j + dj
if 0 <= ni < m and 0 <= nj < n:
if list1[ni][nj] == '*':
for nd in range(4):
if nd != (d+2)%4 and (ni,nj,nd) not in s:
s.add((ni,nj,nd))
q.append((ni,nj,nd,step+1))
elif list1[ni][nj] == '.' or list1[ni][nj] == 'T':
if (ni,nj,d) not in s:
s.add((ni,nj,d))
q.append((ni,nj,d,step+1))
if not label:
print(-1)
4.魔法数字
思路:
用 step来储存
from collections import deque
def bfs():
a = input().split(',')
n = int(a[0])
m = int(a[1])
s = set()
q = deque([(n,0)])
while q:
x,step = q.popleft()
if x == m:
print(step)
return
if x + 1 not in s:
q.append((x+1,step+1))
s.add(x+1)
if x - 1 not in s:
q.append((x-1,step+1))
s.add(x-1)
if x ** 2 not in s:
q.append((x**2,step+1))
s.add(x**2)
bfs()
5.这一定是一道数学题
from collections import deque
def bfs():
n, x = map(int, input().split())
s = set()
q = deque([(x, 0)])
s.add(x)
while q:
shu,step = q.popleft()
if len(str(shu)) == n:
print(step)
return
for i in range(len(str(shu))):
w = int(str(shu)[i])
if shu * w not in s:
s.add(shu * w)
q.append((shu * w,step + 1))
print(-1)
return
bfs()