경험의 기록

문제 : 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 2583 영역 구하기
#include<iostream>
#include<algorithm>
using namespace std;
 
int adj[101][101];
int visit[101];
int arr[101];
int m, n, k;
 
int arrcount = 0// 분리된 영역 수
int cnt = 0// 영역 넓이
int moveX[4= { 010-1 };
int moveY[4= { 10-10 };
 
void dfs(int a, int b) {
    if (adj[a][b] == 0) {
        adj[a][b] = 1;
        cnt++;
 
            for (int l = 0; l < 4++l) {
                int x = moveX[l] + a;
                int y = moveY[l] + b;
 
                if (x >= 0 && y >= 0 && x < m && y < n) {
                    dfs(x, y);
                }
            }
        }
}
 
int main() {
 
    int x1, y1, x2, y2;
 
    cin >> m >> n >> k;
 
    for (int i = 0; i < k; i++) {
        cin >> x1 >> y1 >> x2 >> y2;
 
        for (int i = x1; i < x2; ++i) {
            for (int j = y1; j < y2; ++j) {
                adj[j][i] = 1;
            }
        } // 직사각형 영역 표시하기
    }
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (adj[i][j] == 0) {
                cnt = 0;
                dfs(i, j);
                arr[arrcount++= cnt;
            }
        }
    } // 표시되있지 않은 곳에서 dfs 실행
    
    cout << arrcount << endl;
 
    sort(arr, arr + arrcount); // 오름차순으로 정렬
 
    for (int i = 0; i < arrcount; ++i) {
        cout << arr[i] << " ";
    }
}
 
 
 
 
cs
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading