forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17142.cpp
More file actions
126 lines (111 loc) · 2.54 KB
/
Copy path17142.cpp
File metadata and controls
126 lines (111 loc) · 2.54 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <cmath>
#include <list>
#include <vector>
using namespace std;
char board[50][50];
bool visited[50][50];
int N, M;
int ans = 2500;
vector<pair<int, int>> v;
bool include[11];
list<pair<pair<int, int>, int>> bfsQueue;
bool checkVirusSpread()
{
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (board[i][j] == '0' && !visited[i][j])
return false;
}
}
return true;
}
void resetVisited()
{
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
visited[i][j] = false;
}
}
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int brute(int n, int selected)
{
if (selected == M)
{
resetVisited();
bfsQueue.clear();
for (int i = 0; i < 11; i++)
{
if (include[i])
{
bfsQueue.push_front({{v[i].first, v[i].second}, 0});
}
}
int time = 0;
while (!bfsQueue.empty())
{
int x = bfsQueue.front().first.first;
int y = bfsQueue.front().first.second;
int t = bfsQueue.front().second;
bfsQueue.pop_front();
for (int i = 0; i < 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if (tx >= 0 && ty >= 0 && tx < N && ty < N && board[tx][ty] != '1' && !visited[tx][ty])
{
if (board[tx][ty] == '0')
time = max(time, t + 1);
bfsQueue.push_back({{tx, ty}, t + 1});
visited[tx][ty] = true;
}
}
}
if (checkVirusSpread())
return time;
else
return 5000;
}
if (n + 1 >= v.size())
return 5000;
int res = 5000;
include[n + 1] = true;
res = min(res, brute(n + 1, selected + 1));
include[n + 1] = false;
res = min(res, brute(n + 1, selected));
return res;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int i, j;
cin >> N >> M;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
cin >> board[i][j];
if (board[i][j] == '2')
v.push_back({i, j});
}
}
int ans = 5000;
include[0] = true;
ans = min(ans, brute(0, 1));
include[0] = false;
ans = min(ans, brute(0, 0));
if (ans == 5000)
cout << "-1\n";
else
cout << ans << "\n";
return 0;
}