[Python] BOJ 7569 - 토마토(BFS)
728x90
반응형
import sys
from collections import deque
input = sys.stdin.readline
q = deque()
M, N, H = map(int,input().split())
dx, dy, dz = [1,-1,0,0,0,0], [0,0,-1,1,0,0], [0,0,0,0,-1,1]
m = [[list(map(int,input().split())) for _ in range(N)] for _ in range(H)]
visited = [[[0 for _ in range(M)] for _ in range(N)] for _ in range(H)]
def bfs():
while q:
z, x, y = q.popleft()
visited[z][x][y] = 1
for i in range(6):
nx, ny, nz = x + dx[i], y + dy[i], z + dz[i]
if 0 <= nx < N and 0 <= ny < M and 0 <= nz < H and m[nz][nx][ny] == 0 and not visited[nz][nx][ny]:
m[nz][nx][ny] = m[z][x][y] + 1
visited[nz][nx][ny] = 1
q.append([nz,nx,ny])
for i in range(N):
for j in range(M):
for k in range(H):
if m[k][i][j] == 1:
q.append([k,i,j])
bfs()
time = 0
tf = False
for i in range(N):
for j in range(M):
for k in range(H):
if m[k][i][j] == 0:
tf = True
time = max(time, m[k][i][j])
if tf:
print(-1)
else:
print(time-1)
728x90
반응형
'문제풀이 > BaekjoonOnlineJudge' 카테고리의 다른 글
[Python] BOJ 5014 - Elevator Trouble(스타트링크) (0) | 2021.03.31 |
---|---|
[Python] BOJ 6593 - Dungeon Master(상범 빌딩) (0) | 2021.03.31 |
[Python] BOJ 21316 - 스피카 (0) | 2021.03.28 |
[Python] BOJ 21318 - 피아노 체조 (0) | 2021.03.28 |
[Python] BOJ 18291 - 비요뜨의 징검다리 건너기 (0) | 2021.03.24 |
TAGS.