728x90
반응형
https://www.acmicpc.net/problem/6593
6593번: 상범 빌딩
당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어
www.acmicpc.net
문제해결
- BFS의 전형적인 문제이지만 3차원으로 확장했다는 것이 다르다.
CODE
from collections import deque
import sys
input = sys.stdin.readline
INF = sys.maxsize
def bfs(z,x,y):
global answer
que = deque()
que.append((z,x,y,0))
visited = [[[-1 for _ in range(c)] for _ in range(r)] for _ in range(l)]
visited[z][x][y] = 1
dxs = [-1,1,0,0,0,0]
dys = [0,0,-1,1,0,0]
dzs = [0,0,0,0,-1,1]
while que:
mz, mx, my, cnt = que.popleft()
for dz,dx,dy in zip(dzs,dxs,dys):
nz = mz + dz
nx = mx + dx
ny = my + dy
if not(0<=nz<l and 0<=nx<r and 0<=ny<c):continue
elif building[nz][nx][ny] == 'E':
answer = min(answer,cnt+1)
elif visited[nz][nx][ny] == -1 and building[nz][nx][ny]=='.':
visited[nz][nx][ny] = 1
que.append((nz,nx,ny,cnt+1))
while True:
l, r, c= map(int, input().split())
if l==0 and r==0 and c ==0:break
building = []
answer = INF
for h in range(l):
floor = []
for i in range(r):
row = [str(x) for x in input().rstrip()]
floor.append(row)
building.append(floor)
input()
for h in range(l):
for i in range(r):
for j in range(c):
if building[h][i][j] =='S':
bfs(h,i,j)
print(f'Escaped in {answer} minute(s).' if answer != INF else 'Trapped!')
728x90
반응형
'알고리즘 > [python] 백준 BOJ' 카테고리의 다른 글
[python] 백준 18185 라면 사기 (small) (0) | 2023.04.29 |
---|---|
[python] 백준 4716 풍선 (1) | 2023.04.28 |
[python] 백준 17471 게리맨더링 (0) | 2023.04.26 |
[python] 백준 1477 휴게소 세우기 (0) | 2023.04.25 |
[python] 백준 7453 합이 0인 네 정수 (0) | 2023.04.25 |
댓글