File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ # https://www.acmicpc.net/problem/2667
2+
3+ import sys
4+ from collections import deque
5+ input = sys .stdin .readline
6+
7+ N = int (input ())
8+
9+ graph = []
10+
11+ for _ in range (N ):
12+ graph .append (list (map (int , input ().strip ())))
13+
14+ visited = [[False ] * N for _ in range (N )]
15+
16+ # 상하좌우
17+ dx = [- 1 , 1 , 0 , 0 ]
18+ dy = [0 , 0 , - 1 , 1 ]
19+
20+ # 하나의 단지
21+ def bfs (x , y ):
22+ q = deque ()
23+ q .append ((x , y ))
24+ visited [x ][y ] = True
25+
26+ home = 1 # 단지 내 집 수
27+
28+ while q :
29+ x , y = q .popleft ()
30+
31+ for i in range (4 ):
32+ nx = x + dx [i ]
33+ ny = y + dy [i ]
34+
35+ if 0 <= nx < N and 0 <= ny < N : # 범위 내
36+ if not visited [nx ][ny ] and graph [nx ][ny ] == 1 : # 방문 X, 그래프에 있다면
37+ visited [nx ][ny ] = True
38+ q .append ((nx , ny ))
39+ home += 1 # 단지 집 수 추가
40+
41+ return home # 한 단지 집 개수
42+
43+ answer = []
44+
45+ for i in range (N ):
46+ for j in range (N ):
47+ if not visited [i ][j ] and graph [i ][j ] == 1 :
48+ group = bfs (i , j )
49+ answer .append (group )
50+
51+ answer .sort () # 오름차순
52+
53+ print (len (answer ))
54+ for i in range (len (answer )):
55+ print (answer [i ])
You can’t perform that action at this time.
0 commit comments