-

[DFS] 백준 2468번 - 안전영역 (정답률 33%) 본문

3. DFS & 백트래킹

[DFS] 백준 2468번 - 안전영역 (정답률 33%)

asdklfjlasdlfkj 2020. 1. 13. 18:05

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

 

2468번: 안전 영역

재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는 안전한 영역이 최대로 몇 개가 만들어 지는 지를 조사하려고 한다. 이때, 문제를 간단하게 하기 위하여, 장마철에 내리는 비의 양에 따라 일정한 높이 이하의 모든 지점은 물에 잠긴다고 가정한다. 어떤 지역의 높이 정보는 행과 열의 크기가 각각 N인 2차원 배열 형태로 주어

www.acmicpc.net

백준 DFS 연습문제다.

보통 이런 문제에 대해 한 가지 경우에 대한 문제는 50~60%정도 정답률을 보이지만,

이 문제와 같이 다양한 경우에 대해 dfs 탐색을 요구하는 문제는 정답률이 약 30%대로 떨어진다.

 

전자의 문제와 다를 바 없이, 매 조건에 대해 새롭게 방문여부 벡터를 초기화해주고, 조건에 맞게 세팅을 한 뒤에 dfs 탐색을 똑같이 하면 쉽게 풀 수 있다.

 

아래는 제출한 코드 (16ms)

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>
#define INF 987654321
using namespace std;
 
int N;
int maxheight = -1;
int answer = -INF;
int cur_safety_fields = 0;
vector<vector<int> > height;
vector<vector<bool> > check;
typedef struct dir{
    int dr, dc;
}dir;
dir direction[4= { { 01 }, { 0-1 }, { 10 }, { -10 } };
 
void reset(){
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            check[i][j] = false;
}
 
void dfs(int r, int c){
    for (int i = 0; i < 4; i++){
        int nr = r + direction[i].dr;
        int nc = c + direction[i].dc;
        if (nr >= 0 && nr < N && nc >= 0 && nc < N){
            if (!check[nr][nc]){
                check[nr][nc] = true;
                dfs(nr, nc);
            }
        }
    }
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> N;
    height.assign(N, vector<int>(N, 0));
    check.assign(N, vector<bool>(N, 0));
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            cin >> height[i][j];
            if (height[i][j] > maxheight)
                maxheight = height[i][j];
        }
    }
    
    for (int limit = 0; limit <= maxheight; limit++){
        //limit 이하 지역 모두 침수, 안전영역 구하기.
 
        // 1. limit 이하 지역 모두 침수
        for (int i = 0; i < N; i++){
            for (int j = 0; j < N; j++){
                if (height[i][j] <= limit){
                    check[i][j] = true;
                }
            }
        }
 
        // 2. 이제 안전영역의 개수를 구해보자.
        for (int i = 0; i < N; i++){
            for (int j = 0; j < N; j++){
                if (!check[i][j]){
                    check[i][j] = true;
                    dfs(i, j);
                    cur_safety_fields++;
                }
            }
        }
        answer = max(answer, cur_safety_fields);
        cur_safety_fields = 0;
        reset();
    }
    cout << answer << '\n';
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

Comments