아싸 ~ 3차원 BFS ..!
아 인덱스 때문에 머리 꼬여서 고생했는데 하루 자고 풀어보니까 풀린다 ㅎㅎㅎㅎㅎㅎ 행복해 ~ 재밌어 코테 ㅎㅎㅎㅎ
그 .. 개인적으로 3차원에서 z 인덱스를 맨 앞으로 뺀다는 부분에서 헷갈리지 말아야 한다.
import sys
input = sys.stdin.readline
from collections import deque
# 동 서 남 북 위 아래
dx = [-1, 0, 1, 0, 0, 0]
dy = [0, 1, 0, -1, 0, 0]
dz = [0, 0, 0, 0, -1, 1]
def bfs():
q = deque([(sz, sx, sy)])
building[sz][sx][sy] = 0
while q:
z, x, y = q.popleft()
for i in range(6):
nx = x + dx[i]
ny = y + dy[i]
nz = z + dz[i]
if 0 <= nz < l and 0 <= nx < r and 0 <= ny < c:
if building[nz][nx][ny] == '.' or building[nz][nx][ny] == 'E':
building[nz][nx][ny] = building[z][x][y] + 1
q.append((nz, nx, ny))
while True:
l, r, c = map(int, input().split())
if l == 0:
break
building = []
sx, sy, sz = 0, 0, 0
ex, ey, ez = 0, 0, 0
for i in range(l):
floor = []
for j in range(r):
row = list(input().strip())
for k in range(c):
if row[k] == 'S':
sz, sx, sy = i, j, k
elif row[k] == 'E':
ez, ex, ey = i, j, k
floor.append(row)
building.append(floor)
blank = input()
bfs()
if building[ez][ex][ey] == 'E':
print('Trapped!')
else:
print(f'Escaped in {building[ez][ex][ey]} minute(s).')
'CodingTest > Baekjun Online Judge' 카테고리의 다른 글
[ BOJ / 파이썬 ] 11559. Puyo Puyo (0) | 2023.02.13 |
---|---|
[ BOJ / 파이썬 ] 6603. 로또 (0) | 2023.02.13 |
[ BOJ / 파이썬 ] 14501.퇴사 (0) | 2023.02.06 |
[ BOJ / 파이썬 ] 9461. 파도반 수열 (0) | 2023.02.06 |
[ BOJ / 파이썬 ] 코테. 증가하는 부분 수열(LIS) 문제 시리즈 (0) | 2023.02.06 |