-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7576.py
More file actions
33 lines (31 loc) · 773 Bytes
/
7576.py
File metadata and controls
33 lines (31 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from collections import deque
import sys
input = sys.stdin.readline
dy = [0, 0, -1, 1]
dx = [-1, 1, 0, 0]
# 열, 행
m, n = map(int, input().split())
board = []
dq = deque()
for i in range(n):
row = list(map(int, input().split()))
board.append(row)
for j in range(m):
if row[j] == 1:
dq.append((i, j, 0))
answer = 0
while dq:
y, x, cnt = dq.popleft()
answer = max(answer, cnt)
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
if 0 <= ny < n and 0 <= nx < m and board[ny][nx] == 0:
dq.append((ny, nx, cnt + 1))
board[ny][nx] = 1
for i in range(n):
for j in range(m):
if board[i][j] == 0: # 0이 있다면 실패
print(-1)
exit()
print(answer)