#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 == INT_MIN)?-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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
432 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 |
187 ms |
7304 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
0 ms |
436 KB |
Output isn't correct |
18 |
Correct |
1 ms |
600 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 |
348 KB |
Output isn't correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Incorrect |
0 ms |
452 KB |
Output isn't correct |
24 |
Correct |
0 ms |
388 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 |
0 ms |
348 KB |
Output isn't correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Incorrect |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
33 |
Incorrect |
4 ms |
1584 KB |
Output isn't correct |
34 |
Correct |
5 ms |
1720 KB |
Output is correct |
35 |
Correct |
177 ms |
1924 KB |
Output is correct |
36 |
Incorrect |
5 ms |
1880 KB |
Output isn't correct |
37 |
Correct |
6 ms |
1884 KB |
Output is correct |
38 |
Correct |
269 ms |
2344 KB |
Output is correct |
39 |
Incorrect |
7 ms |
2392 KB |
Output isn't correct |
40 |
Correct |
8 ms |
2396 KB |
Output is correct |
41 |
Correct |
399 ms |
2976 KB |
Output is correct |
42 |
Incorrect |
8 ms |
3160 KB |
Output isn't correct |
43 |
Correct |
10 ms |
3164 KB |
Output is correct |
44 |
Correct |
535 ms |
3476 KB |
Output is correct |
45 |
Incorrect |
10 ms |
3676 KB |
Output isn't correct |
46 |
Correct |
12 ms |
3672 KB |
Output is correct |
47 |
Correct |
734 ms |
4568 KB |
Output is correct |
48 |
Incorrect |
12 ms |
4188 KB |
Output isn't correct |
49 |
Correct |
13 ms |
4092 KB |
Output is correct |
50 |
Correct |
965 ms |
5256 KB |
Output is correct |
51 |
Incorrect |
13 ms |
4700 KB |
Output isn't correct |
52 |
Correct |
16 ms |
4700 KB |
Output is correct |
53 |
Execution timed out |
1055 ms |
5700 KB |
Time limit exceeded |
54 |
Incorrect |
16 ms |
5464 KB |
Output isn't correct |
55 |
Correct |
24 ms |
5304 KB |
Output is correct |
56 |
Execution timed out |
1006 ms |
6724 KB |
Time limit exceeded |
57 |
Incorrect |
18 ms |
5980 KB |
Output isn't correct |
58 |
Correct |
21 ms |
5980 KB |
Output is correct |
59 |
Execution timed out |
1095 ms |
7536 KB |
Time limit exceeded |
60 |
Incorrect |
20 ms |
6732 KB |
Output isn't correct |
61 |
Correct |
23 ms |
6908 KB |
Output is correct |
62 |
Execution timed out |
1041 ms |
8256 KB |
Time limit exceeded |
63 |
Incorrect |
34 ms |
6740 KB |
Output isn't correct |
64 |
Incorrect |
43 ms |
6748 KB |
Output isn't correct |
65 |
Incorrect |
41 ms |
6684 KB |
Output isn't correct |
66 |
Incorrect |
38 ms |
6736 KB |
Output isn't correct |
67 |
Correct |
32 ms |
6740 KB |
Output is correct |
68 |
Correct |
33 ms |
6736 KB |
Output is correct |
69 |
Correct |
34 ms |
6732 KB |
Output is correct |
70 |
Correct |
32 ms |
6748 KB |
Output is correct |
71 |
Correct |
38 ms |
6736 KB |
Output is correct |
72 |
Correct |
36 ms |
6724 KB |
Output is correct |
73 |
Correct |
34 ms |
6924 KB |
Output is correct |
74 |
Incorrect |
499 ms |
7508 KB |
Output isn't correct |
75 |
Correct |
161 ms |
6996 KB |
Output is correct |
76 |
Correct |
190 ms |
7308 KB |
Output is correct |
77 |
Correct |
180 ms |
7252 KB |
Output is correct |
78 |
Correct |
56 ms |
7180 KB |
Output is correct |
79 |
Incorrect |
105 ms |
7012 KB |
Output isn't correct |
80 |
Incorrect |
59 ms |
6992 KB |
Output isn't correct |
81 |
Correct |
39 ms |
6736 KB |
Output is correct |
82 |
Incorrect |
66 ms |
6996 KB |
Output isn't correct |
83 |
Correct |
81 ms |
7128 KB |
Output is correct |
84 |
Correct |
320 ms |
7376 KB |
Output is correct |
85 |
Correct |
254 ms |
7004 KB |
Output is correct |
86 |
Correct |
217 ms |
7276 KB |
Output is correct |
87 |
Correct |
145 ms |
6996 KB |
Output is correct |
88 |
Correct |
288 ms |
7504 KB |
Output is correct |
89 |
Correct |
588 ms |
7344 KB |
Output is correct |
90 |
Correct |
146 ms |
7248 KB |
Output is correct |
91 |
Correct |
467 ms |
7744 KB |
Output is correct |
92 |
Correct |
343 ms |
7620 KB |
Output is correct |