-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ2667.py
More file actions
65 lines (54 loc) · 1.76 KB
/
BJ2667.py
File metadata and controls
65 lines (54 loc) · 1.76 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
62
63
64
from sys import stdin
from collections import deque
N = int(input())
arr = []
tempArr = []
for i in range(N):
user = input()
for j in range(N):
tempArr.append(int(user[j]))
arr.append(tempArr)
tempArr = []
queue = deque([])
count = 0
countArr = []
visited = []
for i in range(N):
for j in range(N):
if arr[i][j] == 1:
queue.append([i, j])
visited.append([i, j])
count = 1
while len(queue) > 0:
x = queue[0][0]
y = queue[0][1]
pop = queue.popleft()
if x < N - 1:
if arr[x + 1][y] == 1 and [x + 1, y] not in visited:
queue.append([x + 1, y])
visited.append([x + 1, y])
arr[x + 1][y] = 0
count += 1
if x > 0:
if arr[x - 1][y] == 1 and [x - 1, y] not in visited:
queue.append([x - 1, y])
visited.append([x - 1, y])
arr[x - 1][y] = 0
count += 1
if y < N - 1:
if arr[x][y + 1] == 1 and [x, y + 1] not in visited:
queue.append([x, y + 1])
visited.append([x, y + 1])
arr[x][y + 1] = 0
count += 1
if y > 0:
if arr[x][y - 1] == 1 and [x, y - 1] not in visited:
queue.append([x, y - 1])
visited.append([x, y - 1])
arr[x][y - 1] = 0
count += 1
countArr.append(count)
print(len(countArr))
countArr.sort()
for i in countArr:
print(i)