本文主要是介绍大胖子走迷宫,bfs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.大胖子走迷宫 - 蓝桥云课 (lanqiao.cn)
from collections import dequen,k=map(int,input().split())
mp=[0]
for i in range(n):mp.append('0'+input())
vis=[[0 for i in range(n+1)] for i in range(n+1)]
q=deque()
q.append((3,3,0))
vis[3][3]=1
def f(t):if t<k:return 2elif t<2*k:return 1else :return 0def j1(x,y,z):for i in range(x-z,x+z+1):for j in range(y-z,y+z+1):if mp[i][j]=='*':return 0return 1def bfs():s=[[0,1],[0,-1],[1,0],[-1,0]]while len(q):x,y,t=q.popleft()if x==n-2 and y==n-2: return ttt = t + 1z = f(t)if z!=0: q.append((x,y,tt))for i,j in s:xx=x+iyy=y+jif xx-z<=0 or yy-z<=0 or xx+z>n or yy+z>n: continueif vis[xx][yy]==1:continueif j1(xx,yy,z)==1:q.append((xx,yy,tt))vis[xx][yy]=1print(bfs())
这篇关于大胖子走迷宫,bfs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!