|
3 | 3 | https://www.acmicpc.net/problem/7569 |
4 | 4 | ์ ํ: Graph, BFS |
5 | 5 | """ |
6 | | -# PR ์ฌ๋ฆด ๋ ๊ณต๊ฐ ์์ |
| 6 | + |
| 7 | +""" |
| 8 | +ํ์ด1 |
| 9 | +""" |
| 10 | +import sys |
| 11 | +from collections import deque |
| 12 | +input = sys.stdin.readline |
| 13 | + |
| 14 | +# 1. ์
๋ ฅ ์ฒ๋ฆฌ |
| 15 | +M, N, H = map(int, input().split()) # ๊ฐ๋ก, ์ธ๋ก, ๋์ด |
| 16 | +box = [[list(map(int, input().split())) for _ in range(N)] for _ in range(H)] |
| 17 | + |
| 18 | +# 2. ์ด๊ธฐ ๋ณ์ ์ค์ |
| 19 | +queue = deque([]) |
| 20 | +directions = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0), |
| 21 | + (0, 0, 1), (0, 0, -1)] # ์-์ค๋ฅธ์ชฝ-์๋-์ผ์ชฝ-์-๋ค |
| 22 | +day = 0 # ์ ๋ต์ผ๋ก ๋ฐํํ ๋ณ์ |
| 23 | + |
| 24 | +# 3. ์ด๊ธฐ ์ต์ ํ ๋งํ ๋ฅผ ํ์ ์ถ๊ฐํ๊ธฐ |
| 25 | +for i in range(H): |
| 26 | + for j in range(N): |
| 27 | + for k in range(M): |
| 28 | + if box[i][j][k] == 1: |
| 29 | + queue.append((i, j, k)) |
| 30 | + |
| 31 | +# 4. BFS ํ์ |
| 32 | +while queue: |
| 33 | + z, y, x = queue.popleft() |
| 34 | + |
| 35 | + for dx, dy, dz in directions: |
| 36 | + nx, ny, nz = x + dx, y + dy, z + dz |
| 37 | + # ๋ฒ์ ๋ด์ ์๊ณ ์์ง ์ ์ต์ ํ ๋งํ ๋ผ๋ฉด |
| 38 | + if (0 <= nx < M and 0 <= ny < N and 0 <= nz < H) and (box[nz][ny][nx] == 0): |
| 39 | + box[nz][ny][nx] += box[z][y][x] + 1 # ์ต์ ๋ ์ง ๋์ ๊ฐฑ์ |
| 40 | + queue.append((nz, ny, nx)) |
| 41 | + |
| 42 | +# 5. ์ ๋ต ๊ตฌํ๊ธฐ |
| 43 | +for height in box: |
| 44 | + for row in height: |
| 45 | + for tomato in row: |
| 46 | + # ์ ์ต์ ํ ๋งํ ๊ฐ ๋จ์์๋์ง ์ฌ๋ถ ํ์ธ |
| 47 | + if tomato == 0: |
| 48 | + print(-1) |
| 49 | + exit() |
| 50 | + # ์ต๋๋ฐ ๊ฑธ๋ฆฐ ์ต๋ ์ผ์ ์ถ์ |
| 51 | + day = max(day, max(row)) |
| 52 | + |
| 53 | +# 6. ์ ๋ต ์ถ๋ ฅ |
| 54 | +print(day - 1) |
| 55 | + |
| 56 | + |
| 57 | +""" |
| 58 | +ํ์ด2 |
| 59 | +""" |
| 60 | +import sys |
| 61 | +from collections import deque |
| 62 | + |
| 63 | +input = sys.stdin.readline |
| 64 | + |
| 65 | +# ์์์ ๊ฐ๋ก m, ์ธ๋ก n, ๋์ด h |
| 66 | +m, n, h = map(int, input().split()) |
| 67 | +tomatoes = [] |
| 68 | + |
| 69 | +for _ in range(h): |
| 70 | + layer = [list(map(int, input().split())) for _ in range(n)] |
| 71 | + tomatoes.append(layer) |
| 72 | + |
| 73 | +directions = [(0, 0, 1), (0, 0, -1), (0, -1, 0), (0, 1, 0), (1, 0, 0), (-1, 0, 0)] # ์ํ์ข์ฐ์๋ค |
| 74 | + |
| 75 | +def bfs(): |
| 76 | + |
| 77 | + queue = deque() # (์ธต, ํ, ์ด, ์ผ์) |
| 78 | + |
| 79 | + # ์ด๊ธฐ ์ต์ ํ ๋งํ ์์น ํ์ ์ถ๊ฐ |
| 80 | + for z in range(h): |
| 81 | + for x in range(n): |
| 82 | + for y in range(m): |
| 83 | + if tomatoes[z][x][y] == 1: |
| 84 | + queue.append((z, x, y, 0)) # (์ธต, ํ, ์ด, ์ผ์) |
| 85 | + |
| 86 | + max_day = 0 # ์ต๋ ๋ฐ ๊ฑธ๋ฆฐ ์ต๋ ์ผ์ ์ถ์ |
| 87 | + |
| 88 | + while queue: |
| 89 | + z, x, y, day = queue.popleft() |
| 90 | + max_day = max(max_day, day) # ๊ฐ์ฅ ์ค๋ ๊ฑธ๋ฆฐ ์ผ์ ๊ฐฑ์ |
| 91 | + |
| 92 | + for dz, dx, dy in directions: |
| 93 | + nz, nx, ny = z + dz, x + dx, y + dy |
| 94 | + if 0 <= nz < h and 0 <= nx < n and 0 <= ny < m: |
| 95 | + if tomatoes[nz][nx][ny] == 0: # ์ต์ง ์์ ํ ๋งํ ์ผ ๋ |
| 96 | + tomatoes[nz][nx][ny] = 1 |
| 97 | + queue.append((nz, nx, ny, day + 1)) # ์ต๋ ๋ฐ ํ๋ฃจ ์ถ๊ฐ |
| 98 | + |
| 99 | + |
| 100 | + return max_day |
| 101 | + |
| 102 | +def all_ripe(): |
| 103 | + # ๋ชจ๋ ํ ๋งํ ๊ฐ ์ต์๋์ง ํ์ธ |
| 104 | + for i in range(h): |
| 105 | + for j in range(n): |
| 106 | + for k in range(m): |
| 107 | + if tomatoes[i][j][k] == 0: |
| 108 | + return False |
| 109 | + return True |
| 110 | + |
| 111 | +''' |
| 112 | +1: ์ต์ ํ ๋งํ |
| 113 | +0: ์ต์ง ์์ ํ ๋งํ |
| 114 | +-1: ํ ๋งํ ๊ฐ ์์ |
| 115 | +''' |
| 116 | +# ์ด๊ธฐ ์ํ ํ์ธ |
| 117 | +if all_ripe(): |
| 118 | + print(0) |
| 119 | + exit(0) |
| 120 | +else: |
| 121 | + days = bfs() |
| 122 | + |
| 123 | + # ๋ชจ๋ ํ ๋งํ ๊ฐ ์ต์๋์ง ๋ค์ ํ์ธ |
| 124 | + if all_ripe(): |
| 125 | + print(days) |
| 126 | + else: |
| 127 | + print(-1) |
| 128 | + |
0 commit comments