-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17142.cpp
More file actions
106 lines (85 loc) Β· 1.7 KB
/
Copy path17142.cpp
File metadata and controls
106 lines (85 loc) Β· 1.7 KB
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
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int N,M;
int map[51][51] = {0,};
vector<pair<int, int> > virus;
int check[11] = {0,};
int dy[4] = {-1,1,0,0};
int dx[4] = {0,0,-1,1};
int min_sec = 987654321;
int Empty = 0;
int go(){
queue<pair<int,int> > q;
int sec[51][51];
memset(sec, -1, sizeof(sec));
int size = virus.size();
for(int k=0; k<size; k++){
if(check[k]){
q.push(make_pair(virus[k].first, virus[k].second));
sec[virus[k].first][virus[k].second] = 0;
}
}
int infect = 0;
int total_time = 0;
while(!q.empty()){
int y = q.front().first;
int x = q.front().second;
q.pop();
for(int i=0; i<4; i++){
int ny = y + dy[i];
int nx = x + dx[i];
if((ny<0) || (nx<0) || (ny>=N) || (nx>=N))
continue;
if((map[ny][nx] == 0) && sec[ny][nx]==-1){
sec[ny][nx] = sec[y][x] + 1;
q.push(make_pair(ny, nx));
infect++;
total_time = sec[ny][nx];
}
if((map[ny][nx] == 2) && sec[ny][nx]==-1){
sec[ny][nx] = sec[y][x] + 1;
q.push(make_pair(ny, nx));
}
}
}
if(Empty == infect)
return total_time;
return min_sec;
}
void dfs(int idx, int cnt){
if(cnt == M){
int sec = go();
min_sec = min(min_sec, sec);
return;
}
int size = virus.size();
for(int i=idx+1; i<size; i++){
check[i] = 1;
dfs(i, cnt+1);
check[i] = 0;
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>N>>M;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
cin>>map[i][j];
if(map[i][j] == 0)
Empty++;
if(map[i][j] == 2)
virus.push_back(make_pair(i, j));
}
}
dfs(-1, 0);
if(min_sec == 987654321)
cout<<"-1\n";
else
cout<<min_sec<<"\n";
return 0;
}