728x90
반응형
https://www.acmicpc.net/problem/6593
최종 코드
import sys
from collections import deque
input = sys.stdin.readline
L, R, C = map(int, input().split())
building = list()
q = deque(list())
dl = [0, 0, 0, 0, 1, -1]
dr = [-1, 0, 1, 0, 0, 0]
dc = [0, -1, 0, 1, 0, 0]
answer = []
while L+R+C > 0:
q.clear()
building.clear()
visit = [[[0] * C for _ in range(R)] for _ in range(L)]
for l in range(L):
floor = list()
for r in range(R):
floor.append(list(input().rstrip()))
for c in range(C):
if floor[r][c] == 'S':
q.append([l, r, c, 0])
floor[r][c] = '.'
visit[l][r][c] = 1
building.append(floor)
input()
while q:
l, r, c, mins = q.popleft()
if building[l][r][c] == 'E':
answer.append("Escaped in %d minute(s)." %mins)
break
for ddl, ddr, ddc in zip(dl, dr, dc):
nl = l + ddl
nr = r + ddr
nc = c + ddc
if 0 <= nl < L and 0 <= nr < R and 0 <= nc < C:
if not visit[nl][nr][nc] and building[nl][nr][nc] != '#':
visit[nl][nr][nc] = 1
q.append([nl, nr, nc, mins+1])
else: answer.append("Trapped!")
L, R, C = map(int, input().split())
for ans in answer:
print(ans)
풀이 과정
풀이 시간 30분
import sys
from collections import deque
input = sys.stdin.readline
L, R, C = map(int, input().split())
#상범 빌딩
building = list()
q = deque(list())
#북, 서, 남, 동, 하, 상
dl = [0, 0, 0, 0, 1, -1]
dr = [-1, 0, 1, 0, 0, 0]
dc = [0, -1, 0, 1, 0, 0]
answer = []
while L+R+C > 0:
#초기화
q.clear()
building.clear()
visit = [[[0] * C for _ in range(R)] for _ in range(L)]
for l in range(L):
floor = list()
for r in range(R):
floor.append(list(input().rstrip()))
for c in range(C):
if floor[r][c] == 'S':
#시작점 위치(l, r, c), 0분 큐에 담는다
q.append([l, r, c, 0])
floor[r][c] = '.'
visit[l][r][c] = 1
building.append(floor)
input()
while q:
l, r, c, mins = q.popleft()
#출구에 도착한 경우,
if building[l][r][c] == 'E':
answer.append("Escaped in %d minute(s)." %mins)
break
#인접한 6 방향 탐색
for ddl, ddr, ddc in zip(dl, dr, dc):
nl = l + ddl
nr = r + ddr
nc = c + ddc
if 0 <= nl < L and 0 <= nr < R and 0 <= nc < C:
#이전에 방문하지 않은 칸이면서 벽이 아닌 경우, 방문처리 후 큐에 담는다
if not visit[nl][nr][nc] and building[nl][nr][nc] != '#':
visit[nl][nr][nc] = 1
q.append([nl, nr, nc, mins+1])
#탈출 할 수 없는 경우,
else: answer.append("Trapped!")
L, R, C = map(int, input().split())
for ans in answer:
print(ans)
#시간복잡도 = O(V+E), 공간복잡도 = O(n)
728x90
반응형
'코테 노트 > 백준' 카테고리의 다른 글
백준 5427 불 Python 3 (0) | 2022.02.07 |
---|---|
백준 9019 DSLR Python 3 (0) | 2022.02.07 |
백준 5014 스타트링크 Python 3 (0) | 2022.02.05 |
백준 7576 토마토 Python 3 (0) | 2022.02.04 |
백준 1697 숨바꼭질 Python 3 (0) | 2022.02.04 |