ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • BOJ 17141 [연구소 2]
    알고리즘 풀이/BFS(너비 우선 탐색) 2021. 3. 4. 23:08

    BOJ 연구소 2 : www.acmicpc.net/problem/17141

     

    17141번: 연구소 2

    인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 M개를 놓을 것이고, 승원이의 신호와 동시에 바이

    www.acmicpc.net

    옛날에 연구소는 풀어봤는데, 2탄도 있는줄 몰랐다.

    나의 풀이 방법은 다음과 같다.

    1. 바이러스를 놓을 수 있는 구역을 먼저 체크해 vector에 담아놓는다.
    2. 브루트 포스를 이용해 바이러스를 놓는 모든 경우의 수를 체크한다.
    3. BFS로 바이러스를 확산시켜본다.

    제출하니 420ms나 걸린다...

    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
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    #include <iostream>
    #include <queue>
    #include <vector>
     
    using namespace std;
     
    struct Node
    {
        int x;
        int y;
        int depth;
    public:
        Node(int x, int y, int depth)
        {
            this->= x;
            this->= y;
            this->depth = depth;
        }
    };
     
    struct Point
    {
        int x;
        int y;
    public:
        Point(int x, int y)
        {
            this->= x;
            this->= y;
        }
    };
     
    int N, M;
     
    int result = 999999;
     
    int lab[50][50];
    int corona[50][50];
     
     
    bool visited[50][50];
    int dx[] = { -1,0,0,1 };
    int dy[] = { 0,-1,1,0 };
     
    queue<Node> q;
    vector<Point> selectedPoint;
    vector<Point> virusPoint;
     
    void InitializeVisit()
    {
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                visited[i][j] = false;
            }
        }
    }
     
    void InputVirus()
    {
        cin >> N >> M;
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                cin >> lab[i][j];
                
                if(lab[i][j] == 2)
                {
                    virusPoint.push_back(Point(i, j));
                }
            }
        }
    }
     
    void CopyLab()
    {
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                switch (lab[i][j])
                {
                case 1:
                    corona[i][j] = -1;
                    break;
                case 0:
                case 2:
                    corona[i][j] = 0;
                    break;
                }
            }
        }
    }
     
    bool IsBoundary(int x, int y)
    {
        return x >= 0 && x < N && y >= 0 && y < N;
    }
     
    int GetResult()
    {
        int time = 0;
     
        CopyLab();
     
        for (int i = 0; i < selectedPoint.size(); i++)
        {
            int x = selectedPoint[i].x;
            int y = selectedPoint[i].y;
     
            corona[x][y] = 1;
            
            q.push(Node(x, y, 0));
        }
     
        while (!q.empty())
        {
            Node node = q.front();
            q.pop();
            
            time = node.depth;
     
            for (int j = 0; j < 4; j++)
            {
                int nextX = node.x + dx[j];
                int nextY = node.y + dy[j];
                if (IsBoundary(nextX, nextY) && corona[nextX][nextY] == 0)
                {
                    corona[nextX][nextY]++;
                    q.push(Node(nextX, nextY, node.depth + 1));
                }
            }
        }
        return time;
    }
     
    bool EmptyCheck()
    {
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (corona[i][j] == 0)
                {
                    return false;
                }
            }
        }
        return true;
    }
     
    void SelectPoint(int depth, int index)
    {
        if (depth == M || index == N * N)
        {
            int time = GetResult();
            
            if (EmptyCheck())
            {
                if (time <= result)
                {
                    result = time;
                }
            }
            return;
        }
        int size = virusPoint.size();
        for (int i = index; i < size; i++)
        {
            int x = virusPoint[i].x;
            int y = virusPoint[i].y;
            
            selectedPoint.push_back(Point(x, y));
            SelectPoint(depth + 1, i + 1);
            selectedPoint.pop_back();
            SelectPoint(depth, i + 1);
        }
    }
     
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        
        InputVirus();
     
        SelectPoint(00);
     
        if (result == 999999)
        {
            result = -1;
        }
     
        cout << result << endl;
        return 0;
    }
     
    cs

     

    반응형

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

    BOJ 9184 [신나는 함수 실행]  (0) 2021.03.07
    BOJ 17836 [공주님을 구해라!]  (0) 2021.03.07
    BOJ 11559 [Puyo Puyo]  (4) 2018.10.04
    BOJ 2146 [다리 만들기]  (2) 2018.10.04
    BOJ 1726 [로봇]  (0) 2018.10.04

    댓글

Designed by Tistory.