반응형

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
#include <iostream>
#include <queue>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int m, n, k, cnt;
int dx[4= { 10-10 };
int dy[4= { 010-1 };
bool visited[101][101];
priority_queue<intvector<int>, greater<int> > q;
void dfs(int x, int y) {
    cnt++;
    visited[x][y] = 1;
    rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
            if (!visited[nx][ny]) 
                dfs(nx, ny);
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> m >> n >> k;
    rep(i,k){
        int lx, ly, rx, ry;
        cin >> lx >> ly >> rx >> ry;
        // 입력 받은 영역 채우기
        for (int i = lx; i < rx; i++) {
            for (int j = ly; j < ry; j++
                visited[i][j] = 1;
            
        }
    }
 
    int ans = 0;
    rep(i, n) {
        rep(j, m) {
            if (!visited[i][j]) {
                cnt = 0;
                dfs(i, j);
                q.push(cnt);
                ans++;
            }
        }
    }
    cout << ans << '\n';
    while (!q.empty()) {
        cout << q.top() << ' ';
        q.pop();
    }
}
cs
반응형

'백준 > DFS, BFS' 카테고리의 다른 글

백준 1707  (0) 2021.02.19
백준 13023  (0) 2021.02.19
백준 2468  (0) 2021.02.03
백준 1697  (0) 2021.02.03
백준 1012  (0) 2021.02.03

+ Recent posts