#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 (G[nxt.f][nxt.s] == 0 || (G[nxt.f][nxt.s] == 2 && nxt != p_en)){
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 |
Incorrect |
66 ms |
348 KB |
Output isn't correct |
3 |
Correct |
72 ms |
596 KB |
Output is correct |
4 |
Incorrect |
67 ms |
348 KB |
Output isn't correct |
5 |
Correct |
66 ms |
408 KB |
Output is correct |
6 |
Incorrect |
65 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
378 ms |
5732 KB |
Output isn't correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
66 ms |
348 KB |
Output is correct |
10 |
Correct |
68 ms |
348 KB |
Output is correct |
11 |
Correct |
67 ms |
348 KB |
Output is correct |
12 |
Incorrect |
72 ms |
348 KB |
Output isn't correct |
13 |
Correct |
73 ms |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
66 ms |
416 KB |
Output is correct |
16 |
Correct |
73 ms |
348 KB |
Output is correct |
17 |
Correct |
66 ms |
344 KB |
Output is correct |
18 |
Correct |
66 ms |
348 KB |
Output is correct |
19 |
Correct |
66 ms |
348 KB |
Output is correct |
20 |
Correct |
65 ms |
424 KB |
Output is correct |
21 |
Correct |
67 ms |
348 KB |
Output is correct |
22 |
Correct |
68 ms |
344 KB |
Output is correct |
23 |
Correct |
67 ms |
348 KB |
Output is correct |
24 |
Correct |
69 ms |
344 KB |
Output is correct |
25 |
Correct |
69 ms |
344 KB |
Output is correct |
26 |
Correct |
66 ms |
348 KB |
Output is correct |
27 |
Correct |
68 ms |
348 KB |
Output is correct |
28 |
Correct |
67 ms |
348 KB |
Output is correct |
29 |
Correct |
71 ms |
348 KB |
Output is correct |
30 |
Correct |
71 ms |
452 KB |
Output is correct |
31 |
Correct |
69 ms |
460 KB |
Output is correct |
32 |
Correct |
83 ms |
460 KB |
Output is correct |
33 |
Correct |
149 ms |
1368 KB |
Output is correct |
34 |
Correct |
121 ms |
1616 KB |
Output is correct |
35 |
Correct |
99 ms |
1368 KB |
Output is correct |
36 |
Correct |
178 ms |
1628 KB |
Output is correct |
37 |
Correct |
136 ms |
1628 KB |
Output is correct |
38 |
Correct |
125 ms |
1696 KB |
Output is correct |
39 |
Correct |
212 ms |
1880 KB |
Output is correct |
40 |
Correct |
151 ms |
1880 KB |
Output is correct |
41 |
Correct |
123 ms |
2028 KB |
Output is correct |
42 |
Correct |
248 ms |
2408 KB |
Output is correct |
43 |
Correct |
178 ms |
2392 KB |
Output is correct |
44 |
Correct |
144 ms |
2396 KB |
Output is correct |
45 |
Correct |
309 ms |
2896 KB |
Output is correct |
46 |
Correct |
198 ms |
2812 KB |
Output is correct |
47 |
Correct |
156 ms |
2652 KB |
Output is correct |
48 |
Correct |
341 ms |
3276 KB |
Output is correct |
49 |
Correct |
249 ms |
3276 KB |
Output is correct |
50 |
Correct |
183 ms |
3408 KB |
Output is correct |
51 |
Correct |
389 ms |
3760 KB |
Output is correct |
52 |
Correct |
270 ms |
3772 KB |
Output is correct |
53 |
Correct |
190 ms |
3768 KB |
Output is correct |
54 |
Correct |
517 ms |
4436 KB |
Output is correct |
55 |
Correct |
319 ms |
4300 KB |
Output is correct |
56 |
Correct |
225 ms |
4316 KB |
Output is correct |
57 |
Correct |
540 ms |
4860 KB |
Output is correct |
58 |
Correct |
329 ms |
4856 KB |
Output is correct |
59 |
Correct |
249 ms |
4872 KB |
Output is correct |
60 |
Correct |
631 ms |
5488 KB |
Output is correct |
61 |
Correct |
359 ms |
5468 KB |
Output is correct |
62 |
Correct |
249 ms |
5464 KB |
Output is correct |
63 |
Correct |
290 ms |
5492 KB |
Output is correct |
64 |
Correct |
292 ms |
5464 KB |
Output is correct |
65 |
Correct |
308 ms |
5716 KB |
Output is correct |
66 |
Correct |
320 ms |
5488 KB |
Output is correct |
67 |
Correct |
23 ms |
5468 KB |
Output is correct |
68 |
Correct |
322 ms |
5560 KB |
Output is correct |
69 |
Correct |
276 ms |
5468 KB |
Output is correct |
70 |
Correct |
301 ms |
5556 KB |
Output is correct |
71 |
Correct |
292 ms |
5716 KB |
Output is correct |
72 |
Correct |
359 ms |
5464 KB |
Output is correct |
73 |
Correct |
311 ms |
6084 KB |
Output is correct |
74 |
Correct |
331 ms |
6064 KB |
Output is correct |
75 |
Correct |
370 ms |
6200 KB |
Output is correct |
76 |
Correct |
337 ms |
6076 KB |
Output is correct |
77 |
Correct |
352 ms |
6264 KB |
Output is correct |
78 |
Correct |
24 ms |
5980 KB |
Output is correct |
79 |
Correct |
337 ms |
5996 KB |
Output is correct |
80 |
Correct |
360 ms |
6272 KB |
Output is correct |
81 |
Correct |
358 ms |
5976 KB |
Output is correct |
82 |
Correct |
352 ms |
6024 KB |
Output is correct |
83 |
Correct |
335 ms |
5896 KB |
Output is correct |
84 |
Correct |
351 ms |
5924 KB |
Output is correct |
85 |
Correct |
338 ms |
5720 KB |
Output is correct |
86 |
Correct |
370 ms |
5928 KB |
Output is correct |
87 |
Correct |
341 ms |
5724 KB |
Output is correct |
88 |
Correct |
358 ms |
5840 KB |
Output is correct |
89 |
Correct |
331 ms |
5824 KB |
Output is correct |
90 |
Correct |
353 ms |
5888 KB |
Output is correct |
91 |
Correct |
332 ms |
5724 KB |
Output is correct |
92 |
Correct |
330 ms |
5724 KB |
Output is correct |