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
44
45
46
47
48
49
|
# 2583 영역구하기
# BFS
# Python 60ms
M, N, K = map(int, input().split())
board = [[0 for _ in range(N)] for _ in range(M)]
def color_board(Lx, Ly, Rx, Ry):
for i in range(Lx, Rx):
for j in range(Ly, Ry):
board[j][i]= 1
for i in range(K): # Color the board
Lx, Ly, Rx, Ry = map(int, input().split())
color_board(Lx, Ly, Rx, Ry)
dx = [1,0,-1,0]
dy = [0,1,0,-1]
def bfs(start):
queue = [start]
square = 1
board[start[0]][start[1]]=2
while queue:
point = queue.pop()
for i in range(4):
nx = point[0] + dx[i]
ny = point[1] + dy[i]
if 0<=nx and nx<M and 0<=ny and ny<N and board[nx][ny] == 0:
board[nx][ny]=2
square +=1
queue.append((nx, ny))
return square
LST = []
for i in range(M):
for j in range(N):
if board[i][j]==0:
LST.append(bfs((i,j)))
print(len(LST))
LST.sort()
for i in LST:
print(i, end=" ")
|
'프로그래밍' 카테고리의 다른 글
[백준 7562 파이썬] 나이트 이동 (0) | 2020.02.25 |
---|---|
[백준 10026 파이썬] 적록색약 (0) | 2020.02.24 |
[백준 11724 파이썬] 연결 요소의 개수 (0) | 2020.02.24 |
[백준 2667 파이썬] 단지번호붙이기 (0) | 2020.02.24 |
[백준 1260 파이썬] DFS와 BFS (0) | 2020.02.24 |