728x90
반응형
문제
https://www.acmicpc.net/problem/7569
문제 풀이
import collections
import sys
M, N, H = map(int, sys.stdin.readline().split())
tomato = [[] for _ in range(H)]
queue = collections.deque([])
for h in range(H):
for y in range(N):
temp = list(map(int, sys.stdin.readline().split()))
tomato[h].append(temp)
for x in range(M):
if temp[x] == 1:
queue.append((h, y, x))
# h, y, x
dpos = [(1, 0, 0), (-1, 0, 0), (0, 0, -1), (0, 0, 1), (0, -1, 0), (0, 1, 0)]
def bfs():
while queue:
hh, yy, xx = queue.popleft()
for d in dpos:
nh = hh + d[0]
ny = yy + d[1]
nx = xx + d[2]
if 0 <= nh < H and 0 <= ny < N and 0 <= nx < M:
if tomato[nh][ny][nx] == 0:
tomato[nh][ny][nx] = tomato[hh][yy][xx] + 1
queue.append((nh, ny, nx))
for h in range(H):
for y in range(N):
for x in range(M):
if tomato[h][y][x] == 1:
bfs()
max_date = 0
for h in range(H):
for y in range(N):
if 0 in tomato[h][y]:
print(-1)
exit()
max_date = max(max(tomato[h][y]), max_date)
print(max_date - 1)
728x90
반응형
'IT > 알고리즘' 카테고리의 다른 글
[백준] 11403 파이썬(python) (0) | 2022.01.08 |
---|---|
[백준] 9372 파이썬(python) (0) | 2022.01.08 |
[Python] 백준 11724 - 연결 요소의 개수 (0) | 2021.11.07 |
[Python] 백준 7576 - 토마토 (0) | 2021.11.06 |
[Python] 백준 1012 - 유기농 배추 (0) | 2021.11.06 |