-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2583_영역_구하기.py
41 lines (33 loc) · 1005 Bytes
/
2583_영역_구하기.py
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
from collections import deque
import sys
def input(): return sys.stdin.readline().strip()
darr = [(1, 0), (-1, 0), (0, 1), (0, -1)]
M, N, K = map(int, input().split())
data = [list(map(int, input().split())) for _ in range(K)]
board = [[False] * N for _ in range(M)]
for x1, y1, x2, y2 in data:
for x in range(x1, x2):
for y in range(y1, y2):
board[y][x] = True
answer = []
for i in range(N * M):
sy, sx = divmod(i, N)
if board[sy][sx] == True:
continue
queue = deque([(sx, sy)])
board[sy][sx] = True
cnt = 1
while queue:
x, y = queue.popleft()
for dx, dy in darr:
nx, ny = x + dx, y + dy
if not 0 <= nx < N or not 0 <= ny < M:
continue
if board[ny][nx]:
continue
board[ny][nx] = True
cnt += 1
queue.append((nx, ny))
answer.append(cnt)
print(len(answer))
print(*sorted(answer))