728x90
https://www.acmicpc.net/problem/17142
17142번: 연구소 3
인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 바이러스는 활성 상태와 비활성 상태가 있다. 가장 처음에 모든 바이러스는 비활성 상태이고
www.acmicpc.net
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
|
#include <iostream>
#include <cstring>
#include <queue>
#define MAX 110
using namespace std;
int t, n, m, k, cnt, samsung;
int arr[MAX][MAX];
int cost[MAX][MAX];
int comResult[MAX];
int _min = 9999;
bool check[MAX][MAX];
bool wall[MAX][MAX];
bool com[MAX];
bool visited[MAX][MAX];
bool space[MAX][MAX];
int dy[4] = { 0, 0, 1, -1 };
int dx[4] = { 1 , -1, 0, 0 };
struct p {
int y;
int x;
};
queue <p> Queue;
void BFS() {
samsung++;
int _max = 0;
bool flag = false;
while (!Queue.empty()) {
p front = Queue.front();
Queue.pop();
int curY = front.y;
int curX = front.x;
visited[curY][curX] = true;
for (int i = 0; i < 4; i++) {
int yy = curY + dy[i];
int xx = curX + dx[i];
if (yy >= 1 && yy <= n && xx >= 1 && xx <= n
&& visited[yy][xx] == false
&& wall[yy][xx] == false
&& check[yy][xx] == false) {
if (cost[yy][xx] == 0) {
cost[yy][xx] = cost[curY][curX] + 1;
if (_max < cost[yy][xx] && space[yy][xx] == true) _max = cost[yy][xx];
}
Queue.push({ yy, xx });
visited[yy][xx] = true;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
//cout<<cost[i][j]<<" ";
}//cout<<endl;
}
//cout<<endl;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (cost[i][j] == 0 && visited[i][j] == false && wall[i][j] == false) {
flag = true;
// cout<<i<<" "<<j<<endl;
}
}
}
if (flag == true) cnt++;
else if (flag == false) {
if (_min > _max) _min = _max;
}
//flag = false;
//cout<<cnt;
memset(cost, 0, sizeof(cost));
memset(visited, 0, sizeof(visited));
memset(check, 0, sizeof(check));
}
void getCombination(int x, int diffuser) {
if (x >= k) {
// for (int i = 0; i < k; i++) {
// cout<<comResult[i]<<" ";
// }cout<<endl;
for (int q = 0; q < k; q++) {
int num;
num = comResult[q];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (wall[i][j] == false) {
if (num == arr[i][j]) {
Queue.push({ i, j });
check[i][j] = true;
}
}
}
}
}
BFS();
}
else {
for (int i = 1; i < diffuser; i++) {
int data = i;
if (com[i] == false) {
if (x > 0 && comResult[x - 1] > data) continue;
comResult[x] = data;
com[i] = true;
getCombination(x + 1, diffuser);
com[i] = false;
comResult[x] = 0;
}
}
}
}
int main() {
// t = 1;
cin >> n >> k;
int diffuser = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> arr[i][j];
if (arr[i][j] == 0) space[i][j] = true;
else if (arr[i][j] == 1 && wall[i][j] == false) {
wall[i][j] = true;
//cost[i][j] = -1;
}
else if (arr[i][j] == 2 && check[i][j] == false) {
arr[i][j] = diffuser;
diffuser++;
//check[i][j] = true;
}
}
}
//cout<<diffuser;
getCombination(0, diffuser);
// cout<<samsung;
if (cnt >= samsung) cout << "-1" << endl;
else cout << _min << endl;
return 0;
}
/*
2
4 4 2
0 1 0 2
2 0 1 0
0 0 0 1
0 0 2 0
4 4 15
0 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
*/
|
728x90
'Algorithm Study > BOJ with C++' 카테고리의 다른 글
(BAEKJOON) 17837번: 새로운 게임2 (0) | 2020.07.08 |
---|---|
(BAEKJOON) 17779번: 게리맨더링 2 (0) | 2020.07.08 |
(BAEKJOON) 17140번: 이차원 배열과 연산 (0) | 2020.07.08 |
(BAEKJOON) 17143번: 낚시왕 (0) | 2020.07.08 |
(BAEKJOON) 17144번: 미세먼지 안녕! (0) | 2020.07.08 |
댓글