#pragma GCC optimise("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;
}
Compilation message
mecho.cpp:1: warning: ignoring '#pragma GCC optimise' [-Wunknown-pragmas]
1 | #pragma GCC optimise("03,unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
174 ms |
7032 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 |
1 ms |
348 KB |
Output isn't correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Correct |
0 ms |
356 KB |
Output is correct |
25 |
Incorrect |
0 ms |
356 KB |
Output isn't correct |
26 |
Correct |
0 ms |
360 KB |
Output is correct |
27 |
Incorrect |
1 ms |
360 KB |
Output isn't correct |
28 |
Correct |
0 ms |
360 KB |
Output is correct |
29 |
Incorrect |
1 ms |
356 KB |
Output isn't correct |
30 |
Correct |
0 ms |
468 KB |
Output is correct |
31 |
Incorrect |
1 ms |
352 KB |
Output isn't correct |
32 |
Correct |
0 ms |
356 KB |
Output is correct |
33 |
Incorrect |
3 ms |
1640 KB |
Output isn't correct |
34 |
Correct |
3 ms |
1708 KB |
Output is correct |
35 |
Correct |
176 ms |
1936 KB |
Output is correct |
36 |
Incorrect |
3 ms |
1884 KB |
Output isn't correct |
37 |
Correct |
4 ms |
1888 KB |
Output is correct |
38 |
Correct |
266 ms |
2412 KB |
Output is correct |
39 |
Incorrect |
4 ms |
2404 KB |
Output isn't correct |
40 |
Correct |
5 ms |
2404 KB |
Output is correct |
41 |
Correct |
389 ms |
3308 KB |
Output is correct |
42 |
Incorrect |
5 ms |
2908 KB |
Output isn't correct |
43 |
Correct |
7 ms |
2908 KB |
Output is correct |
44 |
Correct |
534 ms |
3712 KB |
Output is correct |
45 |
Incorrect |
6 ms |
3420 KB |
Output isn't correct |
46 |
Correct |
8 ms |
3304 KB |
Output is correct |
47 |
Correct |
731 ms |
4148 KB |
Output is correct |
48 |
Incorrect |
7 ms |
3928 KB |
Output isn't correct |
49 |
Correct |
9 ms |
3932 KB |
Output is correct |
50 |
Correct |
934 ms |
5052 KB |
Output is correct |
51 |
Incorrect |
8 ms |
4444 KB |
Output isn't correct |
52 |
Correct |
10 ms |
4440 KB |
Output is correct |
53 |
Execution timed out |
1039 ms |
5724 KB |
Time limit exceeded |
54 |
Incorrect |
10 ms |
5212 KB |
Output isn't correct |
55 |
Correct |
13 ms |
5724 KB |
Output is correct |
56 |
Execution timed out |
1040 ms |
6268 KB |
Time limit exceeded |
57 |
Incorrect |
11 ms |
5976 KB |
Output isn't correct |
58 |
Correct |
14 ms |
5980 KB |
Output is correct |
59 |
Execution timed out |
1047 ms |
7504 KB |
Time limit exceeded |
60 |
Incorrect |
12 ms |
6748 KB |
Output isn't correct |
61 |
Correct |
16 ms |
6744 KB |
Output is correct |
62 |
Execution timed out |
1040 ms |
8344 KB |
Time limit exceeded |
63 |
Incorrect |
22 ms |
6736 KB |
Output isn't correct |
64 |
Incorrect |
36 ms |
6744 KB |
Output isn't correct |
65 |
Incorrect |
33 ms |
6612 KB |
Output isn't correct |
66 |
Incorrect |
30 ms |
6748 KB |
Output isn't correct |
67 |
Correct |
23 ms |
6836 KB |
Output is correct |
68 |
Correct |
24 ms |
6664 KB |
Output is correct |
69 |
Correct |
30 ms |
6852 KB |
Output is correct |
70 |
Correct |
24 ms |
6740 KB |
Output is correct |
71 |
Correct |
23 ms |
6740 KB |
Output is correct |
72 |
Correct |
26 ms |
6748 KB |
Output is correct |
73 |
Correct |
28 ms |
6840 KB |
Output is correct |
74 |
Incorrect |
503 ms |
7356 KB |
Output isn't correct |
75 |
Correct |
154 ms |
7060 KB |
Output is correct |
76 |
Correct |
183 ms |
6996 KB |
Output is correct |
77 |
Correct |
171 ms |
6988 KB |
Output is correct |
78 |
Correct |
49 ms |
7268 KB |
Output is correct |
79 |
Incorrect |
91 ms |
6732 KB |
Output isn't correct |
80 |
Incorrect |
51 ms |
6892 KB |
Output isn't correct |
81 |
Correct |
30 ms |
6744 KB |
Output is correct |
82 |
Incorrect |
55 ms |
6736 KB |
Output isn't correct |
83 |
Correct |
70 ms |
6920 KB |
Output is correct |
84 |
Correct |
318 ms |
7236 KB |
Output is correct |
85 |
Correct |
218 ms |
6996 KB |
Output is correct |
86 |
Correct |
216 ms |
7432 KB |
Output is correct |
87 |
Correct |
137 ms |
6996 KB |
Output is correct |
88 |
Correct |
272 ms |
7248 KB |
Output is correct |
89 |
Correct |
542 ms |
7436 KB |
Output is correct |
90 |
Correct |
133 ms |
6976 KB |
Output is correct |
91 |
Correct |
494 ms |
7668 KB |
Output is correct |
92 |
Correct |
324 ms |
7544 KB |
Output is correct |