Algorithm/백준

[BOJ] - 14502. 연구소

benguin 2019. 7. 3. 11:57

[URL]

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

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크기가 N×M인 직사각형으로 나타낼 수 있으며, 직사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.  일부 칸은 바이러스가 존재하며, 이 바이러스는 상하좌우로 인접한 빈 칸으로 모두 퍼져나갈 수 있다.

www.acmicpc.net

 

[풀이 과정]

* DFS, BFS

 

1. map입력 : 0인 곳 저장(빈공간),  2인 곳 저장(바이러스) --> 벡터에 저장

 

2. 빈 공간 중 후보군 3개 선출(DFS)

 

3. 3개 선출 한 곳 벽으로 세우고 바이러스 확산(BFS)

 

4. 최종 빈 공간 계산 --> 최대치 계산

 

[소스 코드]

 

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
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
 
struct info {
    int x;
    int y;
};
 
int N, M;
int map[8][8];
int c_map[8][8];
int MAX;
 
vector <info> v;
vector <info> virus;
 
bool checkDFS[64];    // DFS에서 체크 배열
bool checkBFS[8][8= {0, };
int candIndex[3];    // v값들 중 3개 후보 인덱스!! 저장
 
queue <info> q;
 
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
 
void clear() {
    memset(checkDFS, 0sizeof(bool* 64);
    memset(candIndex, -1sizeof(int* 3);
}
 
void input() {
    scanf("%d %d"&N, &M);
 
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            scanf("%d"&map[i][j]);
 
            // 빈 공간 벡터에 담기
            if (map[i][j] == 0)
            {
                info temp;
                temp.x = i;
                temp.y = j;
 
                v.push_back(temp);
            }
 
            // 바이러스 벡터에 담기
            if (map[i][j] == 2)
            {
                info temp;
                temp.x = i;
                temp.y = j;
 
                virus.push_back(temp);
            }
        }
    }
}
 
void BFS(int index) {
    info temp;
    temp.x = virus[index].x;
    temp.y = virus[index].y;
 
    q.push(temp);
 
    while (!q.empty())
    {
        int cx = q.front().x;
        int cy = q.front().y;
        checkBFS[cx][cy] = true;
 
        q.pop();
 
        for (int k = 0; k < 4; k++)
        {
            int nx = cx + dx[k];
            int ny = cy + dy[k];
 
            if (nx >= 0 && nx < N &&  ny >= 0 && ny < M && c_map[nx][ny] == 0 && checkBFS[nx][ny] == false)
            {
                c_map[nx][ny] = 2;
 
                info temp;
                temp.x = nx;
                temp.y = ny;
 
                q.push(temp);
            }
        }
    }
}
 
// 벽 세워 놓고 바이러스 퍼졌을 때 나오는 빈공간 계산
int calc() {
    int res = 0;
 
    // map[][] --> c_map[][] 카피
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            c_map[i][j] = map[i][j];
        }
    }
 
    // 후보 3개 벽으로 넣어주기
    for (int i = 0; i < 3; i++)
    {
        c_map[v[candIndex[i]].x][v[candIndex[i]].y] = 1;
    }
 
    // check[][] 0으로 초기화
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            checkBFS[i][j] = 0;
        }
    }
 
    // 바이러스 확산
    for (int i = 0; i < virus.size(); i++)
    {
        BFS(i);
    }
 
    // 최종 남은 공간 계산
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (c_map[i][j] == 0)
            {
                res++;
            }
        }
    }
 
    return res;
}
 
void DFS(int cnt) {
    if (cnt == 3)
    {
        // 후보 3개 추출
        int temp = calc();
        if (temp > MAX)
            MAX = temp;
 
        return;
    }
 
    //개념: 가능한 후보 전체(v.size()) 중 3개만 추출
    for (int i = candIndex[cnt - 1]; i < v.size(); i++)
    {
        if (candIndex[cnt] == -1 && checkDFS[i] == false)
        {
            checkDFS[i] = true;
            candIndex[cnt] = i;
 
            DFS(cnt + 1);
 
            candIndex[cnt] = -1;
            checkDFS[i] = false;
        }
    }
}
 
int main() {
 
    //clear();
    clear();
 
    // 1. 맵 입력
    input();
 
    // 2. dfs() -> 벽 3개 정하기
    DFS(0);
 
    printf("%d\n", MAX);
 
    return 0;
}
cs

'Algorithm > 백준' 카테고리의 다른 글

[BOJ] - 16236. 아기 상어  (0) 2019.07.10
[BOJ] - 13023. ABCDE  (0) 2019.07.10
[BOJ] - 2156. 포도주 시식  (0) 2019.07.01
[BOJ] - 17144. 미세먼지 안녕!  (0) 2019.06.25
[BOJ] - 2178. 미로 탐색  (0) 2019.06.20