티스토리 뷰
728x90
https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
from collections import deque
def bfs(x, y):
dq = deque()
dq.append([x, y])
cnt = 0
while dq:
x, y = dq.popleft()
for dx, dy in direction:
nx, ny = x + dx, y + dy
if 0 <= nx < N and 0 <= ny < N and arr[nx][ny] == 1 and not visit[nx][ny]:
visit[nx][ny] = True
cnt += 1
dq.append([nx, ny])
if cnt == 0:
cnt = 1
return cnt
N = int(input())
arr = []
visit = [[False for _ in range(N + 1)] for _ in range(N + 1)]
direction = [(0, 1), (1, 0), (0, -1), (-1, 0)]
res = []
for _ in range(N):
lst = list(map(int, input().rstrip()))
arr.append(lst)
for i in range(N):
for j in range(N):
if arr[i][j] == 1 and not visit[i][j]:
area = bfs(i, j)
res.append(area)
res.sort()
print(len(res))
for i in res:
print(i)
|
cs |
'코딩 > 파이썬 백준' 카테고리의 다른 글
백준 15815 천재 수학자 성필 (파이썬) (0) | 2022.03.08 |
---|---|
백준 2583 영역 구하기 (파이썬) (0) | 2022.03.07 |
백준 2251 물통 (파이썬) (0) | 2022.03.04 |
백준 2304 창고 다각형 (파이썬) (0) | 2022.03.02 |
백준 16396 선 그리기 (파이썬) (0) | 2022.03.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준 2304
- 백준 1504 파이썬
- 백준 20362 파이썬
- 백준 10825
- 백준 2304 파이썬
- 백준 2075 파이썬
- 백준 9205 파이썬
- 백준 6593
- 백준 12788 파이썬
- 백준 23253 파이썬
- 백준 4446 파이썬
- 백준 1916
- 백준 4446
- 백준 10825 파이썬
- 백준 23253
- 백준 2075
- 백준 13335 파이썬
- 백준 2491 파이썬
- 백준 13335
- 백준 12034 파이썬
- 백준 1351
- 백준 12034
- 백준 11123
- 백준 11123 파이썬
- 백준 1916 파이썬
- 백준 6593 파이썬
- 백준 12788
- 백준 1351 파이썬
- 백준 9205
- 백준 20362
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
글 보관함