https://www.acmicpc.net/problem/2667
n = int(input())
data = [list(map(int, input())) for _ in range(n)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def dfs(x, y):
count = 1
data[x][y] = 0
q = [(x, y)]
while q:
x, y = q.pop()
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < n and 0 <= ny < n and data[nx][ny] == 1:
q.append((nx,ny))
data[nx][ny] = 0
count += 1
return count
result = 0
number = []
for x in range(n):
for y in range(n):
if data[x][y] == 1:
result += 1
c = dfs(x, y)
number.append(c)
number.sort()
print(result)
for num in number:
print(num)
'알고리즘' 카테고리의 다른 글
백준 10026번 : 적록색약(python) (0) | 2021.07.26 |
---|---|
백준 2583번 : 영역 구하기(python) (0) | 2021.07.26 |
백준 7576번 : 토마토(python) (0) | 2021.07.26 |
백준 1600번 : 말이 되고픈 원숭이(Python) (0) | 2021.06.20 |
programmers 두 개 뽑아서 더하기(Python) (0) | 2021.06.12 |