Python代码1
from collections import deque
l,r,c=map(int,input().split())
def bfs(x,y,z):
q=deque()
q.append((x,y,z))
st[x][y][z]=1
while q:
x0,y0,z0=q.popleft()
for i in range(3):
a=x0+dx[i]
for j in range(4):
if i==1:
b=y0+dy[j]
d=z0+dz[j]
else:
b=y0
d=z0
if a>=0 and a<l and b>=0 and b<r and d>=0 and d<c:
if st[a][b][d]==0 and m[a][b][d]=='.':
q.append((a,b,d))
st[a][b][d]=st[x0][y0][z0]+1
if m[a][b][d]=="E":
return st[x0][y0][z0]
return 0
while l!=0 and r!=0 and c!=0:
dx=[-1,0,1]
dy=[-1,0,1,0,0,0]
dz=[0,1,0,-1,0,0]
st=[[[0 for _ in range(c)]for _ in range(r)]for _ in range(l)]
m=[[] for _ in range(l)]
for i in range(l):
for j in range(r):
m[i].append(list(input()))
input()
for i in range(l):
for j in range(r):
for k in range(c):
if m[i][j][k]=='S':
res=bfs(i,j,k)
if res>0:
print(f'Escaped in {res} minute(s).')
else:
print("Trapped!")
l,r,c=map(int,input().split())
Python代码2
from collections import deque
l,r,c=map(int,input().split())
def bfs(z,x,y):
q.append((z,x,y))
dist[z][x][y]=0
while q:
z1,x1,y1=q.popleft()
for i in range(6):
a=x1+dx[i]
b=y1+dy[i]
d=z1+dz[i]
if a>=r or b>=c or d>=l or d<0 or a<0 or b<0 or dist[d][a][b]!=-1 or maze[d][a][b]=='#':
continue
q.append((d,a,b))
dist[d][a][b]=dist[z1][x1][y1]+1
if maze[d][a][b]=='E':
return f'Escaped in {dist[d][a][b]} minute(s).'
return 'Trapped!'
while l!=0 and r!=0 and c!=0:
maze=[]
dx=[-1,0,1,0,0,0]
dy=[0,1,0,-1,0,0]
dz=[0,0,0,0,-1,1]
for _ in range(l):
floor = []
for _ in range(r):
floor.append(list(input().strip()))
maze.append(floor)
kong=list(input())
q=deque()
dist=[[[-1 for _ in range(c)]for _ in range(r)]for _ in range(l)]
for i in range(l):
for j in range(r):
for k in range(c):
if maze[i][j][k] == 'S':
print(bfs(i, j, k))
break
else:
continue
break
else:
continue
break
l,r,c=map(int,input().split())