본문 바로가기

# Coding/# 백준

[백준 / 7569] 토마토 - Python

728x90
반응형

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

 

7569번: 토마토

첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,

www.acmicpc.net

 

<전체 코드>

import sys
from collections import deque

M, N, H = map(int, sys.stdin.readline().split())

box = [[] for _ in range(H)]
tomatos = deque([])
not_tomatos = 0

for i in range(N*H):
    line = list(map(int, sys.stdin.readline().split()))
    line_len = len(line)
    for j in range(line_len):
        if line[j] == 1:
            tomatos.append([i//N, i % N, j])
        elif line[j] == 0:
            not_tomatos += 1
    box[i//N].append(line)
# floor, row, col

d_height = [1, -1, 0, 0, 0, 0]
d_row = [0, 0, -1, 0, 1, 0]
d_col = [0, 0, 0, 1, 0, -1]
day = 0
while tomatos and not_tomatos != 0:
    day += 1
    len_tomatos = len(tomatos)
    for _ in range(len_tomatos):
        height, row, col = tomatos.popleft()

        for i in range(6):
            next_height = height+d_height[i]
            if not 0 <= next_height < H:
                continue
            next_row = row+d_row[i]
            if not 0 <= next_row < N:
                continue
            next_col = col+d_col[i]
            if not 0 <= next_col < M:
                continue

            if box[next_height][next_row][next_col] == 0:
                box[next_height][next_row][next_col] = 1
                not_tomatos -= 1
                tomatos.append([next_height, next_row, next_col])

print(-1 if not_tomatos != 0 else day)
728x90
반응형