[๋ฐฑ์ค-7569] ํ ๋งํ / Python
๐ Problem Solving/Baekjoon
from collections import deque
import sys
m, n, h = map(int, sys.stdin.readline().split())
dx = [-1, 1, 0, 0, 0, 0]
dy = [0, 0, -1, 1, 0, 0]
dz = [0, 0, 0, 0, -1, 1]
def bfs():
while queue:
x, y, z = queue.popleft()
for i in range(6):
nx = x + dx[i]
ny = y + dy[i]
nz = z + dz[i]
if 0 <= nx < n and 0 <= ny < m and 0 <= nz < h and graph[nz][nx][ny] == 0:
queue.append([nx, ny, nz])
graph[nz][nx][ny] = graph[z][x][y] + 1
graph = [[] for _ in range(h)]
queue = deque()
isTrue = False
for i in range(h):
for j in range(n):
graph[i].append(list(map(int, sys.stdin.readline().split())))
for z in range(h):
for x in range(n):
for y in range(m):
if graph[z][x][y] == 1:
queue.append([x, y, z])
bfs()
maxValue = 0
for z in range(h):
for x in range(n):
for y in range(m):
if graph[z][x][y] == 0:
isTrue = True
break
maxValue = max(maxValue, graph[z][x][y])
if isTrue == True:
print(-1)
else:
print(maxValue - 1)
ํด์ค
์ด์ ์ ํ์๋ ํ ๋งํ ๋ฌธ์ ์ ๋น์ทํ ์ ํ์ด๋ค. ๋ค๋ง ์ด ๋ฌธ์ ์์๋ ๋์ด๊ฐ ์ถ๊ฐ๋๋ค.
๊ฐ๋ก, ์ธ๋ก, ๋์ด์ ๋ง๊ฒ ์ต์ ํ ๋งํ ๋ฅผ ํ์ธํ๊ณ deque์ ๋ฃ์ด์ bfs๋ฅผ ์งํํ๋ค.
'๐ Problem Solving > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค-1620] ๋๋์ผ ํฌ์ผ๋ชฌ ๋ง์คํฐ ์ด๋ค์ / Python (0) | 2021.05.25 |
---|---|
[๋ฐฑ์ค-1074] Z / Python (0) | 2021.05.25 |
[๋ฐฑ์ค-2468] ์์ ์์ญ / Python (0) | 2021.05.23 |
[๋ฐฑ์ค-2583] ์์ญ ๊ตฌํ๊ธฐ / Python (0) | 2021.05.22 |
[๋ฐฑ์ค-13549] ์จ๋ฐ๊ผญ์ง 3 / Python (0) | 2021.05.21 |