-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj_02667.py
More file actions
36 lines (30 loc) · 748 Bytes
/
boj_02667.py
File metadata and controls
36 lines (30 loc) · 748 Bytes
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
# 2022.02.28
# Dongyoung Kwon @Chuncheonian (ehddud2468@gmail.com)
# https://www.acmicpc.net/problem/2667
n = int(input())
matrix = [list(map(int, input())) for _ in range(n)]
visited = [[0] * n for _ in range(n)]
dr, dc = [0, -1, 0, 1], [1, 0, -1, 0]
cnt = 0
def dfs(x, y):
global cnt
if x < 0 or x >= n or y < 0 or y >= n:
return False
if matrix[x][y] == 1:
cnt += 1
matrix[x][y] = 0
for i in range(4):
nx = x + dr[i]
ny = y + dc[i]
dfs(nx, ny)
return True
result = []
for i in range(n):
for j in range(n):
if dfs(i, j):
result.append(cnt)
cnt = 0
print(len(result))
result.sort()
for elem in result:
print(elem)