https://www.acmicpc.net/problem/2667
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
board = [list(map(int, input().strip())) for _ in range(n)]
is_home = []
for i in range(n):
for j in range(n):
if board[i][j] == 1:
is_home.append((i, j))
di = [0, 1, 0, -1]
dj = [1, 0, -1, 0]
def bfs(si, sj):
global visited
count = 1
dq = deque([(si, sj)])
visited[si][sj] = True
while dq:
ci, cj = dq.popleft()
for d in range(4):
ni, nj = di[d] + ci, dj[d] + cj
if 0 <= ni < n and 0 <= nj < n and not visited[ni][nj]:
visited[ni][nj] = True
if board[ni][nj] == 1: # 집이면
count += 1
dq.append((ni, nj))
return count
visited = [[False] * n for _ in range(n)]
dange = 0
counts = []
for si, sj in is_home:
if not visited[si][sj]:
counts.append(bfs(si, sj))
dange += 1
counts.sort()
print(dange)
for count in counts:
print(count)
'PS > 백준' 카테고리의 다른 글
[백준] 7576번 토마토 (python) (0) | 2024.12.30 |
---|---|
[백준] 1600번 말이 되고픈 원숭이 (python) (0) | 2024.12.30 |
[백준] 1913번 달팽이 (python 정답 코드) (0) | 2024.10.10 |
[백준] 21610번 마법사 상어와 비바라기 (python 정답 코드) (0) | 2024.10.09 |
[백준] 14890번 경사로 (python 정답 코드) (0) | 2024.10.08 |