#include <bits/stdc++.h>
using namespace std;
template<typename T> void dout(string name, T arg) {
cerr << name << " = " << arg << "\e[39m" << endl;
}
template<typename T1, typename... T2> void dout(string names, T1 arg, T2... args) {
cerr << names.substr(0, names.find(',')) << " = " << arg << " | ";
dout(names.substr(names.find(',') + 2), args...);
}
#define debug(...) cerr << "\e[91m" << "[" << __LINE__ << ":" << __func__ << "]=> ", dout(#__VA_ARGS__, __VA_ARGS__);
const pair<int, int> dir[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
long long n, s;
vector<vector<char>> arr;
vector<pair<int, int>> hives;
pair<int, int> m, d;
queue<pair<int, int>> q;
bool move(int x, int y) {
return x >= 0 && y >= 0 && x < n && y < n;
}
bool check(long long tim, vector<vector<long long>>& dist, vector<vector<long long>>& swarm) {
vector<vector<bool>> visited(n, vector<bool>(n, false));
while (q.size()) q.pop();
q.push(m);
visited[m.first][m.second] = true;
while (!q.empty()) {
auto [x, y] = q.front();
if (q.front() == d) return true;
q.pop();
for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && (arr[x + xx][y + yy] == 'G' ||
arr[x + xx][y + yy] == 'D') && !visited[x + xx][y + yy] && (dist[x + xx][y + yy] + tim < swarm[x + xx][y + yy] ||
(dist[x + xx][y + yy] + tim == swarm[x + xx][y + yy] && dist[d.first][d.second] == dist[x + xx][y + yy]))) {
q.push({x + xx, y + yy});
visited[x + xx][y + yy] = true;
}
}
return false;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s;
arr.resize(n, vector<char> (n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
if (arr[i][j] == 'M') {
m = {i, j};
}
if (arr[i][j] == 'H') {
hives.push_back({i, j});
}
if (arr[i][j] == 'D') {
d = {i, j};
}
}
}
vector<vector<long long>> dist(n, vector<long long>(n, INT_MAX)), swarm(n, vector<long long>(n, INT_MAX));
dist[m.first][m.second] = 0;
q.push(m);
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && (arr[x + xx][y + yy] == 'G' ||
arr[x + xx][y + yy] == 'D') && dist[x + xx][y + yy] > dist[x][y] + 1) {
dist[x + xx][y + yy] = dist[x][y] + 1;
q.push({x + xx, y + yy});
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = dist[i][j] / s + (dist[i][j] % s != 0);
}
}
for (auto [x, y] : hives) {
swarm[x][y] = 0;
q.push({x, y});
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && arr[x + xx][y + yy] == 'G' && swarm[x + xx][y + yy] > swarm[x][y] + 1) {
swarm[x + xx][y + yy] = swarm[x][y] + 1;
q.push({x + xx, y + yy});
}
}
long long l = 0, h = 640000;
while (h - l > 1) {
int mid = (h + l) / 2;
if (check(mid, dist, swarm)) l = mid;
else h = mid;
}
cout << l << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'bool check(long long int, std::vector<std::vector<long long int> >&, std::vector<std::vector<long long int> >&)':
mecho.cpp:29:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | auto [x, y] = q.front();
| ^
mecho.cpp:32:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && (arr[x + xx][y + yy] == 'G' ||
| ^
mecho.cpp: In function 'int main()':
mecho.cpp:65:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
65 | auto [x, y] = q.front();
| ^
mecho.cpp:67:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
67 | for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && (arr[x + xx][y + yy] == 'G' ||
| ^
mecho.cpp:78:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
78 | for (auto [x, y] : hives) {
| ^
mecho.cpp:83:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | auto [x, y] = q.front();
| ^
mecho.cpp:85:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
85 | for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && arr[x + xx][y + yy] == 'G' && swarm[x + xx][y + yy] > swarm[x][y] + 1) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
112 ms |
11176 KB |
Output is correct |
8 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
7 ms |
2388 KB |
Output is correct |
34 |
Correct |
7 ms |
2388 KB |
Output is correct |
35 |
Incorrect |
24 ms |
2436 KB |
Output isn't correct |
36 |
Correct |
11 ms |
3072 KB |
Output is correct |
37 |
Correct |
12 ms |
3156 KB |
Output is correct |
38 |
Incorrect |
30 ms |
3028 KB |
Output isn't correct |
39 |
Correct |
10 ms |
3740 KB |
Output is correct |
40 |
Correct |
11 ms |
3668 KB |
Output is correct |
41 |
Incorrect |
39 ms |
3744 KB |
Output isn't correct |
42 |
Correct |
15 ms |
4564 KB |
Output is correct |
43 |
Correct |
14 ms |
4564 KB |
Output is correct |
44 |
Incorrect |
52 ms |
4544 KB |
Output isn't correct |
45 |
Correct |
19 ms |
5424 KB |
Output is correct |
46 |
Correct |
16 ms |
5416 KB |
Output is correct |
47 |
Incorrect |
61 ms |
5532 KB |
Output isn't correct |
48 |
Correct |
19 ms |
6356 KB |
Output is correct |
49 |
Correct |
23 ms |
6376 KB |
Output is correct |
50 |
Incorrect |
84 ms |
6440 KB |
Output isn't correct |
51 |
Correct |
22 ms |
7500 KB |
Output is correct |
52 |
Correct |
20 ms |
7508 KB |
Output is correct |
53 |
Incorrect |
90 ms |
7380 KB |
Output isn't correct |
54 |
Correct |
27 ms |
8532 KB |
Output is correct |
55 |
Correct |
26 ms |
8532 KB |
Output is correct |
56 |
Incorrect |
120 ms |
8636 KB |
Output isn't correct |
57 |
Correct |
34 ms |
9748 KB |
Output is correct |
58 |
Correct |
28 ms |
9768 KB |
Output is correct |
59 |
Incorrect |
130 ms |
9856 KB |
Output isn't correct |
60 |
Correct |
36 ms |
11092 KB |
Output is correct |
61 |
Correct |
35 ms |
11056 KB |
Output is correct |
62 |
Incorrect |
154 ms |
11184 KB |
Output isn't correct |
63 |
Correct |
129 ms |
11220 KB |
Output is correct |
64 |
Incorrect |
209 ms |
11084 KB |
Output isn't correct |
65 |
Incorrect |
233 ms |
11172 KB |
Output isn't correct |
66 |
Correct |
172 ms |
11176 KB |
Output is correct |
67 |
Incorrect |
142 ms |
11168 KB |
Output isn't correct |
68 |
Correct |
77 ms |
11068 KB |
Output is correct |
69 |
Incorrect |
67 ms |
11076 KB |
Output isn't correct |
70 |
Incorrect |
67 ms |
11068 KB |
Output isn't correct |
71 |
Incorrect |
80 ms |
11056 KB |
Output isn't correct |
72 |
Incorrect |
66 ms |
11080 KB |
Output isn't correct |
73 |
Incorrect |
95 ms |
11344 KB |
Output isn't correct |
74 |
Correct |
84 ms |
11348 KB |
Output is correct |
75 |
Correct |
100 ms |
11336 KB |
Output is correct |
76 |
Correct |
105 ms |
11332 KB |
Output is correct |
77 |
Correct |
134 ms |
11344 KB |
Output is correct |
78 |
Incorrect |
107 ms |
11320 KB |
Output isn't correct |
79 |
Correct |
71 ms |
11320 KB |
Output is correct |
80 |
Correct |
84 ms |
11328 KB |
Output is correct |
81 |
Correct |
124 ms |
11320 KB |
Output is correct |
82 |
Correct |
87 ms |
11312 KB |
Output is correct |
83 |
Correct |
118 ms |
11276 KB |
Output is correct |
84 |
Correct |
108 ms |
11208 KB |
Output is correct |
85 |
Correct |
99 ms |
11276 KB |
Output is correct |
86 |
Correct |
116 ms |
11272 KB |
Output is correct |
87 |
Correct |
115 ms |
11200 KB |
Output is correct |
88 |
Correct |
111 ms |
11320 KB |
Output is correct |
89 |
Correct |
114 ms |
11220 KB |
Output is correct |
90 |
Correct |
117 ms |
11208 KB |
Output is correct |
91 |
Correct |
112 ms |
11244 KB |
Output is correct |
92 |
Correct |
128 ms |
11196 KB |
Output is correct |