본문 바로가기
알고리즘/DFS

2583 영역 구하기

by 시끄러인마 2019. 7. 30.
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
69
#include <stdio.h>
#include <queue>
 
using namespace std;
 
int check[105][105= { 0 };
int val = 0, m, n, k;
 
void DFS(int y, int x);
 
int main() {
    scanf("%d %d %d"&m, &n, &k);
 
    for (int i = 1; i <= k; i++) {
        int x1, x2, y1, y2;
        scanf("%d %d %d %d"&x1, &y1, &x2, &y2);
 
        for (int j = y1; j < y2; j++) {
            for (int k = x1; k < x2; k++) {
                check[j][k] = 1;
            }
        }
    }
 
    priority_queue<int> q;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (check[i][j] == 0) {
                check[i][j] = 1;
                val = 0;
                DFS(i, j);
                q.push(-val);
            }
        }
    }
 
    printf("%d\n", q.size());
    while (q.empty() == false) {
        printf("%d "-q.top());
        q.pop();
    }
}
 
void DFS(int y, int x) {
    if (y >= m || x >= n)
        return;
    val++;
 
    if (y>=1 && check[y - 1][x] == 0) {
        check[y - 1][x] = 1;
    
        DFS(y - 1, x);
    }
    if (check[y + 1][x] == 0) {
        check[y + 1][x] = 1;
 
        DFS(y + 1, x);
    }
    if (x >= 1 && check[y][x - 1== 0) {
        check[y][x-1= 1;
 
        DFS(y, x-1);
    }
    if (check[y][x + 1== 0) {
        check[y][x + 1= 1;
 
        DFS(y, x + 1);
    }
}
cs


'알고리즘 > DFS' 카테고리의 다른 글

1389 케빈 베이컨의 6단계 법칙  (0) 2019.08.01
6603 로또  (0) 2019.07.31
10026 적록색약  (0) 2019.07.30
11724 연결요소의 갯수  (0) 2019.07.30
11403 경로찾기  (0) 2019.07.30