728x90
6593번: 상범 빌딩
당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어
www.acmicpc.net
🤔 문제분석
3차원 배열에서 너비우선탐색을 이용하면 된다, 가로,세로,높이를 입력받고 갈수 있는 방향은 6가지로 모든 경우의수를 너비우선탐색으로 방문해가면서 도달 할 수 있는지 체크하면 된다.
💻 코드
import sys
from collections import deque
input = sys.stdin.readline
moves = [(0, 0, 1), (0, 1, 0), (1, 0, 0),
(0, 0, -1), (0, -1, 0), (-1, 0, 0)]
def is_range(i, j, k):
return L > i >=0 and R > j >=0 and C > k >=0
while True:
L, R, C = map(int, input().split())
if L == 0 and R == 0 and C == 0:
break
s_h, s_y, s_x = 0, 0, 0
e_h, e_y, e_x = 0, 0, 0
table = [[] for _ in range(L)]
for i in range(L):
for j in range(R):
temp = list(str(input().strip()))
for k in range(C):
if temp[k] == 'S':
s_h, s_y, s_x = i, j, k
elif temp[k] == 'E':
e_h, e_y, e_x = i, j, k
table[i].append(temp)
input()
visited = [[[False for _ in range(C)] for _ in range(R)] for _ in range(L)]
visited[s_h][s_y][s_x] = True
queue = deque()
queue.append((s_h, s_y, s_x, 0))
is_ended = False
while queue:
h, y, x, cost = queue.popleft()
if h == e_h and y == e_y and x == e_x:
print("Escaped in {} minute(s).".format(cost))
is_ended = True
break
for dh, dy, dx in moves:
nh, ny, nx = dh + h, dy + y, dx + x
if is_range(nh, ny, nx):
v = not visited[nh][ny][nx]
t = table[nh][ny][nx] == '.' or table[nh][ny][nx] == 'E'
if v and t:
visited[nh][ny][nx] = True
queue.append((nh, ny, nx, cost + 1))
if not is_ended:
print("Trapped!")
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 4386번 : 별자리 만들기 (0) | 2024.03.18 |
---|---|
[백준] 1963번 : 소수경로 (0) | 2024.03.18 |
[백준] 2458번 : 키 순서 (1) | 2024.03.16 |
[백준] 1865번 : 웜홀 (0) | 2024.03.10 |
[백준] 1613번 : 역사 (0) | 2024.03.10 |