728x90
파이썬
실1 단지번호붙이기
https://www.acmicpc.net/problem/2667
|
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
38
39
40
41
42
43
|
import sys
from collections import deque
input = sys.stdin.readline
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def bfs(graph, a, b):
queue = deque()
queue.append((a, b))
graph[a][b] = 0
cnt = 1
n = len(graph)
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx >= n or ny >= n or nx < 0 or ny < 0:
continue
if graph[nx][ny] == 1:
graph[nx][ny] = 0
queue.append((nx, ny))
cnt += 1
return cnt
input = sys.stdin.readline
N = int(input())
house = []
for i in range(N):
house.append(list(map(int, input().rstrip())))
##print(house)
countHouse = []
for i in range(N):
for j in range(N):
if house[i][j] == 1:
countHouse.append(bfs(house, i, j))
countHouse.sort()
print(len(countHouse))
for i in range(len(countHouse)):
print(countHouse[i])
|
cs |
728x90
'Python' 카테고리의 다른 글
| 211213 백준 코딩연습 파이썬 7576 토마토 (0) | 2021.12.14 |
|---|---|
| 211210 백준 코딩연습 파이썬 2606 바이러스 (0) | 2021.12.10 |
| 211209 백준 코딩연습 파이썬 10451 순열 사이클 (0) | 2021.12.10 |
| 211208 백준 코딩연습 파이썬 2750 수 정렬하기 (0) | 2021.12.08 |
| 211207 파이썬 코딩테스트 수업 5일차, 백준 코딩연습 파이썬 2331 반복수열, 컴프리헨션 (0) | 2021.12.07 |





최근댓글