#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);
vvi bee;
vector<string>tab;
int n, k;
pii start;
int best[801][801];
bool isViable(int target){
if(bee[start.f][start.s] <= target) return false;
memset(best, -1, sizeof best);
queue<pair<pii, pii>>q;
q.push({{target, k}, start});
best[start.f][start.s] = bee[start.f][start.s] - target;
while(!q.empty()){
auto &[T, curr] = q.front();
auto &[t, steps] = T;
q.pop();
if(tab[curr.f][curr.s] == 'D') return (best[curr.f][curr.s] > 0);
for(int i = 0; i < 4; i++){
auto [x, y] = make_pair(curr.f + dx[i], curr.s + 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(best[curr.f][curr.s], bee[x][y] - (steps?t:t+1))){
best[x][y] = min(best[curr.f][curr.s], bee[x][y] - (steps?t:t+1));
q.push({{(steps)?t:t+1, (steps?steps-1:k-1)}, {x, y}});
}
}
}
return false;
}
void fx() {
// Functionality for fx
cin >> n >> k;
tab.assign(n, "");
bee.assign(n, vi(n, INT_MAX));
vii hives;
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' or tab[X][Y] == 'D') and bee[X][Y] > t + 1){
q.push({t+1, {X, Y}});
bee[X][Y] = t+1;
}
}
}
return;
if(!isViable(0)){
cout << "-1" << endl;
return;
}
int lo = 0;
int hi = 805;
while(hi > lo){
int mid = (lo + hi +1)/2;
if(isViable(mid)) lo = mid;
else hi = mid-1;
}
cout << lo << 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 |
396 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
392 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
22 ms |
3676 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
26 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
27 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
28 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
30 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
34 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
35 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
36 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
37 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
38 |
Incorrect |
3 ms |
1116 KB |
Output isn't correct |
39 |
Incorrect |
3 ms |
1372 KB |
Output isn't correct |
40 |
Incorrect |
3 ms |
1372 KB |
Output isn't correct |
41 |
Incorrect |
6 ms |
1372 KB |
Output isn't correct |
42 |
Incorrect |
3 ms |
1628 KB |
Output isn't correct |
43 |
Incorrect |
3 ms |
1628 KB |
Output isn't correct |
44 |
Incorrect |
5 ms |
1628 KB |
Output isn't correct |
45 |
Incorrect |
4 ms |
1884 KB |
Output isn't correct |
46 |
Incorrect |
4 ms |
1888 KB |
Output isn't correct |
47 |
Incorrect |
7 ms |
1888 KB |
Output isn't correct |
48 |
Incorrect |
5 ms |
2144 KB |
Output isn't correct |
49 |
Incorrect |
4 ms |
2144 KB |
Output isn't correct |
50 |
Incorrect |
8 ms |
2144 KB |
Output isn't correct |
51 |
Incorrect |
5 ms |
2400 KB |
Output isn't correct |
52 |
Incorrect |
5 ms |
2404 KB |
Output isn't correct |
53 |
Incorrect |
8 ms |
2616 KB |
Output isn't correct |
54 |
Incorrect |
6 ms |
2700 KB |
Output isn't correct |
55 |
Incorrect |
8 ms |
2912 KB |
Output isn't correct |
56 |
Incorrect |
10 ms |
2912 KB |
Output isn't correct |
57 |
Incorrect |
7 ms |
3244 KB |
Output isn't correct |
58 |
Incorrect |
7 ms |
3164 KB |
Output isn't correct |
59 |
Incorrect |
11 ms |
3316 KB |
Output isn't correct |
60 |
Incorrect |
8 ms |
3676 KB |
Output isn't correct |
61 |
Incorrect |
7 ms |
3672 KB |
Output isn't correct |
62 |
Incorrect |
13 ms |
3676 KB |
Output isn't correct |
63 |
Incorrect |
18 ms |
3676 KB |
Output isn't correct |
64 |
Incorrect |
18 ms |
3672 KB |
Output isn't correct |
65 |
Incorrect |
18 ms |
3672 KB |
Output isn't correct |
66 |
Incorrect |
18 ms |
3676 KB |
Output isn't correct |
67 |
Incorrect |
18 ms |
3676 KB |
Output isn't correct |
68 |
Incorrect |
20 ms |
3676 KB |
Output isn't correct |
69 |
Incorrect |
19 ms |
3672 KB |
Output isn't correct |
70 |
Incorrect |
21 ms |
3676 KB |
Output isn't correct |
71 |
Incorrect |
23 ms |
3568 KB |
Output isn't correct |
72 |
Incorrect |
23 ms |
3676 KB |
Output isn't correct |
73 |
Incorrect |
18 ms |
3932 KB |
Output isn't correct |
74 |
Incorrect |
17 ms |
3932 KB |
Output isn't correct |
75 |
Incorrect |
17 ms |
3932 KB |
Output isn't correct |
76 |
Incorrect |
19 ms |
3932 KB |
Output isn't correct |
77 |
Incorrect |
18 ms |
3944 KB |
Output isn't correct |
78 |
Incorrect |
18 ms |
3940 KB |
Output isn't correct |
79 |
Incorrect |
22 ms |
4188 KB |
Output isn't correct |
80 |
Incorrect |
17 ms |
4184 KB |
Output isn't correct |
81 |
Incorrect |
25 ms |
3932 KB |
Output isn't correct |
82 |
Incorrect |
18 ms |
3932 KB |
Output isn't correct |
83 |
Incorrect |
18 ms |
3932 KB |
Output isn't correct |
84 |
Incorrect |
18 ms |
3932 KB |
Output isn't correct |
85 |
Incorrect |
17 ms |
3932 KB |
Output isn't correct |
86 |
Incorrect |
17 ms |
3828 KB |
Output isn't correct |
87 |
Incorrect |
18 ms |
3932 KB |
Output isn't correct |
88 |
Incorrect |
18 ms |
3932 KB |
Output isn't correct |
89 |
Incorrect |
18 ms |
3932 KB |
Output isn't correct |
90 |
Incorrect |
16 ms |
3932 KB |
Output isn't correct |
91 |
Incorrect |
19 ms |
3932 KB |
Output isn't correct |
92 |
Incorrect |
23 ms |
3932 KB |
Output isn't correct |