728x90
https://www.acmicpc.net/problem/2667
from collections import deque
n = int(input())
graph = [ list(map(int, input())) for _ in range(n) ]
dx = [ -1, 1, 0, 0 ]
dy = [ 0, 0, -1, 1 ]
answer = []
cnt = 0 #1
res = 0 #2
def bfs(x, y):
global cnt
graph[x][y] = 2 #3
q = deque()
q.append((x,y)) #4
while q:
x, y = q.popleft()
cnt += 1 #5
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if graph[nx][ny] == 1:
graph[nx][ny] = 2
q.append((nx, ny))
return cnt #6
for i in range(n):
for j in range(n):
if graph[i][j] == 1: #7
answer.append(bfs(i, j))
res += 1
cnt = 0
answer.sort()
print(res)
for i in answer:
print(i)
728x90
'[Coding Test] > [백준]' 카테고리의 다른 글
[백준] 1929 파이썬(python) : 소수 구하기 - (에라토스테네스의 체) (0) | 2022.07.05 |
---|---|
[백준] 1697: 숨바꼭질 (0) | 2022.06.30 |
[백준] 7569: 토마토 (0) | 2022.06.30 |
[백준] 2178: 미로 탐색 (0) | 2022.06.27 |
[백준] 2869 python(파이썬) : 달팽이는 올라가고 싶다 - 상세해설 (0) | 2022.05.20 |