[백준/파이썬] 6593번

민정·2024년 1월 16일
0

[백준/파이썬]

목록 보기
236/245
post-thumbnail

📍백준 6593 문제

https://www.acmicpc.net/problem/6593

코드

import sys
from collections import deque
input = sys.stdin.readline

dx = [1, -1, 0, 0, 0, 0]
dy = [0, 0, 1, -1, 0, 0]
dz = [0, 0, 0, 0, -1, 1]


def bfs(x, y, z, e1, e2, e3):
    que = deque()
    que.append((x, y, z))
    visited[x][y][z] = 0
    while que:
        x, y, z = que.popleft()
        if x == e1 and y == e2 and z == e3:
            return visited[x][y][z]
        for i in range(6):
            nx = x + dx[i]
            ny = y + dy[i]
            nz = z + dz[i]
            if 0 <= nx < s and 0 <= ny < n and 0 <= nz < m:
                if visited[nx][ny][nz] == -1 and maps[nx][ny][nz] == '.' or maps[nx][ny][nz] == 'E':
                    que.append((nx, ny, nz))
                    visited[nx][ny][nz] = visited[x][y][z] + 1
    return -1


while True:
    s, n, m = map(int, input().split())
    if s == 0 and n == 0 and m == 0:
        break
    visited = [[[-1 for _ in range(m)]for _ in range(n)]for _ in range(s)]
    maps = []
    for _ in range(s):
        temp1 = []
        for i in range(n+1):
            temp2 = list(input().rstrip('\n'))
            if i != n:
                temp1.append(temp2)
        maps.append(temp1)

    for i in range(s):
        for j in range(n):
            for k in range(m):
                if maps[i][j][k] == 'S':
                    x, y, z = i, j, k
                elif maps[i][j][k] == 'E':
                    e1, e2, e3 = i, j, k

    time = bfs(x, y, z, e1, e2, e3)
    if time == -1:
        print("Trapped!")
    else:
        print("Escaped in", time, "minute(s).")

풀이

토마토 문제(백준 7569번) 와 유사하다.

profile
パㅔバ6ㅇr 덤벼ㄹΓ :-0

0개의 댓글