OSgood의 개발일기

[백준] 2583번 영역 구하기 본문

Algorithm/Algorithm 문제 연습

[백준] 2583번 영역 구하기

OSgood 2020. 2. 23. 15:41

문제 링크

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

 

2583번: 영역 구하기

첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오른쪽 위 꼭짓점의 x, y좌표값이 빈칸을 사이에 두고 차례로 주어진다. 모눈종이의 왼쪽 아래 꼭짓점의 좌표는 (0,0)이고, 오른쪽 위 꼭짓점의 좌표는(N,M)이다. 입력되는 K개의 직사각형들이 모눈종이 전체를 채우는 경우는 없다.

www.acmicpc.net

전형적인 탐색 문제다. bfs를 해도 되고 dfs를 해도 상관 없다. 필자 같은 경우는 dfs가 좀 더 편하기 때문에 dfs로 코드를 작성하였다. 급하게 풀어서 변수명들이 개판인 것 같다. 앞으로는 좀 더 신경써서 변수명을 정해야겠다.

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
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int M,N,K;
int wholemap[100][100= { 0, };
int result_count = 0;
vector<int> result_pi;
 
int temp_area = 0;
int dfs(int x,int y)
{
    temp_area += 1;
    wholemap[x][y] = 1;
 
    if (wholemap[x + 1][y] == 0 && ((x + 1<N))
    {
        dfs(x + 1, y);
    }
    if (wholemap[x ][y+1== 0 && ((y + 1< M))
    {
        dfs(x , y+1);
    }
    if (wholemap[x][y - 1== 0 && ((y - 1> -1))
    {
        dfs(x, y - 1);
    }
    if (wholemap[x-1][y ] == 0 && ((x - 1> -1))
    {
        dfs(x-1, y );
    }
 
    return temp_area;
 
}
 
int main() {
 
    cin >> M >> N >> K;
 
    int tempquad[4];
 
    for (int i = 0; i < K; i++)
    {
        cin >> tempquad[0>> tempquad[1>> tempquad[2>> tempquad[3];
    
        for (int x = tempquad[0]; x < tempquad[2]; x++)
        {
            for (int y = tempquad[1]; y < tempquad[3]; y++)
            {
                wholemap[x][y] += 1;
            }
        }
    }
    
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (wholemap[i][j] == 0)
            {
                temp_area = 0;
                result_pi.push_back(dfs(i, j));    
            }
        }
    }
 
    result_count = result_pi.size();
    sort(result_pi.begin(), result_pi.end());
    cout << result_count << endl;
    for (auto i : result_pi)
    {
        cout << i << " ";
    }
 
    return 0;
}
 
cs
Comments