[알고리즘] 백준 - 2667 (단지번호붙이) / 파이썬

배고픈메꾸리·2021년 7월 31일
0

알고리즘

목록 보기
104/128

import sys
n = int(sys.stdin.readline().rstrip())

town = [[] for _ in range(n)]
count = []
dx = [1,0,-1,0]
dy = [0,1,0,-1]

dange = 2

for i in range(n):
    town[i] += map(int,sys.stdin.readline().rstrip())

from collections import deque
def BFS(startX , startY):
    queue = deque([[startX,startY]])
    town[startX][startY] = dange
    c = 1
    while queue:
        pop = queue.popleft()
        for i in range(4):
            nextX = pop[0] + dx[i]
            nextY = pop[1] + dy[i]
            if(0<= nextX < n and  0 <= nextY < n and town[nextX][nextY] ==1 ):
                queue.append([nextX,nextY])
                town[nextX][nextY] = dange
                c+=1
    count.append(c)



for i in range(n):
    for j in range(n):
        if(town[i][j] ==1 ):
            BFS(i,j)
            dange += 1
            
print(len(count))
count.sort()
for i in count:
    print(i)


profile
FE 개발자가 되자

0개의 댓글