cmod.ify
[2667] 단지번호붙이기 본문
728x90
반응형
그냥 제출 했는데 틀려서 뭐지 하고 다시 글 봤더니 정렬해서 출력해야 했다
앗차차 글을 잘 읽도록 하자
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
gra = []
for i in range(n):
li = input().strip()
li2 = [int(i) for i in li]
gra.append(li2)
dx = [0,0,-1,1]
dy = [-1,1,0,0]
visited = [[False] * n for _ in range(n)]
def bfs(x, y, cnt):
q = deque([(x,y)])
visited[x][y] = True
while q:
cx, cy = q.popleft()
for i in range(4):
nx = cx + dx[i]
ny = cy + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if visited[nx][ny] == False:
if gra[nx][ny] == 1:
q.append((nx, ny))
visited[nx][ny] = True
cnt += 1
return cnt
answer = []
for i in range(n):
for j in range(n):
if gra[i][j] == 1 and visited[i][j] == False:
answer.append(bfs(i,j,1))
print(len(answer))
answer.sort()
for i in answer:
print(i)
728x90
반응형
'BASIC > 코딩테스트' 카테고리의 다른 글
| [11286] 절댓값 힙 (0) | 2026.01.06 |
|---|---|
| [5525] IOIOI (0) | 2026.01.06 |
| [2178] 미로 탐색 (0) | 2026.01.05 |
| [1389] 케빈 베이컨의 6단계 법칙 (0) | 2025.12.31 |
| [30804] 과일 탕후루 (0) | 2025.12.30 |