1-1. 삼성 SW 테스트

[삼성SW테스트] 백준 17142번 연구소 3

asdklfjlasdlfkj 2019. 12. 25. 09:53

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

 

17142번: 연구소 3

인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 바이러스는 활성 상태와 비활성 상태가 있다. 가장 처음에 모든 바이러스는 비활성 상태이고, 활성 상태인 바이러스는 상하좌우로 인접한 모든 빈 칸으로 동시에 복제되며, 1초가 걸린다. 승원이는 연구소의 바이러스 M개를 활성 상태로 변경하려고 한다. 연구소는 크기가 N×N인 정사각형으로 나타낼 수 있으며, 정사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는

www.acmicpc.net

삼성 SW 역량테스트 기출문제이다.

바이러스의 존재 가능 위치와 실제 놓는 위치에 대한 next_permutation 함수의 활용으로 브루트포스로 풀 수 있었다.

놓을 수 있는 모든 경우의 수에 대해 최소시간을 확인해야 정답을 구할 수 있으므로 다른 트릭을 사용하라는 문제는 아니었다. 다만 문제를 꼼꼼히 봐서 비활성 바이러스를 감염시키지 않고 빈 공간을 모두 오염시켰을 경우 바로 시간 카운트를 종료해야하는 부분과 같이 세심한 코딩이 필요한 문제였다.

 

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
#include  <iostream>
#include  <queue>
#include  <algorithm>
#include  <vector>
#define maxlen 50 
using namespace std
 
typedef struct dir{ 
int dx, dy; 
}dir; 
dir direction[4= { { 01 }, { 0-1 }, { 10 }, { -10 } }; 
 
int N, M; 
int empty_space = 0, empty_space_copy = 0
int num_whole_virus = 0
int arr[maxlen][maxlen]; 
int Copy[maxlen][maxlen]; 
vector<pair<intint> > virus; // 바이러스 시작 위치. 
vector combination; // 바이러스 시작 위치 조합. 
vector answers; 
void Copy2arr(){ 
for (int i = 0; i < N; i++){ 
for (int j = 0; j < N; j++){ 
Copy[i][j] = arr[i][j]; 
 
void bfs(){ 
queue<pair<intint> > q; 
for (int i = 0; i < combination.size(); i++){ 
if (combination[i]){ 
q.push(make_pair(virus[i].first, virus[i].second)); 
int time = 0
while (!q.empty()){ 
int size = q.size(); 
for (int i = 0; i < size; i++){ 
int front_x = q.front().first; 
int front_y = q.front().second; 
Copy[front_x][front_y] = 3
q.pop(); 
for (int j = 0; j < 4; j++){ 
int next_x = front_x + direction[j].dx; 
int next_y = front_y + direction[j].dy; 
if (next_x >= 0 && next_x < N && next_y >= 0 && next_y < N){ 
if (Copy[next_x][next_y] != 3 && Copy[next_x][next_y] != 1){ 
if (Copy[next_x][next_y] == 0){ 
empty_space_copy--
Copy[next_x][next_y] = 3
q.push(make_pair(next_x, next_y)); 
else if (Copy[next_x][next_y] == 2){ 
Copy[next_x][next_y] = 3
q.push(make_pair(next_x, next_y)); 
time++
if (empty_space_copy == 0){ 
break
if (empty_space_copy > 0){ 
answers.push_back(-1); 
else 
answers.push_back(time); 
 
// 원상복구 
empty_space_copy = empty_space; 
Copy2arr(); 
 
int main(){ 
ios_base::sync_with_stdio(0); 
cin.tie(0); 
cout.tie(0); 
 
cin >> N >> M; 
for (int i = 0; i < N; i++){ 
for (int j = 0; j < N; j++){ 
cin >> arr[i][j]; 
Copy[i][j] = arr[i][j]; 
if (arr[i][j] == 0){ 
empty_space++
empty_space_copy++
else if (arr[i][j] == 2){ 
num_whole_virus++
virus.push_back(make_pair(i, j)); 
if (empty_space == 0){ 
cout << 0 << '\n'
return 0
for (int i = 0; i < M; i++){ 
combination[i] = 1
sort(combination.begin(), combination.end()); 
 
do
bfs(); 
while (next_permutation(combination.begin(), combination.end())); 
sort(answers.begin(), answers.end()); 
for (int i = 0; i < answers.size(); i++){ 
if (answers[i] != -1
cout << answers[i] << '\n'
return 0
cout << -1 << '\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