From e83e6d26a2a0a72976e1adc68d0ef06515c0c935 Mon Sep 17 00:00:00 2001 From: Minjeong Kim <101111603+Mingguriguri@users.noreply.github.com> Date: Sat, 24 May 2025 21:35:52 +0900 Subject: [PATCH] =?UTF-8?q?feat:=205=EC=9B=94=203=EC=A3=BC=EC=B0=A8=20?= =?UTF-8?q?=EA=B3=BC=EC=A0=9C=20=EB=AC=B8=EC=A0=9C=20=ED=92=80=EC=9D=B4=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...9_\355\206\240\353\247\210\355\206\240.py" | 124 +++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-) diff --git "a/_WeeklyChallenges/W23-[BFS]/Assignment_BOJ_7569_\355\206\240\353\247\210\355\206\240.py" "b/_WeeklyChallenges/W23-[BFS]/Assignment_BOJ_7569_\355\206\240\353\247\210\355\206\240.py" index 693231d..8f8e863 100644 --- "a/_WeeklyChallenges/W23-[BFS]/Assignment_BOJ_7569_\355\206\240\353\247\210\355\206\240.py" +++ "b/_WeeklyChallenges/W23-[BFS]/Assignment_BOJ_7569_\355\206\240\353\247\210\355\206\240.py" @@ -3,4 +3,126 @@ https://www.acmicpc.net/problem/7569 유형: Graph, BFS """ -# PR 올릴 때 공개 예정 + +""" +풀이1 +""" +import sys +from collections import deque +input = sys.stdin.readline + +# 1. 입력 처리 +M, N, H = map(int, input().split()) # 가로, 세로, 높이 +box = [[list(map(int, input().split())) for _ in range(N)] for _ in range(H)] + +# 2. 초기 변수 설정 +queue = deque([]) +directions = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0), + (0, 0, 1), (0, 0, -1)] # 위-오른쪽-아래-왼쪽-앞-뒤 +day = 0 # 정답으로 반환할 변수 + +# 3. 초기 익은 토마토를 큐에 추가하기 +for i in range(H): + for j in range(N): + for k in range(M): + if box[i][j][k] == 1: + queue.append((i, j, k)) + +# 4. BFS 탐색 +while queue: + z, y, x = queue.popleft() + + for dx, dy, dz in directions: + nx, ny, nz = x + dx, y + dy, z + dz + # 범위 내에 있고 아직 안 익은 토마토라면 + if (0 <= nx < M and 0 <= ny < N and 0 <= nz < H) and (box[nz][ny][nx] == 0): + box[nz][ny][nx] += box[z][y][x] + 1 # 익은 날짜 누적 갱신 + queue.append((nz, ny, nx)) + +# 5. 정답 구하기 +for height in box: + for row in height: + for tomato in row: + # 안 익은 토마토가 남아있는지 여부 확인 + if tomato == 0: + print(-1) + exit() + # 익는데 걸린 최대 일수 추적 + day = max(day, max(row)) + +# 6. 정답 출력 +print(day - 1) + + +""" +풀이2 +""" +import sys +from collections import deque + +input = sys.stdin.readline + +# 상자의 가로 m, 세로 n, 높이 h +m, n, h = map(int, input().split()) +tomatoes = [] + +for _ in range(h): + layer = [list(map(int, input().split())) for _ in range(n)] + tomatoes.append(layer) + +directions = [(0, 0, 1), (0, 0, -1), (0, -1, 0), (0, 1, 0), (1, 0, 0), (-1, 0, 0)] # 상하좌우앞뒤 + +def bfs(): + + queue = deque() # (층, 행, 열, 일수) + + # 초기 익은 토마토 위치 큐에 추가 + for z in range(h): + for x in range(n): + for y in range(m): + if tomatoes[z][x][y] == 1: + queue.append((z, x, y, 0)) # (층, 행, 열, 일수) + + max_day = 0 # 익는 데 걸린 최대 일수 추적 + + while queue: + z, x, y, day = queue.popleft() + max_day = max(max_day, day) # 가장 오래 걸린 일수 갱신 + + for dz, dx, dy in directions: + nz, nx, ny = z + dz, x + dx, y + dy + if 0 <= nz < h and 0 <= nx < n and 0 <= ny < m: + if tomatoes[nz][nx][ny] == 0: # 익지 않은 토마토일 때 + tomatoes[nz][nx][ny] = 1 + queue.append((nz, nx, ny, day + 1)) # 익는 데 하루 추가 + + + return max_day + +def all_ripe(): + # 모든 토마토가 익었는지 확인 + for i in range(h): + for j in range(n): + for k in range(m): + if tomatoes[i][j][k] == 0: + return False + return True + +''' +1: 익은 토마토 +0: 익지 않은 토마토 +-1: 토마토가 없음 +''' +# 초기 상태 확인 +if all_ripe(): + print(0) + exit(0) +else: + days = bfs() + + # 모든 토마토가 익었는지 다시 확인 + if all_ripe(): + print(days) + else: + print(-1) +