#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);
vector <vector<int>> bdist(n, vector<int>(n, inf));
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'));
};
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) && grid[l+i][c+j] != 'D' && bdist[l+i][c+j] > s + bdist[l][c]){
queue.push(get_hash(l+i, c+j));
bdist[l+i][c+j] = s + bdist[l][c];
}
}
}
}
auto check = [&](int t){
while(queue.size()) queue.pop();
vector <vector<int>> dist(n, vector<int>(n, inf));
dist[st/n][st%n] = s*t;
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;
};
int l = 0, r = 1e6;
int ans = -1;
while(l <= r){
int 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:109:5: note: in expansion of macro 'prs'
109 | prs(string(50, '-'));
| ^~~
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, '-'));
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
Correct |
90 ms |
6904 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is 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 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
5 ms |
1692 KB |
Output is correct |
34 |
Correct |
6 ms |
1688 KB |
Output is correct |
35 |
Correct |
23 ms |
1732 KB |
Output is correct |
36 |
Correct |
7 ms |
2108 KB |
Output is correct |
37 |
Correct |
7 ms |
2144 KB |
Output is correct |
38 |
Correct |
35 ms |
2084 KB |
Output is correct |
39 |
Correct |
12 ms |
2496 KB |
Output is correct |
40 |
Correct |
8 ms |
2488 KB |
Output is correct |
41 |
Correct |
37 ms |
2496 KB |
Output is correct |
42 |
Correct |
15 ms |
2948 KB |
Output is correct |
43 |
Correct |
15 ms |
3220 KB |
Output is correct |
44 |
Correct |
57 ms |
2976 KB |
Output is correct |
45 |
Correct |
15 ms |
3516 KB |
Output is correct |
46 |
Correct |
12 ms |
3480 KB |
Output is correct |
47 |
Correct |
59 ms |
3472 KB |
Output is correct |
48 |
Correct |
14 ms |
4100 KB |
Output is correct |
49 |
Correct |
25 ms |
4104 KB |
Output is correct |
50 |
Correct |
77 ms |
4064 KB |
Output is correct |
51 |
Correct |
18 ms |
4684 KB |
Output is correct |
52 |
Correct |
19 ms |
4684 KB |
Output is correct |
53 |
Correct |
96 ms |
4664 KB |
Output is correct |
54 |
Correct |
21 ms |
5408 KB |
Output is correct |
55 |
Correct |
28 ms |
5376 KB |
Output is correct |
56 |
Correct |
116 ms |
5340 KB |
Output is correct |
57 |
Correct |
29 ms |
6088 KB |
Output is correct |
58 |
Correct |
28 ms |
6284 KB |
Output is correct |
59 |
Correct |
147 ms |
6108 KB |
Output is correct |
60 |
Correct |
23 ms |
6892 KB |
Output is correct |
61 |
Correct |
33 ms |
6876 KB |
Output is correct |
62 |
Correct |
125 ms |
6876 KB |
Output is correct |
63 |
Correct |
110 ms |
6864 KB |
Output is correct |
64 |
Correct |
162 ms |
6860 KB |
Output is correct |
65 |
Correct |
176 ms |
6864 KB |
Output is correct |
66 |
Correct |
132 ms |
6856 KB |
Output is correct |
67 |
Correct |
115 ms |
6872 KB |
Output is correct |
68 |
Correct |
50 ms |
6916 KB |
Output is correct |
69 |
Correct |
51 ms |
6876 KB |
Output is correct |
70 |
Correct |
63 ms |
6876 KB |
Output is correct |
71 |
Correct |
48 ms |
6884 KB |
Output is correct |
72 |
Incorrect |
51 ms |
6876 KB |
Output isn't correct |
73 |
Incorrect |
93 ms |
6764 KB |
Output isn't correct |
74 |
Correct |
69 ms |
6876 KB |
Output is correct |
75 |
Correct |
74 ms |
6800 KB |
Output is correct |
76 |
Correct |
89 ms |
6904 KB |
Output is correct |
77 |
Correct |
73 ms |
6888 KB |
Output is correct |
78 |
Correct |
73 ms |
6892 KB |
Output is correct |
79 |
Correct |
64 ms |
6896 KB |
Output is correct |
80 |
Correct |
67 ms |
6916 KB |
Output is correct |
81 |
Correct |
77 ms |
6924 KB |
Output is correct |
82 |
Correct |
63 ms |
6892 KB |
Output is correct |
83 |
Correct |
84 ms |
6868 KB |
Output is correct |
84 |
Correct |
77 ms |
6888 KB |
Output is correct |
85 |
Correct |
85 ms |
6896 KB |
Output is correct |
86 |
Correct |
111 ms |
6888 KB |
Output is correct |
87 |
Correct |
87 ms |
6888 KB |
Output is correct |
88 |
Correct |
87 ms |
6876 KB |
Output is correct |
89 |
Correct |
110 ms |
6856 KB |
Output is correct |
90 |
Correct |
93 ms |
6892 KB |
Output is correct |
91 |
Correct |
91 ms |
6896 KB |
Output is correct |
92 |
Correct |
94 ms |
6876 KB |
Output is correct |