Skip to content

Commit 1645f3a

Browse files
committed
[Silver I] Title: 단지번호붙이기, Time: 116 ms, Memory: 113496 KB -BaekjoonHub
1 parent 770b64e commit 1645f3a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Silver I] 단지번호붙이기 - 2667
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2667)
4+
5+
### 성능 요약
6+
7+
메모리: 113496 KB, 시간: 116 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색
12+
13+
### 제출 일자
14+
15+
2025년 2월 27일 13:29:24
16+
17+
### 문제 설명
18+
19+
<p><그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여기서 연결되었다는 것은 어떤 집이 좌우, 혹은 아래위로 다른 집이 있는 경우를 말한다. 대각선상에 집이 있는 경우는 연결된 것이 아니다. <그림 2>는 <그림 1>을 단지별로 번호를 붙인 것이다. 지도를 입력하여 단지수를 출력하고, 각 단지에 속하는 집의 수를 오름차순으로 정렬하여 출력하는 프로그램을 작성하시오.</p>
20+
21+
<p style="text-align: center;"><img alt="" src="https://www.acmicpc.net/upload/images/ITVH9w1Gf6eCRdThfkegBUSOKd.png" style="height:192px; width:409px"></p>
22+
23+
### 입력
24+
25+
<p>첫 번째 줄에는 지도의 크기 N(정사각형이므로 가로와 세로의 크기는 같으며 5≤N≤25)이 입력되고, 그 다음 N줄에는 각각 N개의 자료(0혹은 1)가 입력된다.</p>
26+
27+
### 출력
28+
29+
<p>첫 번째 줄에는 총 단지수를 출력하시오. 그리고 각 단지내 집의 수를 오름차순으로 정렬하여 한 줄에 하나씩 출력하시오.</p>
30+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
input = sys.stdin.readline
3+
from collections import deque
4+
5+
def bfs(i, j):
6+
q = deque()
7+
q.append((i, j))
8+
visited[i][j] = 1
9+
cnt = 1
10+
11+
dx = [-1, 1, 0, 0]
12+
dy = [0, 0, -1, 1]
13+
14+
while q:
15+
i, j = q.popleft()
16+
for k in range(4):
17+
ni, nj = i+dx[k], j+dy[k]
18+
if 0 <= ni < N and 0 <= nj < N and nums[ni][nj] == '1' and not visited[ni][nj]:
19+
visited[ni][nj] = 1
20+
q.append((ni, nj))
21+
cnt += 1
22+
23+
return cnt
24+
25+
N = int(input())
26+
nums = [input().rstrip() for _ in range(N)]
27+
visited = [[0]*N for _ in range(N)]
28+
res = []
29+
30+
for i in range(N):
31+
for j in range(N):
32+
if nums[i][j] == '1' and not visited[i][j]:
33+
res.append(bfs(i, j))
34+
35+
res.sort()
36+
print(len(res))
37+
for i in res: print(i)

0 commit comments

Comments
 (0)