소프티어 LV.2_장애물 인식 프로그램
import sys
from collections import deque
n = int(sys.stdin.readline().rstrip())
maps = [sys.stdin.readline().rstrip() for _ in range(n)]
queue = deque([])
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
visited = [[0]*n for _ in range(n)]
cnt = 1
for i in range(n):
for j in range(n):
if maps[i][j] == '1' and visited[i][j] == 0:
visited[i][j] = cnt
queue.append([i, j])
while queue:
x, y = queue.popleft()
for k in range(4):
nx, ny = x + dx[k], y + dy[k]
if 0<= nx < n and 0<= ny < n and maps[nx][ny] == '1':
if visited[nx][ny] == 0:
visited[nx][ny] = cnt
queue.append([nx, ny])
cnt += 1
max_value = 0
result = {}
for i in range(n):
for j in range(n):
max_value = max(max_value, visited[i][j])
if visited[i][j] > 0:
if visited[i][j] in result:
result[visited[i][j]] += 1
else:
result[visited[i][j]] = 1
result = sorted(result.items(), key=lambda x : x[1])
print(max_value)
for i in range(len(result)):
print(result[i][1])