#pragma GCC optimize("03,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
// Shortcuts for common operations
#define pb push_back
#define ppb pop_back
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define endl "\n"
// Type definitions for convenience
typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef unordered_set<int> usi;
typedef unordered_map<int, int> umii;
// Debugging macros
#define debug(x) cerr << #x << " = " << (x) << '\n'
#define debug_vector(v) _debug_vector(#v, v)
template<typename T>
void _debug_vector(const string& name, const vector<T>& a) {
cerr << name << " = [ ";
for(const auto &x : a) cerr << x << ' ';
cerr << "]\n";
}
// I/O redirection for local testing
#define iofile(io) \
freopen((io + ".in").c_str(), "r", stdin); \
freopen((io + ".out").c_str(), "w", stdout);
// delta for floodfill
vi dx = {0, 1, 0, -1};
vi dy = {1, 0, -1, 0};
// extended deltas for floodfill
vi edx = {0, 1, 0, -1, 1, 1, -1, -1};
vi edy = {1, 0, -1, 0, 1, -1, 1, -1};
// Common outputs
void yes() { cout << "YES" << '\n'; }
void no() { cout << "NO" << '\n'; }
// cin.tie(0)->sync_with_stdio(0);
void fx() {
// Functionality for fx
int n, k;
cin >> n >> k;
vector<string>tab(n);
vvi bee(n, vi(n, INT_MAX));
vii hives;
pii start;
for(int i = 0; i < n; i++){
cin >> tab[i];
for(int j = 0; j < n; j++){
if(tab[i][j] == 'M') start = {i, j};
else if(tab[i][j] == 'H') hives.pb({i, j});
}
}
queue<pair<int, pii>>q;
for(auto &hive: hives){
q.push({0, hive});
bee[hive.f][hive.s] = 0;
}
while(!q.empty()){
auto &[t, hive] = q.front();
q.pop();
auto &[x, y] = hive;
for(int i = 0; i < 4; i++){
auto [X, Y] = make_pair(x+dx[i], y+dy[i]);
if(X >= 0 and X < n and Y >= 0 and Y < n and (tab[X][Y] == 'G' or tab[X][Y] == 'M') and bee[X][Y] > t + 1){
q.push({t+1, {X, Y}});
bee[X][Y] = t+1;
}
}
}
queue<pair<array<int, 3>, pii>>que;
vector<vi>best(n, vi(n, INT_MIN)); // <- see init.
que.push({{0, bee[start.f][start.s], k}, {start.f, start.s}}); //<- sure about init?
best[start.f][start.s] = bee[start.f][start.s];
int res = INT_MIN;
while(!que.empty()){
auto [T, C] = que.front();
que.pop();
auto [t, s, steps] = T;
auto [x, y] = C;
if(tab[x][y] == 'D') res = max(res, best[x][y]);
for(int i = 0; i < 4; i++){
auto [X, Y] = make_pair(x + dx[i], y + dy[i]);
if(X >= 0 and X < n and Y >= 0 and Y < n and tab[X][Y] != 'T' and (bee[X][Y] > (steps?t:t+1)) and best[X][Y] < min(s, bee[X][Y] - (steps?t:t+1))){
que.push({{(steps?t:t+1), min(s, bee[X][Y] - (steps?t:t+1)), (steps?steps-1:k-1)}, {X, Y}});
best[X][Y] = max(best[X][Y], min(s, bee[X][Y] -(steps?t:t+1)));
}
}
}
cout << ((res <= 0)?-1:res-1) << endl;
}
int main() {
// Uncomment the following lines for file I/O
// iofile(string("hello"))
cin.tie(0)->sync_with_stdio(0);
// Uncomment the following lines for multiple test cases
// int t; cin >> t;
// while(t--) fx();
// Single test case
fx();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
162 ms |
6348 KB |
Output is correct |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Incorrect |
2 ms |
1580 KB |
Output isn't correct |
34 |
Correct |
4 ms |
1368 KB |
Output is correct |
35 |
Correct |
178 ms |
1992 KB |
Output is correct |
36 |
Incorrect |
3 ms |
1884 KB |
Output isn't correct |
37 |
Correct |
4 ms |
1884 KB |
Output is correct |
38 |
Correct |
269 ms |
2252 KB |
Output is correct |
39 |
Incorrect |
4 ms |
2140 KB |
Output isn't correct |
40 |
Correct |
5 ms |
2140 KB |
Output is correct |
41 |
Correct |
379 ms |
2900 KB |
Output is correct |
42 |
Incorrect |
5 ms |
2652 KB |
Output isn't correct |
43 |
Correct |
6 ms |
2652 KB |
Output is correct |
44 |
Correct |
509 ms |
3260 KB |
Output is correct |
45 |
Incorrect |
5 ms |
3160 KB |
Output isn't correct |
46 |
Correct |
7 ms |
3164 KB |
Output is correct |
47 |
Correct |
697 ms |
3844 KB |
Output is correct |
48 |
Incorrect |
6 ms |
3676 KB |
Output isn't correct |
49 |
Correct |
8 ms |
3676 KB |
Output is correct |
50 |
Correct |
925 ms |
4688 KB |
Output is correct |
51 |
Incorrect |
7 ms |
4188 KB |
Output isn't correct |
52 |
Correct |
10 ms |
4100 KB |
Output is correct |
53 |
Execution timed out |
1054 ms |
5180 KB |
Time limit exceeded |
54 |
Incorrect |
9 ms |
4700 KB |
Output isn't correct |
55 |
Correct |
13 ms |
4724 KB |
Output is correct |
56 |
Execution timed out |
1079 ms |
5976 KB |
Time limit exceeded |
57 |
Incorrect |
10 ms |
5468 KB |
Output isn't correct |
58 |
Correct |
12 ms |
5468 KB |
Output is correct |
59 |
Execution timed out |
1059 ms |
6484 KB |
Time limit exceeded |
60 |
Incorrect |
11 ms |
5976 KB |
Output isn't correct |
61 |
Correct |
14 ms |
5980 KB |
Output is correct |
62 |
Execution timed out |
1065 ms |
7644 KB |
Time limit exceeded |
63 |
Incorrect |
20 ms |
5976 KB |
Output isn't correct |
64 |
Incorrect |
34 ms |
5980 KB |
Output isn't correct |
65 |
Incorrect |
32 ms |
5980 KB |
Output isn't correct |
66 |
Incorrect |
29 ms |
6000 KB |
Output isn't correct |
67 |
Correct |
21 ms |
6004 KB |
Output is correct |
68 |
Correct |
22 ms |
6236 KB |
Output is correct |
69 |
Correct |
25 ms |
6236 KB |
Output is correct |
70 |
Correct |
26 ms |
5972 KB |
Output is correct |
71 |
Correct |
21 ms |
6236 KB |
Output is correct |
72 |
Correct |
25 ms |
6028 KB |
Output is correct |
73 |
Correct |
23 ms |
6172 KB |
Output is correct |
74 |
Incorrect |
452 ms |
6936 KB |
Output isn't correct |
75 |
Correct |
146 ms |
6484 KB |
Output is correct |
76 |
Correct |
167 ms |
6484 KB |
Output is correct |
77 |
Correct |
160 ms |
6560 KB |
Output is correct |
78 |
Correct |
37 ms |
6164 KB |
Output is correct |
79 |
Incorrect |
83 ms |
6232 KB |
Output isn't correct |
80 |
Incorrect |
45 ms |
6120 KB |
Output isn't correct |
81 |
Correct |
27 ms |
6236 KB |
Output is correct |
82 |
Incorrect |
49 ms |
6328 KB |
Output isn't correct |
83 |
Correct |
66 ms |
6272 KB |
Output is correct |
84 |
Correct |
299 ms |
6536 KB |
Output is correct |
85 |
Correct |
200 ms |
6404 KB |
Output is correct |
86 |
Correct |
193 ms |
6528 KB |
Output is correct |
87 |
Correct |
129 ms |
6236 KB |
Output is correct |
88 |
Correct |
256 ms |
6688 KB |
Output is correct |
89 |
Correct |
475 ms |
6848 KB |
Output is correct |
90 |
Correct |
125 ms |
6356 KB |
Output is correct |
91 |
Correct |
438 ms |
7004 KB |
Output is correct |
92 |
Correct |
328 ms |
7336 KB |
Output is correct |