-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBJ2667.py
51 lines (43 loc) · 1.01 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
1. 아이디어
- 2중 for, 값 1 && 방문X => DFS
- DFS를 통해 찾은 값을 저장 후 정렬 해서 출력
2. 시간복잡도
- DFS : O(V+E)
- V, E : N^2, 4N^2
- V+E : 5N^2 ~= N^2 ~= 625 >> 가능
3. 자료구조
- 그래프 저장 : int[][]
- 방문여부 : bool[][]
- 결과값 : int[]
"""
import sys
input = sys.stdin.readline
N = int(input())
map = [list(map(int, input().strip())) for _ in range(N)]
chk = [[False] * N for _ in range(N)]
result = []
each = 0
dy = [0,1,0,-1]
dx = [1,0,-1,0]
def dfs(y, x):
global each
each += 1
for k in range(4):
ny = y + dy[k]
nx = x + dx[k]
if 0<=ny<N and 0<=nx<N:
if map[ny][nx] == 1 and chk[ny][nx] == False:
chk[ny][nx] = True
dfs(ny, nx)
for j in range(N):
for i in range(N):
if map[j][i] == 1 and chk[j][i] == False:
chk[j][i] = True
each = 0
dfs(j,i)
result.append(each)
result.sort()
print(len(result))
for i in result:
print(i)