๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 6593๋ฒ
ํ์ด ๊ณผ์
์์ ์ง์ ์์ ๋ชฉํ ์ง์ ์ ๋๋ฌํ๊ธฐ ์ํ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ BFS
๋ฌธ์ ์
๋๋ค.
๋น๋ฉ์ด 3์ฐจ์์ด๊ธฐ ๋๋ฌธ์ ์ํ๊ณต๊ฐ์ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํด์ค๋๋ค.
visit[z][x][y] = z ์ธต์ (x, y) ์ง์ ์ ๋ฐฉ๋ฌธํ๋์ง ์ ๋ฌด๋ฅผ ์ ์ฅ
์ฌ๊ธฐ์ z ์ถ
์ ์ฒซ๋ฒ์งธ ์ธ๋ฑ์ค๋ก ์ง์ ํ ๊ฒ์ ๊ตฌํ์์ ํธ์๋ฅผ ์ํ ๊ฒ์
๋๋ค.
๊ฐ ์ง์ ๋ค์ ๋ฐฉ๋ฌธํ ๋ ๋์๋จ๋ถ
๋ฐ ์์ธต, ์๋์ธต
๊น์ง ๊ฐ์ด ํ์์ ์ํํ๋ฉฐ ๋๋ฌ ์ฌ๋ถ๋ฅผ ํ๋จํด์ฃผ๋ฉด ๋ฉ๋๋ค.
์ฝ๋
import sys
from collections import deque
L, R, C = 0, 0, 0
building = []
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
dz = [1, -1]
def bfs(start):
q = deque()
visit = [[[0] * C for _ in range(R)] for _ in range(L)]
q.append([start[0], start[1], start[2], 0])
visit[start[0]][start[1]][start[2]] = 1
while q:
z, x, y, cost = q.popleft()
if building[z][x][y] == 'E':
return cost
# ๋์๋จ๋ถ ์ด๋
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < R and 0 <= ny < C:
if not visit[z][nx][ny] and building[z][nx][ny] != '#':
visit[z][nx][ny] = 1
q.append([z, nx, ny, cost + 1])
# ์ํ ์ด๋
for i in range(2):
nz = z + dz[i]
if 0 <= nz < L:
if not visit[nz][x][y] and building[nz][x][y] != '#':
visit[nz][x][y] = 1
q.append([nz, x, y, cost + 1])
return -1
def solution():
start = []
# ์์์ง์ ์ฐพ๊ธฐ
for z in range(L):
for x in range(R):
for y in range(C):
if building[z][x][y] == 'S':
start = [z, x, y]
return bfs(start)
if __name__ == '__main__':
while True:
L, R, C = list(map(int, sys.stdin.readline().split()))
if not L and not R and not C:
break
building = []
for _ in range(L):
building.append([list(sys.stdin.readline().strip()) for _ in range(R)])
sys.stdin.readline()
ans = solution()
if ans != -1:
print(f'Escaped in {ans} minute(s).')
else:
print('Trapped!')
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 15732 - ๋ํ ๋ฆฌ ์จ๊ธฐ๊ธฐ (0) | 2021.03.18 |
---|---|
BOJ 2178 - ๋ฏธ๋ก ํ์ (0) | 2021.03.18 |
BOJ 16397 - ํ์ถ (0) | 2021.03.18 |
BOJ 2563 - ์์ข ์ด (0) | 2021.03.18 |
BOJ 1963 - ์์ ๊ฒฝ๋ก (0) | 2021.03.18 |
๐ฌ ๋๊ธ