-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBJ2667.py
37 lines (31 loc) · 933 Bytes
/
BJ2667.py
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
# baekjoon 단지번호붙이기 (실버1)
import sys
from collections import deque
def input():
return sys.stdin.readline().rstrip()
N = int(input())
matrix = [list(map(int, list(input()))) for _ in range(N)]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
count = 0
buildings = []
for i in range(N):
for j in range(N):
if matrix[i][j] == 1:
queue = deque([[i, j]])
matrix[i][j] = 0
count += 1
building = 1
while queue:
x, y = queue.popleft()
for k in range(4):
xx = x+dx[k]
yy = y+dy[k]
if 0<=xx<N and 0<=yy<N and matrix[xx][yy] == 1:
building += 1
queue.append([xx, yy])
matrix[xx][yy] = 0
buildings.append(building)
print(count)
for i in sorted(buildings):
print(i)