-
Notifications
You must be signed in to change notification settings - Fork 0
/
17142_실패.cpp
96 lines (94 loc) · 2.54 KB
/
17142_실패.cpp
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
// https://www.acmicpc.net/problem/17142
// 바이러스가 퍼지는 방식을 이해하지 못한듯.. 이해한대로 구현
#include <iostream>
#include <vector>
#include <queue>
#define BLANK 0
#define WALL 1
#define VIRUS 2
using namespace std;
int answer = 100000;
void spread(vector<vector<int>> l, int sec) {
int y, x;
bool done = true;
queue<pair<int,int>> q, tmp;
vector<vector<int>> copied(l.begin(), l.end());
for (int i = 0; i < l.size(); i++) {
for (int j = 0; j < l.size(); j++) {
if (l[i][j] == 0 || l[i][j] == 2) done = false;
if (j - 1 >= 0 && l[i][j - 1] != WALL) copied[i][j - 1] = 3;
if (l[i][j] == 3) {
q.push({i,j});
}
}
}
while(q.empty() == 0) {
y = q.front().first;
x = q.front().second;
q.pop();
if (y + 1 < l.size()) {
if(copied[y+1][x] == BLANK) { q.push({y+1,x}); copied[y + 1][x] = 3; }
else if(copied[y+1][x] == 2) tmp.push({y+1,x});
}
if (x + 1 < l.size()) {
if(copied[y][x+1] == BLANK) { q.push({y,x+1}); copied[y][x+1] = 3; }
else if(copied[y][x+1] == 2) tmp.push({y,x+1});
}
if (y-1 >= 0) {
if(copied[y-1][x] == BLANK) { q.push({y-1,x}); copied[y-1][x] = 3; }
else if(copied[y-1][x] == 2) tmp.push({y-1,x});
}
if (x-1 >= 0) {
if(copied[y][x-1] == BLANK) { q.push({y,x-1}); copied[y][x-1] = 3; }
else if(copied[y][x-1] == 2) tmp.push({y,x-1});
}
}
while(tmp.empty() == 0) {
copied[tmp.front().first][tmp.front().second] = 3;
tmp.pop();
}
if (done == true) {
if (sec < answer) answer = sec;
return;
}
else spread(copied, sec + 1);
}
void choose(vector<vector<int>>& l, vector<pair<int, int>>& v, vector<pair<int, int>>& tmp, int j, int cnt) {
if (cnt >= tmp.size()) {
for (int i = 0; i < tmp.size(); i++) {
l[tmp[i].first][tmp[i].second] = 3;
printf("%d %d \n", tmp[i].first, tmp[i].second);
}
printf("\n");
spread(l, 0);
printf("%d초 걸림\n", answer);
for (int i = 0; i < tmp.size(); i++)
l[tmp[i].first][tmp[i].second] = 2;
return;
}
for (int i = j + 1; i < v.size(); i++) {
tmp[cnt] = { v[i].first, v[i].second };
choose(l, v, tmp, i, cnt + 1);
}
}
void solution(vector<vector<int>>& l, vector<pair<int, int>>& v, int m) {
printf("활성 %d/%d\n", m, v.size());
vector<pair<int, int>> tmp(m);
choose(l, v, tmp, -1, 0);
}
int main() {
int n, m;
cin.tie(0);
cin >> n >> m;
vector<vector<int>> lab(n, vector<int>(n));
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> lab[i][j];
if (lab[i][j] == VIRUS) v.push_back({ i,j });
}
}
solution(lab, v, m);
cout << answer;
return 0;
}