https://www.acmicpc.net/problem/7569
7569๋ฒ: ํ ๋งํ
์ฒซ ์ค์๋ ์์์ ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ ๋ ์ ์ M,N๊ณผ ์์์ฌ๋ ค์ง๋ ์์์ ์๋ฅผ ๋ํ๋ด๋ H๊ฐ ์ฃผ์ด์ง๋ค. M์ ์์์ ๊ฐ๋ก ์นธ์ ์, N์ ์์์ ์ธ๋ก ์นธ์ ์๋ฅผ ๋ํ๋ธ๋ค. ๋จ, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net
7576๋ฒ ๋ฌธ์ ์ 3์ฐจ์ ๋ฒ์ ์ด๋ค
3์ฐจ์์ด๋ผ๋ ๊ฒ๋ง ์ ๊ฒฝ ์จ์ฃผ๊ณ ๋๊ฐ์ด ํ๋ฉด ๋๋ค
pypy3๋ก ์ ์ถํจ
from collections import deque
m, n, h = map(int, input().split())
tomato = [[] for _ in range(h)]
for i in range(h):
for j in range(n):
data = list(map(int, input().split()))
tomato[i].append(data)
queue = deque()
for i in range(h):
for j in range(n):
for k in range(m):
if tomato[i][j][k] == 1:
queue.append([i, j, k])
def bfs():
dx = [1, -1, 0, 0, 0, 0]
dy = [0, 0, 1, -1, 0, 0]
dz = [0, 0, 0, 0, 1, -1]
while queue:
z, x, y = queue.popleft()
for i in range(6):
mx = x + dx[i]
my = y + dy[i]
mz = z + dz[i]
if mx < 0 or mx >= n or my < 0 or my >= m or mz < 0 or mz >= h:
continue
if tomato[mz][mx][my] == 0:
queue.append([mz, mx, my])
tomato[mz][mx][my] = tomato[z][x][y] + 1
bfs()
res = 0
for i in range(h):
for j in range(n):
for k in range(m):
if tomato[i][j][k] == 0:
print(-1)
exit(0)
res = max(res, max(tomato[i][j]))
print(res-1)
'๐ค > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 2606: ๋ฒฝ ๋ถ์๊ณ ์ด๋ํ๊ธฐ (Python) (0) | 2022.02.03 |
---|---|
๋ฐฑ์ค 1697: ์จ๋ฐ๊ผญ์ง (Python) (2) | 2022.02.02 |
๋ฐฑ์ค 7576: ํ ๋งํ (Python) (0) | 2022.02.02 |
๋ฐฑ์ค 2178: ๋ฏธ๋ก ํ์ (Python) (0) | 2022.02.01 |
๋ฐฑ์ค 1012: ์ ๊ธฐ๋ ๋ฐฐ์ถ (Python) (0) | 2022.01.31 |