#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
using ii = pair<int,int>;
int N, S;
vector<int> dx = {1,0,-1,0}, dy = {0,1,0,-1};
pair<int,int> p_st, p_en;
bool in_bounds(pair<int,int> p){
return 0 <= p.f && p.f < N && 0 <= p.s && p.s < N;
}
queue<ii> push_bees(vector<vector<int>>& G, queue<ii> Q){
queue<ii> Q2;
while (!Q.empty()){
ii cur = Q.front(); Q.pop();
for (int d=0;d<4;++d){
ii nxt = {cur.f+dx[d],cur.s+dy[d]};
if (!in_bounds(nxt)) continue;
if (nxt == p_en) continue;
if (G[nxt.f][nxt.s] == 0 || (G[nxt.f][nxt.s] == 2)){
G[nxt.f][nxt.s] = -1;
Q2.push(nxt);
}
}
}
return Q2;
}
queue<ii> push_mecho(vector<vector<int>>& G, queue<ii> Q){
queue<ii> Q2;
while (!Q.empty()){
ii cur = Q.front(); Q.pop();
if (G[cur.f][cur.s] != 2) continue;
for (int d=0;d<4;++d){
ii nxt = {cur.f+dx[d],cur.s+dy[d]};
if (!in_bounds(nxt)) continue;
if (G[nxt.f][nxt.s] == 0){
G[nxt.f][nxt.s] = 2;
Q2.push(nxt);
}
}
}
return Q2;
}
bool good(int t, vector<vector<int>> G){
queue<ii> Q; // bees
for(int i=0;i<N;++i) for(int j=0;j<N;++j) if (G[i][j] == -1) Q.push({i,j});
for(int i=0;i<t;++i) Q = push_bees(G,Q);
queue<ii> Q2; Q2.push(p_st); // mecho
while(!Q2.empty()){
for (int i=0;i<S;++i) Q2 = push_mecho(G,Q2);
Q = push_bees(G,Q);
}
return G[p_en.f][p_en.s] == 2;
}
int main(){
cin >> N >> S;
vector<vector<int>> G(N,vector<int>(N,0));
for(int i=0;i<N;++i){
string st;
cin >> st;
for(int j=0;j<N;++j){
if (st[j]=='T'){
G[i][j] = 1;
}
if (st[j] == 'G'){
G[i][j] = 0;
}
if (st[j] == 'M'){
p_st = {i,j};
G[i][j] = 2;
}
if (st[j] == 'D'){
p_en = {i,j};
G[i][j] = 0;
}
if (st[j] == 'H'){
G[i][j] = -1;
}
}
}
if (!good(0,G)){
cout << -1 << endl; return 0;
}
int lo = 0, hi = 1e6;
while (lo < hi){
int mid = (lo+hi+1)/2;
if (good(mid,G)){
lo = mid;
}
else {
hi = mid-1;
}
}
cout << lo << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
348 KB |
Output is correct |
2 |
Correct |
64 ms |
344 KB |
Output is correct |
3 |
Correct |
69 ms |
408 KB |
Output is correct |
4 |
Correct |
80 ms |
416 KB |
Output is correct |
5 |
Correct |
83 ms |
348 KB |
Output is correct |
6 |
Correct |
66 ms |
348 KB |
Output is correct |
7 |
Correct |
346 ms |
5720 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
87 ms |
416 KB |
Output is correct |
10 |
Correct |
65 ms |
348 KB |
Output is correct |
11 |
Correct |
67 ms |
412 KB |
Output is correct |
12 |
Correct |
66 ms |
344 KB |
Output is correct |
13 |
Correct |
69 ms |
440 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
69 ms |
416 KB |
Output is correct |
16 |
Correct |
66 ms |
416 KB |
Output is correct |
17 |
Correct |
68 ms |
344 KB |
Output is correct |
18 |
Correct |
67 ms |
348 KB |
Output is correct |
19 |
Correct |
81 ms |
348 KB |
Output is correct |
20 |
Correct |
66 ms |
348 KB |
Output is correct |
21 |
Correct |
68 ms |
348 KB |
Output is correct |
22 |
Correct |
76 ms |
348 KB |
Output is correct |
23 |
Correct |
68 ms |
432 KB |
Output is correct |
24 |
Correct |
68 ms |
436 KB |
Output is correct |
25 |
Correct |
67 ms |
440 KB |
Output is correct |
26 |
Correct |
67 ms |
348 KB |
Output is correct |
27 |
Correct |
67 ms |
448 KB |
Output is correct |
28 |
Correct |
88 ms |
444 KB |
Output is correct |
29 |
Correct |
70 ms |
344 KB |
Output is correct |
30 |
Correct |
68 ms |
452 KB |
Output is correct |
31 |
Correct |
69 ms |
344 KB |
Output is correct |
32 |
Correct |
67 ms |
344 KB |
Output is correct |
33 |
Correct |
151 ms |
1372 KB |
Output is correct |
34 |
Correct |
121 ms |
1392 KB |
Output is correct |
35 |
Correct |
98 ms |
1372 KB |
Output is correct |
36 |
Correct |
191 ms |
1628 KB |
Output is correct |
37 |
Correct |
138 ms |
1628 KB |
Output is correct |
38 |
Correct |
116 ms |
1704 KB |
Output is correct |
39 |
Correct |
208 ms |
1884 KB |
Output is correct |
40 |
Correct |
152 ms |
1884 KB |
Output is correct |
41 |
Correct |
121 ms |
2024 KB |
Output is correct |
42 |
Correct |
300 ms |
2640 KB |
Output is correct |
43 |
Correct |
174 ms |
2392 KB |
Output is correct |
44 |
Correct |
136 ms |
2392 KB |
Output is correct |
45 |
Correct |
288 ms |
2812 KB |
Output is correct |
46 |
Correct |
200 ms |
2648 KB |
Output is correct |
47 |
Correct |
156 ms |
2648 KB |
Output is correct |
48 |
Correct |
334 ms |
3164 KB |
Output is correct |
49 |
Correct |
227 ms |
3276 KB |
Output is correct |
50 |
Correct |
183 ms |
3416 KB |
Output is correct |
51 |
Correct |
385 ms |
3676 KB |
Output is correct |
52 |
Correct |
276 ms |
3760 KB |
Output is correct |
53 |
Correct |
196 ms |
3644 KB |
Output is correct |
54 |
Correct |
445 ms |
4188 KB |
Output is correct |
55 |
Correct |
292 ms |
4184 KB |
Output is correct |
56 |
Correct |
252 ms |
4188 KB |
Output is correct |
57 |
Correct |
499 ms |
4944 KB |
Output is correct |
58 |
Correct |
319 ms |
4700 KB |
Output is correct |
59 |
Correct |
227 ms |
4880 KB |
Output is correct |
60 |
Correct |
559 ms |
5484 KB |
Output is correct |
61 |
Correct |
354 ms |
5480 KB |
Output is correct |
62 |
Correct |
249 ms |
5468 KB |
Output is correct |
63 |
Correct |
316 ms |
5536 KB |
Output is correct |
64 |
Correct |
288 ms |
5512 KB |
Output is correct |
65 |
Correct |
306 ms |
5484 KB |
Output is correct |
66 |
Correct |
321 ms |
5468 KB |
Output is correct |
67 |
Correct |
23 ms |
5468 KB |
Output is correct |
68 |
Correct |
323 ms |
5552 KB |
Output is correct |
69 |
Correct |
298 ms |
5564 KB |
Output is correct |
70 |
Correct |
304 ms |
5464 KB |
Output is correct |
71 |
Correct |
300 ms |
5552 KB |
Output is correct |
72 |
Correct |
367 ms |
5556 KB |
Output is correct |
73 |
Correct |
335 ms |
6224 KB |
Output is correct |
74 |
Correct |
341 ms |
6044 KB |
Output is correct |
75 |
Correct |
350 ms |
6068 KB |
Output is correct |
76 |
Correct |
349 ms |
6068 KB |
Output is correct |
77 |
Correct |
338 ms |
6036 KB |
Output is correct |
78 |
Correct |
29 ms |
5780 KB |
Output is correct |
79 |
Correct |
387 ms |
6144 KB |
Output is correct |
80 |
Correct |
339 ms |
5976 KB |
Output is correct |
81 |
Correct |
341 ms |
6016 KB |
Output is correct |
82 |
Correct |
343 ms |
6024 KB |
Output is correct |
83 |
Correct |
329 ms |
6176 KB |
Output is correct |
84 |
Correct |
362 ms |
5920 KB |
Output is correct |
85 |
Correct |
353 ms |
5724 KB |
Output is correct |
86 |
Correct |
345 ms |
5720 KB |
Output is correct |
87 |
Correct |
346 ms |
5928 KB |
Output is correct |
88 |
Correct |
325 ms |
5824 KB |
Output is correct |
89 |
Correct |
348 ms |
5968 KB |
Output is correct |
90 |
Correct |
339 ms |
5832 KB |
Output is correct |
91 |
Correct |
355 ms |
5840 KB |
Output is correct |
92 |
Correct |
329 ms |
5720 KB |
Output is correct |