티스토리 뷰

728x90

https://www.acmicpc.net/problem/2583

 

2583번: 영역 구하기

첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오

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
38
39
40
41
42
from collections import deque
 
 
def bfs(x, y):
    dq = deque()
    dq.append([x, y])
    cnt = 0
    while dq:
        x, y = dq.popleft()
        for d_x, d_y in direction:
            nx, ny = x + d_x, y + d_y
            if 0 <= nx < M and 0 <= ny < N and not visit[nx][ny] and arr[nx][ny] == 0:
                cnt += 1
                visit[nx][ny] = True
                dq.append([nx, ny])
    if cnt == 0:
        cnt = 1
    return cnt
 
 
direction = [(01), (10), (0-1), (-10)]
M, N, K = map(int, input().split())
arr = [[0 for _ in range(N)] for _ in range(M)]
visit = [[False for _ in range(N)] for _ in range(M)]
res,cnt = [], 0
for _ in range(K):
    ux, uy, dx, dy = map(int, input().split())
    for i in range(uy, dy):
        for j in range(ux, dx):
            arr[i][j] = 1
 
for i in range(M):
    for j in range(N):
        if arr[i][j] == 0 and not visit[i][j]:
            area = bfs(i, j)
            res.append(area)
            cnt += 1
 
res.sort()
print(cnt)
for i in res:
    print(i, end=' ')
cs

 

 

댓글