#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
#define fast_io ios::sync_with_stdio(0), cin.tie(0)
#define pb push_back
template <typename T> using vt = vector<T>;
using vi = vt<int>;
using vs = vt<string>;
const int inf = 0x3f3f3f3f;
int main() {
fast_io;
int N, n, d; cin >> N >> d, n = N*N;
vs s(N); int srt, fin; vi hiv;
for(int i = 0; i < N; i++) {
cin >> s[i];
for(int j = 0; j < N; j++) {
int x = i*N+j;
if(s[i][j] == 'M') srt = x;
if(s[i][j] == 'D') fin = x;
if(s[i][j] == 'H') hiv.pb(x);
}
}
queue<int> q; vi hdis(n, inf), dis; int f;
auto put = [&] (int x, int y, int c) {
if(c == 0) {
if(hdis[y] < inf) return;
int a = y/N, b = y-a*N;
if(s[a][b] == 'T' || s[a][b] == 'D') return;
q.push(y), hdis[y] = hdis[x]+1;
} else
if(c == 1) {
if(dis[y] < inf) return;
if(y == fin) {
q.push(y), dis[y] = 0;
return;
}
int a = y/N, b = y-a*N;
if(s[a][b] == 'T') return;
int tim = f+(dis[x]+1)/d;
if(hdis[y] <= tim) return;
q.push(y), dis[y] = dis[x]+1;
}
};
auto bfs = [&] (int c) {
while(!q.empty()) {
int x = q.front(); q.pop();
if(x == fin) {
while(!q.empty()) q.pop();
return;
}
if(x%N > 0) put(x, x-1, c);
if(x%N<N-1) put(x, x+1, c);
if(x/N > 0) put(x, x-N, c);
if(x/N<N-1) put(x, x+N, c);
}
};
for(int x: hiv) q.push(x), hdis[x] = 0;
bfs(0);
auto poss = [&] (int m) {
if(m >= hdis[srt]) return 0;
q.push(srt), dis = vi(n, inf), dis[srt] = 0, f = m;
bfs(1);
return dis[fin] < inf? 1: 0;
};
int l = -1, r = inf, m;
while(l < r) {
m = l+r+1 >> 1;
if(poss(m)) l = m; else
r = m-1;
}
cout << l << "\n";
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:81:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | m = l+r+1 >> 1;
| ~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
106 ms |
9344 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 |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
452 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
0 ms |
456 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
452 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
4 ms |
2140 KB |
Output is correct |
34 |
Correct |
5 ms |
2052 KB |
Output is correct |
35 |
Correct |
28 ms |
2068 KB |
Output is correct |
36 |
Correct |
5 ms |
2676 KB |
Output is correct |
37 |
Correct |
5 ms |
2676 KB |
Output is correct |
38 |
Correct |
36 ms |
2676 KB |
Output is correct |
39 |
Correct |
6 ms |
3164 KB |
Output is correct |
40 |
Correct |
7 ms |
3256 KB |
Output is correct |
41 |
Correct |
47 ms |
3260 KB |
Output is correct |
42 |
Correct |
8 ms |
3928 KB |
Output is correct |
43 |
Correct |
8 ms |
3932 KB |
Output is correct |
44 |
Correct |
58 ms |
3904 KB |
Output is correct |
45 |
Correct |
9 ms |
4636 KB |
Output is correct |
46 |
Correct |
10 ms |
4636 KB |
Output is correct |
47 |
Correct |
74 ms |
4624 KB |
Output is correct |
48 |
Correct |
11 ms |
5340 KB |
Output is correct |
49 |
Correct |
12 ms |
5456 KB |
Output is correct |
50 |
Correct |
90 ms |
5332 KB |
Output is correct |
51 |
Correct |
12 ms |
6256 KB |
Output is correct |
52 |
Correct |
13 ms |
6220 KB |
Output is correct |
53 |
Correct |
106 ms |
6292 KB |
Output is correct |
54 |
Correct |
15 ms |
7136 KB |
Output is correct |
55 |
Correct |
16 ms |
7124 KB |
Output is correct |
56 |
Correct |
129 ms |
7108 KB |
Output is correct |
57 |
Correct |
17 ms |
8236 KB |
Output is correct |
58 |
Correct |
17 ms |
8196 KB |
Output is correct |
59 |
Correct |
142 ms |
8220 KB |
Output is correct |
60 |
Correct |
19 ms |
9308 KB |
Output is correct |
61 |
Correct |
21 ms |
9296 KB |
Output is correct |
62 |
Correct |
174 ms |
9344 KB |
Output is correct |
63 |
Correct |
128 ms |
9156 KB |
Output is correct |
64 |
Correct |
205 ms |
9304 KB |
Output is correct |
65 |
Correct |
204 ms |
9256 KB |
Output is correct |
66 |
Correct |
159 ms |
9308 KB |
Output is correct |
67 |
Correct |
132 ms |
9152 KB |
Output is correct |
68 |
Correct |
52 ms |
9296 KB |
Output is correct |
69 |
Correct |
47 ms |
9300 KB |
Output is correct |
70 |
Correct |
46 ms |
9296 KB |
Output is correct |
71 |
Correct |
40 ms |
9296 KB |
Output is correct |
72 |
Correct |
31 ms |
9296 KB |
Output is correct |
73 |
Correct |
39 ms |
9316 KB |
Output is correct |
74 |
Correct |
70 ms |
9424 KB |
Output is correct |
75 |
Correct |
86 ms |
9436 KB |
Output is correct |
76 |
Correct |
67 ms |
9412 KB |
Output is correct |
77 |
Correct |
77 ms |
9412 KB |
Output is correct |
78 |
Correct |
85 ms |
9412 KB |
Output is correct |
79 |
Correct |
60 ms |
9420 KB |
Output is correct |
80 |
Correct |
73 ms |
9240 KB |
Output is correct |
81 |
Correct |
89 ms |
9384 KB |
Output is correct |
82 |
Correct |
74 ms |
9400 KB |
Output is correct |
83 |
Correct |
92 ms |
9356 KB |
Output is correct |
84 |
Correct |
83 ms |
9520 KB |
Output is correct |
85 |
Correct |
82 ms |
9412 KB |
Output is correct |
86 |
Correct |
90 ms |
9384 KB |
Output is correct |
87 |
Correct |
86 ms |
9400 KB |
Output is correct |
88 |
Correct |
95 ms |
9372 KB |
Output is correct |
89 |
Correct |
94 ms |
9352 KB |
Output is correct |
90 |
Correct |
102 ms |
9380 KB |
Output is correct |
91 |
Correct |
93 ms |
9300 KB |
Output is correct |
92 |
Correct |
97 ms |
9408 KB |
Output is correct |