ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • BOJ 15683 [감시]
    알고리즘 풀이/DFS(깊이 우선 탐색) 2018. 9. 18. 07:04

    BOJ 감시 : https://www.acmicpc.net/problem/15683



    안녕하세요. 이번 문제는 백준 온라인 저지의 감시입니다.


    이 문제는 제가 2018년 삼성전자DS 인턴을 지원했을때 겪은 문제인데요,


    당시 BFS로 접근했다가 큰 낭패를 봤고, 이번엔 DFS로 접근해봤습니다.


    인턴시험때 느낀 것은, 당황하더라도 빠르게 침착함을 유지해야 했다는 것입니다. 



    문제 해결과정은 다음과 같습니다.



    1. 카메라의 좌표를 vector에 담는다.


    2. 각 depth에 해당하는 카메라를 방향마다 설정한다.


    3. 모든 카메라를 세팅하면, 감시영역을 설치하고 사각지대 검사.


    4. 다시 감시영역을 회수한 뒤, 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
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    #include <iostream>
    #include <vector>
    using namespace std;
    int N, M;
    int Map[8][8];
    int Result = 0;
    int dx[4= {0-101};
    int dy[4= {10-10};
    vector<pair<intint>> camera;
    int camera_setting[8][4];
    void DFS(int depth);
    void Set(int depth, int direction);
    void Back(int depth, int direction);
    int Calc() {
        int res = 0;
        for(int i=0; i<N; i++) {
            for(int j=0; j<M; j++) {
                if(!Map[i][j])
                    res++;
            }
        }
        return res;
    }
    void DFS(int depth) {
        if(depth == camera.size()) {
            for(int i = 0; i<camera.size(); i++) {
                for(int j=0; j<4; j++) {
                    if(camera_setting[i][j]) {
                        Set(i, j+1);
                    }
                }
            }
            int temp = Calc();
            if(temp < Result)
                Result = temp;
            for(int i=0; i<camera.size(); i++) {
                for(int j=0; j <4; j++) {
                    if(camera_setting[i][j]) {
                        Back(i, j+1);
                    }
                }
            }
            return;
        }
        int c_i = camera[depth].first;
        int c_j = camera[depth].second;
        switch(Map[c_i][c_j]) {
            case 1: {
                for(int i=1; i<=4; i++) {
                    camera_setting[depth][i-1= 1;
                    DFS(depth + 1);
                    camera_setting[depth][i-1= 0;
                }
                break;
            }
            case 2: {
                for(int i=1; i<=2; i++) {
                    camera_setting[depth][i-1= 1;
                    camera_setting[depth][i+1= 1;
                    DFS(depth + 1);
                    camera_setting[depth][i-1= 0;
                    camera_setting[depth][i+1= 0;
                }
                break;
            }
            case 3: {
                for(int i=1; i<=4; i++) {
                    camera_setting[depth][i-1= 1;
                    if(i != 4)
                        camera_setting[depth][i] = 1;
                    else
                        camera_setting[depth][0= 1;
                    DFS(depth+1);
                    camera_setting[depth][i-1= 0;
                    if(i != 4)
                        camera_setting[depth][i] = 0;
                    else
                        camera_setting[depth][0= 0;
                }
                break;
            }
            case 4: {
                for(int i= 1; i<= 4; i++) {
                    for(int j= 1; j<=4; j++) {
                        if(i == j)
                            continue;
                        camera_setting[depth][j-1= 1;
                    }
                    DFS(depth + 1);
                    for(int j= 1; j<=4; j++) {
                        if(i == j)
                            continue;
                        camera_setting[depth][j-1= 0;
                    }
                }
                break;
            }
            case 5: {
                for(int i=1; i<=4; i++)
                    camera_setting[depth][i-1= 1;
                DFS(depth + 1);
                break;
            }
        }
    }
    void Set(int depth, int direction) {
        int c_i = camera[depth].first;
        int c_j = camera[depth].second;
        int length = 0;
        switch(direction) {
            case 1: {
                length = M - c_j - 1;
                break;
            }
            case 2: {
                length = c_i;
                break;
            }
            case 3: {
                length = c_j;
                break;
            }
            case 4: {
                length = N - c_i - 1;
                break;
            }
        }
        for(int k = 1; k <= length; k++) {
            if(Map[c_i + dx[direction - 1* k][c_j + dy[direction - 1* k] == 0) {
                Map[c_i + dx[direction - 1* k][c_j + dy[direction - 1* k] = -1;
            }
            else if(Map[c_i + dx[direction - 1* k][c_j + dy[direction - 1* k] == 6) {
                break;
            }
        }
    }
    void Back(int depth, int direction) {
        int c_i = camera[depth].first;
        int c_j = camera[depth].second;
        int length = 0;
        switch(direction) {
            case 1: {
                length = M - c_j - 1;
                break;
            }
            case 2: {
                length = c_i;
                break;
            }
            case 3: {
                length = c_j;
                break;
            }
            case 4: {
                length = N - c_i - 1;
                break;
            }
        }
        for(int k = 1; k <= length; k++) {
            if(Map[c_i + dx[direction - 1* k][c_j + dy[direction - 1* k] == -1) {
                Map[c_i + dx[direction - 1* k][c_j + dy[direction - 1* k] = 0;
            }
            else if(Map[c_i + dx[direction - 1* k][c_j + dy[direction - 1* k] == 6) {
                break;
            }
        }
    }
    int main() {
        cin >> N >> M;
        for(int i=0; i<N; i++) {
            for(int j=0; j<M; j++) {
                cin >> Map[i][j];
                if(Map[i][j] != 0 && Map[i][j] != 6)
                    camera.push_back(make_pair(i, j));
                else if(Map[i][j] == 0)
                    Result++;
            }
        }
        DFS(0);
        cout << Result << endl;
        return 0;
    }
    cs


    반응형

    '알고리즘 풀이 > DFS(깊이 우선 탐색)' 카테고리의 다른 글

    SWEA 2112 [보호 필름]  (0) 2018.09.25
    SWEA 1767 [프로세서 연결하기]  (2) 2018.09.23
    BOJ 14500 [테트로미노]  (0) 2018.09.22
    BOJ 1941 [소문난 칠공주]  (2) 2018.09.22
    BOJ 15686 [치킨 배달]  (3) 2018.09.18

    댓글

Designed by Tistory.