-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheese.py
More file actions
55 lines (45 loc) · 1.05 KB
/
cheese.py
File metadata and controls
55 lines (45 loc) · 1.05 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
'''
치즈
'''
import sys
from collections import deque
h, w = map(int, sys.stdin.readline().split())
array = []
for _ in range(h):
array.append(list(map(int, sys.stdin.readline().split())))
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
queue = deque()
remain = 0
count = 0
cheese = 0
for i in range(h):
for j in range(w):
if array[i][j] == 1:
remain += 1
def delete():
for i in range(h):
for j in range(w):
if array[i][j] == 2:
array[i][j] = 0
while remain != 0:
visited = [[0 for _ in range(w)] for _ in range(h)]
queue.append([0, 0])
visited[0][0] = 1
if remain != 0: cheese = remain
while queue:
current = queue.popleft()
for i in range(4):
ndx = current[0] + dx[i]
ndy = current[1] + dy[i]
if 0 <= ndx < h and 0 <= ndy < w and visited[ndx][ndy] == 0:
visited[ndx][ndy] = 1
if array[ndx][ndy] == 1:
array[ndx][ndy] = 2
remain -= 1
else:
queue.append([ndx, ndy])
count += 1
delete()
print(count)
print(cheese)