1. 문제
https://www.acmicpc.net/problem/7569
7569번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net
2. 풀이
# 7569번 : 토마토
# 입력 코드
import sys
input = sys.stdin.readline
from collections import deque
# m : 상자 가로 칸 수
# n : 상자 세로 칸 수
# h : 쌓는 상자의 수
m, n, h = map(int, input().split())
# boxList : 박스에 담긴 토마토의 정보 배열
boxList = [[list(map(int, input().split()))for j in range(n)]for i in range(h)]
# 최초 입력받은 토마토 정보 중 익은 토마토의 좌표를 찾는 코드
q = deque()
for i in range(h):
for j in range(n):
for k in range(m):
if boxList[i][j][k] == 1:
q.append([i, j, k])
# 익은 토마토가 안익은 토마토를 익히는 코드
cnt = 0
while q:
new_q = deque()
cnt += 1
while q:
x, y, z = map(int, q.popleft())
if x - 1 >= 0 and boxList[x-1][y][z] == 0:
boxList[x-1][y][z] = 1
new_q.append([x-1, y, z])
if x + 1 < h and boxList[x+1][y][z] == 0:
boxList[x+1][y][z] = 1
new_q.append([x+1, y, z])
if y - 1 >= 0 and boxList[x][y-1][z] == 0:
boxList[x][y-1][z] = 1
new_q.append([x, y-1, z])
if y + 1 < n and boxList[x][y+1][z] == 0:
boxList[x][y+1][z] = 1
new_q.append([x, y+1, z])
if z - 1 >= 0 and boxList[x][y][z-1] == 0:
boxList[x][y][z-1] = 1
new_q.append([x, y, z-1])
if z + 1 < m and boxList[x][y][z+1] == 0:
boxList[x][y][z+1] = 1
new_q.append([x, y, z+1])
q = new_q
containZero = False
for i in boxList:
for j in i:
if 0 in j:
containZero = True
break
if containZero: break
if containZero:
print(-1)
else:
print(cnt - 1)
'알고리즘 공부 > 백준' 카테고리의 다른 글
(Python)백준 코딩테스트 연습 - 집합의 표현(1717) (0) | 2023.08.07 |
---|---|
(Python)백준 코딩테스트 연습 - 최소비용 구하기(1916) (0) | 2023.07.31 |
(Python)백준 코딩테스트 연습 - 트리(1068) (0) | 2023.07.22 |
(Python)백준 코딩테스트 연습 - 평범한 배낭(12865) (0) | 2023.07.16 |
(Python)백준 코딩테스트 연습 - 토마토(7576) (0) | 2023.07.15 |