-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ7576.py
More file actions
62 lines (51 loc) · 1.35 KB
/
BJ7576.py
File metadata and controls
62 lines (51 loc) · 1.35 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from sys import stdin
from collections import deque
NM = stdin.readline().split(' ')
M = int(NM[0])
N = int(NM[1])
arr = []
tempArr = []
queue = deque([])
count = -1
# [x좌표, y좌표, count]
# x좌표가 세로 N, y좌표가 가로 M
# 2차원 배열 형성하면서 값에 1 있는 경우 큐에 넣기
for i in range(N):
user = stdin.readline()
tempArr = user.split(' ')
for j in range(M):
tempArr[j] = int(tempArr[j])
if tempArr[j] == 1:
queue.append([i, j, count])
arr.append(tempArr)
tempArr = []
while len(queue) > 0:
x = queue[0][0]
y = queue[0][1]
count = queue[0][2]
count += 1
queue.popleft()
# 좌우위아래 검사해서 0인 경우, 1로 바꾸고 큐에 넣기
if x < N - 1 and arr[x + 1][y] == 0:
queue.append([x + 1, y, count])
arr[x + 1][y] = 1
if x > 0 and arr[x - 1][y] == 0:
queue.append([x - 1, y, count])
arr[x - 1][y] = 1
if y < M - 1 and arr[x][y + 1] == 0:
queue.append([x, y + 1, count])
arr[x][y + 1] = 1
if y > 0 and arr[x][y - 1] == 0:
queue.append([x, y - 1, count])
arr[x][y - 1] = 1
noZero = 0
# 0 없는지 검사
for i in range(N):
if 0 not in arr[i]:
noZero += 1
# 0 없으면 count 출력
if noZero == N:
print(count)
# 0 있으면 -1 출력
else:
print(-1)