Algorithm
[백준 6593] 상범 빌딩
moguogu
2024. 8. 31. 17:51
문제
https://www.acmicpc.net/problem/6593
코드
import sys
from collections import deque
input = sys.stdin.readline
d = ((1,0,0),(-1,0,0),(0,1,0),(0,-1,0),(0,0,1),(0,0,-1))
def Bfs(start_l, start_x, start_y):
q = deque()
q.append((start_l, start_x, start_y))
visited[start_l][start_x][start_y] = 1
while q:
v, x, y = q.popleft()
if v == end_l and x == end_x and y == end_y:
return visited[v][x][y] -1
for dv, dx, dy in d:
nv, nx, ny = dv + v, dx + x , dy + y
if not(0<=nx<R and 0<=ny<C and 0<=nv<L): continue
# 막힌곳은 못지나감
if not visited[nv][nx][ny] == 0: continue
if graph[nv][nx][ny] == '.' or graph[nv][nx][ny] == 'E':
q.append((nv,nx,ny))
visited[nv][nx][ny] = visited[v][x][y] + 1
return -1
while True :
L, R, C= map(int, input().split())
if L == 0 and R == 0 and C==0 : break
graph = [[] * R for _ in range(L)]
for i in range(L):
for _ in range(R):
graph[i].append(list(map(str,input().strip())))
input()
visited = [[[0 for _ in range(C)] for _ in range(R)] for _ in range(L)]
for v in range(L):
for i in range(R):
for j in range(C):
if graph[v][i][j] == 'S':
start_l, start_x, start_y = v, i, j
if graph[v][i][j] == 'E':
end_l, end_x, end_y = v, i, j
if Bfs(start_l, start_x, start_y) == -1:
print("Trapped!")
else :
print("Escaped in " + str(visited[end_l][end_x][end_y]-1) + " minute(s).")