백준 - 그래프(# 2667)

Eon·2020년 11월 23일
0

Algorithm

목록 보기
62/70

https://www.acmicpc.net/problem/2667


Code

def grouping(i, j):
    graph[i][j] = 2
    cnt = 0
    if graph[i-1][j] == 1:
        cnt += grouping(i-1, j)
    if graph[i+1][j] == 1:
        cnt += grouping(i+1, j)
    if graph[i][j-1] == 1:
        cnt += grouping(i, j-1)
    if graph[i][j+1] == 1:
        cnt += grouping(i, j+1)
    return cnt + 1
    

n = int(input())
graph = []
graph.append([0]*(n+2))
for _ in range(n):
    graph.append([0]+list(map(int, list(input())))+[0])
graph.append([0]*(n+2))

result = []
for i in range(1,n+1):
    for j in range(1,n+1):
        if graph[i][j]==1:
            cnt = grouping(i,j)
            result.append(cnt)
            
result.sort()
print(len(result))
for r in result:
    print(r)
profile
👨🏻‍💻 🏃🏻‍♂️ 🎶

0개의 댓글