-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2667.py
More file actions
49 lines (42 loc) · 1 KB
/
2667.py
File metadata and controls
49 lines (42 loc) · 1 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
#solved.ac
#실버2
#DFS&BFS
#단지 번호 붙이기
#2667
import sys
from collections import deque
input = sys.stdin.readline
moves = [(1,0),(0,1),(-1,0),(0,-1)]
def bfs(row,col,N):
cnt = 0
#시�� 좌표 큐에 넣어줌
q = deque([(row,col)])
visited[row][col]=True
while q:
row,col = q.popleft()
cnt+=1
for move in moves:
r = row+move[0]
c = col+move[1]
if 0<=r<=N-1 and 0<=c<=N-1:
if maps[r][c]==1 and not visited[r][c]:
q.append((r,c))
visited[r][c]=True
return cnt
N = int(input())
visited=[[False]*N for i in range(N)]
maps = []
cntList = []
#입력
for i in range(N):
maps.append(list(map(int,list(input().rstrip()))))
#bfs 방문
for i in range(N):
for j in range(N):
if not visited[i][j] and maps[i][j]:
cnt = bfs(i,j,N)
cntList.append(cnt)
cntList.sort()
print(len(cntList))
for cnt in cntList:
print(cnt)