#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"));
// 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 |
344 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
176 ms |
6424 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
27 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Incorrect |
4 ms |
1372 KB |
Output isn't correct |
34 |
Correct |
5 ms |
1372 KB |
Output is correct |
35 |
Correct |
178 ms |
1752 KB |
Output is correct |
36 |
Incorrect |
6 ms |
1884 KB |
Output isn't correct |
37 |
Correct |
6 ms |
1884 KB |
Output is correct |
38 |
Correct |
268 ms |
2112 KB |
Output is correct |
39 |
Incorrect |
6 ms |
2140 KB |
Output isn't correct |
40 |
Correct |
8 ms |
2140 KB |
Output is correct |
41 |
Correct |
390 ms |
2812 KB |
Output is correct |
42 |
Incorrect |
8 ms |
2908 KB |
Output isn't correct |
43 |
Correct |
9 ms |
2784 KB |
Output is correct |
44 |
Correct |
550 ms |
3452 KB |
Output is correct |
45 |
Incorrect |
10 ms |
3164 KB |
Output isn't correct |
46 |
Correct |
11 ms |
3164 KB |
Output is correct |
47 |
Correct |
734 ms |
4032 KB |
Output is correct |
48 |
Incorrect |
12 ms |
3676 KB |
Output isn't correct |
49 |
Correct |
13 ms |
3676 KB |
Output is correct |
50 |
Correct |
942 ms |
4768 KB |
Output is correct |
51 |
Incorrect |
13 ms |
4384 KB |
Output isn't correct |
52 |
Correct |
15 ms |
4188 KB |
Output is correct |
53 |
Execution timed out |
1092 ms |
5580 KB |
Time limit exceeded |
54 |
Incorrect |
15 ms |
4956 KB |
Output isn't correct |
55 |
Correct |
18 ms |
4956 KB |
Output is correct |
56 |
Execution timed out |
1063 ms |
5884 KB |
Time limit exceeded |
57 |
Incorrect |
19 ms |
5464 KB |
Output isn't correct |
58 |
Correct |
20 ms |
5464 KB |
Output is correct |
59 |
Execution timed out |
1038 ms |
6780 KB |
Time limit exceeded |
60 |
Incorrect |
20 ms |
6236 KB |
Output isn't correct |
61 |
Correct |
25 ms |
6236 KB |
Output is correct |
62 |
Execution timed out |
1062 ms |
7616 KB |
Time limit exceeded |
63 |
Incorrect |
31 ms |
6236 KB |
Output isn't correct |
64 |
Incorrect |
43 ms |
6280 KB |
Output isn't correct |
65 |
Incorrect |
40 ms |
6060 KB |
Output isn't correct |
66 |
Incorrect |
38 ms |
6068 KB |
Output isn't correct |
67 |
Correct |
34 ms |
6224 KB |
Output is correct |
68 |
Correct |
33 ms |
6236 KB |
Output is correct |
69 |
Correct |
34 ms |
6260 KB |
Output is correct |
70 |
Correct |
31 ms |
6112 KB |
Output is correct |
71 |
Correct |
31 ms |
6112 KB |
Output is correct |
72 |
Correct |
37 ms |
6116 KB |
Output is correct |
73 |
Correct |
35 ms |
6232 KB |
Output is correct |
74 |
Incorrect |
522 ms |
6992 KB |
Output isn't correct |
75 |
Correct |
164 ms |
6560 KB |
Output is correct |
76 |
Correct |
205 ms |
6736 KB |
Output is correct |
77 |
Correct |
182 ms |
6480 KB |
Output is correct |
78 |
Correct |
49 ms |
6232 KB |
Output is correct |
79 |
Incorrect |
106 ms |
6376 KB |
Output isn't correct |
80 |
Incorrect |
66 ms |
6484 KB |
Output isn't correct |
81 |
Correct |
38 ms |
6176 KB |
Output is correct |
82 |
Incorrect |
75 ms |
6228 KB |
Output isn't correct |
83 |
Correct |
79 ms |
6224 KB |
Output is correct |
84 |
Correct |
326 ms |
6992 KB |
Output is correct |
85 |
Correct |
230 ms |
6480 KB |
Output is correct |
86 |
Correct |
234 ms |
6480 KB |
Output is correct |
87 |
Correct |
145 ms |
6428 KB |
Output is correct |
88 |
Correct |
280 ms |
6572 KB |
Output is correct |
89 |
Correct |
538 ms |
7232 KB |
Output is correct |
90 |
Correct |
141 ms |
6224 KB |
Output is correct |
91 |
Correct |
489 ms |
7168 KB |
Output is correct |
92 |
Correct |
333 ms |
6740 KB |
Output is correct |