#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;
int res = INT_MIN;
while(!q.empty()){
auto [T, curr] = q.front();
auto [t, steps] = T;
q.pop();
if(tab[curr.f][curr.s] == 'D') res = (best[curr.f][curr.s]);
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] == 'G' or tab[x][y] == 'D') 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 (res >= 0);
}
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;
}
}
}
if(!isViable(0)){
cout << "-1" << endl;
return;
}
int lo = 0;
int hi = n*n+100;
while(hi >= lo){
int mid = (lo + hi )/2;
if(isViable(mid)) lo = mid + 1;
else hi = mid-1;
}
cout << (lo-2) << 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
7 |
Incorrect |
120 ms |
6352 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
2904 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Incorrect |
1 ms |
2904 KB |
Output isn't correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
30 |
Correct |
1 ms |
2908 KB |
Output is correct |
31 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Incorrect |
4 ms |
3420 KB |
Output isn't correct |
34 |
Correct |
5 ms |
3416 KB |
Output is correct |
35 |
Correct |
950 ms |
4024 KB |
Output is correct |
36 |
Incorrect |
5 ms |
3676 KB |
Output isn't correct |
37 |
Correct |
6 ms |
3800 KB |
Output is correct |
38 |
Execution timed out |
1077 ms |
4008 KB |
Time limit exceeded |
39 |
Incorrect |
6 ms |
3932 KB |
Output isn't correct |
40 |
Correct |
8 ms |
3932 KB |
Output is correct |
41 |
Execution timed out |
1073 ms |
4276 KB |
Time limit exceeded |
42 |
Incorrect |
7 ms |
4188 KB |
Output isn't correct |
43 |
Correct |
9 ms |
4240 KB |
Output is correct |
44 |
Execution timed out |
1048 ms |
4592 KB |
Time limit exceeded |
45 |
Incorrect |
8 ms |
4440 KB |
Output isn't correct |
46 |
Correct |
11 ms |
4444 KB |
Output is correct |
47 |
Execution timed out |
1062 ms |
4892 KB |
Time limit exceeded |
48 |
Incorrect |
10 ms |
4804 KB |
Output isn't correct |
49 |
Correct |
12 ms |
4696 KB |
Output is correct |
50 |
Execution timed out |
1058 ms |
5308 KB |
Time limit exceeded |
51 |
Incorrect |
10 ms |
4956 KB |
Output isn't correct |
52 |
Correct |
14 ms |
4912 KB |
Output is correct |
53 |
Execution timed out |
1046 ms |
5820 KB |
Time limit exceeded |
54 |
Incorrect |
12 ms |
5212 KB |
Output isn't correct |
55 |
Correct |
17 ms |
5212 KB |
Output is correct |
56 |
Execution timed out |
1072 ms |
6316 KB |
Time limit exceeded |
57 |
Incorrect |
14 ms |
5724 KB |
Output isn't correct |
58 |
Correct |
19 ms |
5724 KB |
Output is correct |
59 |
Execution timed out |
1078 ms |
6764 KB |
Time limit exceeded |
60 |
Incorrect |
15 ms |
5976 KB |
Output isn't correct |
61 |
Correct |
21 ms |
5980 KB |
Output is correct |
62 |
Execution timed out |
1077 ms |
7312 KB |
Time limit exceeded |
63 |
Incorrect |
22 ms |
5976 KB |
Output isn't correct |
64 |
Incorrect |
142 ms |
6192 KB |
Output isn't correct |
65 |
Incorrect |
92 ms |
6008 KB |
Output isn't correct |
66 |
Incorrect |
78 ms |
5980 KB |
Output isn't correct |
67 |
Correct |
23 ms |
6232 KB |
Output is correct |
68 |
Correct |
42 ms |
6256 KB |
Output is correct |
69 |
Correct |
45 ms |
6232 KB |
Output is correct |
70 |
Correct |
36 ms |
6480 KB |
Output is correct |
71 |
Correct |
27 ms |
6228 KB |
Output is correct |
72 |
Incorrect |
27 ms |
6236 KB |
Output isn't correct |
73 |
Incorrect |
41 ms |
6616 KB |
Output isn't correct |
74 |
Incorrect |
268 ms |
6488 KB |
Output isn't correct |
75 |
Correct |
354 ms |
6624 KB |
Output is correct |
76 |
Correct |
361 ms |
6612 KB |
Output is correct |
77 |
Correct |
249 ms |
6656 KB |
Output is correct |
78 |
Correct |
28 ms |
6492 KB |
Output is correct |
79 |
Incorrect |
71 ms |
6492 KB |
Output isn't correct |
80 |
Incorrect |
60 ms |
6492 KB |
Output isn't correct |
81 |
Correct |
96 ms |
6384 KB |
Output is correct |
82 |
Incorrect |
52 ms |
6492 KB |
Output isn't correct |
83 |
Correct |
103 ms |
6492 KB |
Output is correct |
84 |
Correct |
230 ms |
6500 KB |
Output is correct |
85 |
Correct |
884 ms |
6580 KB |
Output is correct |
86 |
Execution timed out |
1055 ms |
6492 KB |
Time limit exceeded |
87 |
Correct |
193 ms |
6740 KB |
Output is correct |
88 |
Correct |
648 ms |
6228 KB |
Output is correct |
89 |
Execution timed out |
1027 ms |
6436 KB |
Time limit exceeded |
90 |
Correct |
120 ms |
6436 KB |
Output is correct |
91 |
Correct |
197 ms |
6264 KB |
Output is correct |
92 |
Correct |
144 ms |
6232 KB |
Output is correct |