c124. 00532 - Dungeon Master (迷宮)
python
from collections import deque
def is_valid(x, y, z):
return 0 <= x < C and 0 <= y < R and 0 <= z < L and (maze[z][y][x] == '.' or maze[z][y][x] == 'E')
def bfs(start_x, start_y, start_z, end_x, end_y, end_z):
queue = deque([(start_x, start_y, start_z, 0)])
visited = set()
while queue:
x, y, z, count = queue.popleft()
if (x, y, z) in visited:
continue
if x == end_x and y == end_y and z == end_z:
return count
visited.add((x, y, z))
direction = [(0, 1, 0), (1, 0, 0), (0, -1, 0), (-1, 0, 0), (0, 0, 1), (0, 0, -1)]
for dx, dy, dz in direction:
new_x, new_y, new_z = x + dx, y + dy, z + dz
new_pos = (new_x, new_y, new_z)
if is_valid(new_x, new_y, new_z) and new_pos not in visited:
queue.append((new_x, new_y, new_z, count + 1))
return -1
while True:
L, R, C = list(map(int, input().split()))
if L == R == C == 0:
break
maze = [[] for _ in range(L)]
for i in range(L):
for j in range(R):
line = input().strip()
maze[i].append(line)
if 'S' in line:
start_x, start_y, start_z = line.index('S'), j, i
if 'E' in line:
end_x, end_y, end_z = line.index('E'), j, i
input()
min_path = bfs(start_x, start_y, start_z, end_x, end_y, end_z)
ans = f'Escaped in {min_path} minute(s).' if min_path != -1 else 'Trapped!'
print(ans)