#include <bits/stdc++.h>
using namespace std;
#ifdef local
#include "debug.hpp"
#define pr(...) debug(#__VA_ARGS__, __VA_ARGS__)
#define prs(...) debug_nameless(__VA_ARGS__)
#else
#define pr(...) 69
#define prs(...) 69
#endif
#define endl '\n'
const int inf = 1e9;
const long long binf = 1e18;
void solve(int tc){
int n, s;
cin >> n >> s;
int st = -1, ed = -1;
vector <int> bees;
auto get_hash = [&](int i, int j){
return n*i + j;
};
vector <string> grid(n);
for(int i = 0; i < n; i++){
cin >> grid[i];
for(int j = 0; j < n; j++){
int hash = get_hash(i, j);
if(grid[i][j] == 'M') st = hash;
if(grid[i][j] == 'D') ed = hash;
if(grid[i][j] == 'H') bees.push_back(hash);
}
}
assert(bees.size() > 0 && st != -1 && ed != -1);
auto check = [&](long long t){
vector <vector<long long>> bdist(n, vector<long long>(n, binf));
queue <int> queue;
for(int b : bees){
queue.push(b);
bdist[b/n][b%n] = 0;
}
auto inside = [&](int l, int c){
return (min(l, c) >= 0 && max(l, c) < n && (grid[l][c] == 'G' || grid[l][c] == 'D' || grid[l][c] == 'M'));
};
while(queue.size() > 0){
int node = queue.front();
queue.pop();
int l = node/n, c = node%n;
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
if(i*j != 0) continue;
int delta = (bdist[l][c] < t ? 1 : s);
if(inside(l+i, c+j) && grid[l+i][c+j] != 'D' && bdist[l+i][c+j] > delta + bdist[l][c]){
queue.push(get_hash(l+i, c+j));
bdist[l+i][c+j] = delta + bdist[l][c];
}
}
}
}
while(queue.size()) queue.pop();
vector <vector<long long>> dist(n, vector<long long>(n, binf));
dist[st/n][st%n] = t;
if(bdist[st/n][st%n] <= dist[st/n][st%n]) return false;
queue.push(st);
while(queue.size() > 0){
int node = queue.front();
queue.pop();
int l = node/n, c = node%n;
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
if(i*j != 0) continue;
if(inside(l+i, c+j) && bdist[l+i][c+j] > 1 + dist[l][c] && 1+dist[l][c] < dist[l+i][c+j]){
queue.push(get_hash(l+i, c+j));
dist[l+i][c+j] = 1 + dist[l][c];
}
}
}
}
int l = ed/n, c = ed%n;
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
if(i*j != 0) continue;
if(inside(l+i, c+j) && dist[l+i][c+j] < bdist[l+i][c+j]){
return true;
}
}
}
return false;
};
long long l, r = 1e15;
long long ans = -1;
while(l <= r){
long long mid = (l + r)/2;
if(check(mid)){
l = mid+1;
ans = mid;
}else{
r = mid-1;
}
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int tt = 1;
while(tt--){
prs(string(50, '-'));
solve(tt);
prs(string(50, '-'));
}
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:10:20: warning: statement has no effect [-Wunused-value]
10 | #define prs(...) 69
| ^~
mecho.cpp:111:5: note: in expansion of macro 'prs'
111 | prs(string(50, '-'));
| ^~~
mecho.cpp:10:20: warning: statement has no effect [-Wunused-value]
10 | #define prs(...) 69
| ^~
mecho.cpp:113:5: note: in expansion of macro 'prs'
113 | prs(string(50, '-'));
| ^~~
mecho.cpp: In function 'void solve(int)':
mecho.cpp:95:24: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
95 | long long mid = (l + r)/2;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Execution timed out |
1026 ms |
11268 KB |
Time limit exceeded |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
528 KB |
Output is correct |
13 |
Correct |
3 ms |
348 KB |
Output is correct |
14 |
Correct |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
344 KB |
Output is correct |
28 |
Correct |
4 ms |
344 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
31 |
Correct |
4 ms |
348 KB |
Output is correct |
32 |
Correct |
4 ms |
572 KB |
Output is correct |
33 |
Correct |
93 ms |
2576 KB |
Output is correct |
34 |
Correct |
93 ms |
2592 KB |
Output is correct |
35 |
Correct |
146 ms |
2540 KB |
Output is correct |
36 |
Correct |
123 ms |
3284 KB |
Output is correct |
37 |
Correct |
120 ms |
3200 KB |
Output is correct |
38 |
Correct |
191 ms |
3168 KB |
Output is correct |
39 |
Correct |
149 ms |
3928 KB |
Output is correct |
40 |
Correct |
147 ms |
3844 KB |
Output is correct |
41 |
Correct |
245 ms |
3892 KB |
Output is correct |
42 |
Correct |
186 ms |
4884 KB |
Output is correct |
43 |
Correct |
181 ms |
4744 KB |
Output is correct |
44 |
Correct |
304 ms |
4688 KB |
Output is correct |
45 |
Correct |
219 ms |
5676 KB |
Output is correct |
46 |
Correct |
225 ms |
5604 KB |
Output is correct |
47 |
Correct |
378 ms |
5592 KB |
Output is correct |
48 |
Correct |
267 ms |
6564 KB |
Output is correct |
49 |
Correct |
273 ms |
6560 KB |
Output is correct |
50 |
Correct |
459 ms |
6752 KB |
Output is correct |
51 |
Correct |
307 ms |
7616 KB |
Output is correct |
52 |
Correct |
325 ms |
7608 KB |
Output is correct |
53 |
Correct |
533 ms |
7592 KB |
Output is correct |
54 |
Correct |
383 ms |
8732 KB |
Output is correct |
55 |
Correct |
376 ms |
8760 KB |
Output is correct |
56 |
Correct |
636 ms |
8728 KB |
Output is correct |
57 |
Correct |
432 ms |
9964 KB |
Output is correct |
58 |
Correct |
444 ms |
10000 KB |
Output is correct |
59 |
Correct |
719 ms |
9952 KB |
Output is correct |
60 |
Correct |
505 ms |
11272 KB |
Output is correct |
61 |
Correct |
509 ms |
11448 KB |
Output is correct |
62 |
Correct |
823 ms |
11436 KB |
Output is correct |
63 |
Execution timed out |
1055 ms |
11348 KB |
Time limit exceeded |
64 |
Execution timed out |
1042 ms |
11236 KB |
Time limit exceeded |
65 |
Execution timed out |
1047 ms |
11528 KB |
Time limit exceeded |
66 |
Execution timed out |
1024 ms |
11332 KB |
Time limit exceeded |
67 |
Execution timed out |
1032 ms |
11228 KB |
Time limit exceeded |
68 |
Execution timed out |
1058 ms |
11216 KB |
Time limit exceeded |
69 |
Execution timed out |
1058 ms |
11240 KB |
Time limit exceeded |
70 |
Execution timed out |
1069 ms |
11248 KB |
Time limit exceeded |
71 |
Execution timed out |
1025 ms |
11240 KB |
Time limit exceeded |
72 |
Execution timed out |
1067 ms |
11424 KB |
Time limit exceeded |
73 |
Correct |
969 ms |
11280 KB |
Output is correct |
74 |
Correct |
968 ms |
11260 KB |
Output is correct |
75 |
Execution timed out |
1008 ms |
11272 KB |
Time limit exceeded |
76 |
Correct |
986 ms |
11452 KB |
Output is correct |
77 |
Execution timed out |
1018 ms |
11460 KB |
Time limit exceeded |
78 |
Correct |
966 ms |
11260 KB |
Output is correct |
79 |
Correct |
965 ms |
11272 KB |
Output is correct |
80 |
Correct |
953 ms |
11268 KB |
Output is correct |
81 |
Correct |
980 ms |
11268 KB |
Output is correct |
82 |
Correct |
977 ms |
11308 KB |
Output is correct |
83 |
Execution timed out |
1026 ms |
11392 KB |
Time limit exceeded |
84 |
Execution timed out |
1010 ms |
11280 KB |
Time limit exceeded |
85 |
Correct |
992 ms |
11276 KB |
Output is correct |
86 |
Execution timed out |
1010 ms |
11364 KB |
Time limit exceeded |
87 |
Execution timed out |
1006 ms |
11276 KB |
Time limit exceeded |
88 |
Execution timed out |
1006 ms |
11260 KB |
Time limit exceeded |
89 |
Correct |
993 ms |
11344 KB |
Output is correct |
90 |
Correct |
992 ms |
11332 KB |
Output is correct |
91 |
Execution timed out |
1039 ms |
11256 KB |
Time limit exceeded |
92 |
Execution timed out |
1008 ms |
11412 KB |
Time limit exceeded |