반응형

www.acmicpc.net/problem/2468

 

2468번: 안전 영역

재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는

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 <cstring>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, h, arr[101][101];
bool visited[101][101];
int dx[4= { 10-10 };
int dy[4= { 010-1 };
void dfs(int x, int y) {
    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 < n) {
            if (arr[nx][ny] > h && !visited[nx][ny])
                dfs(nx, ny);
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    int min_height = 101, max_height = -1;
    rep(i, n) {
        rep(j, n) {
            cin >> arr[i][j];
            if (arr[i][j] < min_height)
                min_height = arr[i][j];
            if (arr[i][j] > max_height)
                max_height = arr[i][j];
        }
    }
    int ans = 1;
    // 최소 높이 ~ 최대 높이 한 번씩 다 해봄
    for (int k = min_height; k <= max_height; k++) {
        memset(visited, 0sizeof(visited));
        int cnt = 0;
        h = k;
        rep(i, n) {
            rep(j, n) {
                // 도시가 강수량보다 높으면
                if (arr[i][j] > k && !visited[i][j]) {
                    dfs(i, j);
                    cnt++;
                }
            }
        }
        ans = max(ans, cnt);
    }
    cout << ans;
}
cs
반응형

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

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

+ Recent posts