https://www.acmicpc.net/problem/2667
2667: 복소수 넘버링
에서와 같이
www.acmicpc.net
import sys
from collections import deque
input=sys.stdin.readline
def bfs(x, y):
dq=deque(((x, y)))
visited(x)(y)=1
count=1
while dq:
i, j=dq.popleft()
for dx, dy in directions:
nx=i+dx
ny=j+dy
if 0<=nx<N and 0<=ny<N and visited(nx)(ny)==0 and map(nx)(ny)==1:
visited(nx)(ny)=1
dq.append((nx, ny))
count+=1
result.append(count)
N=int(input())
map=(list(map(int, input().rstrip())) for _ in range(N))
directions=((1, 0), (-1, 0), (0, 1), (0, -1))
visited=((0)*N for _ in range(N))
result=()
for i in range(N):
for j in range(N):
if map(i)(j)==1 and visited(i)(j)==0:
bfs(i, j)
result.sort()
print(len(result))
for r in result:
print(r)
![[자바]백준 2042번 구간 합 [자바]백준 2042번 구간 합](https://full.pumptrack.kr/wp-content/plugins/contextual-related-posts/default.png)