#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
/*
precalcula p grid o tempo que cada célula leva até ser ocupada por abelhas
binary search no tempo
p verificar se tempo x é válido:
floodfill do mecho começando no tempo x e ignorando s:
continuar só se (dist[i][j]+s-1)/s + x < bee[i][j] ou se (dist[i][j]+s-1)/s + x == bee[i][j] e se nn for o último movimento, = dist[i][j]%s != 0
*/
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // overclock random
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
const int MAXN = 800+10;
bool g[MAXN][MAXN];
int bs[MAXN][MAXN]; // bs=bees
int d[MAXN][MAXN]; // distance (mecho)
int aux[4] = {1, -1, 0, 0};
signed main(){
std::ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
// cout << fixed << setprecision(12);
int n, s;
cin >> n >> s;
char x;
pii st = {0,0}, en = {0,0};
queue<pii> bq;
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++){
cin >> x;
g[i][j] = (x=='G' || x=='M');
bs[i][j] = 1e9;
if (x=='H'){
bq.emplace(i,j);
bs[i][j] = 0;
}
else if (x =='M') st = {i,j};
else if (x == 'D') en = {i, j};
}
while (!bq.empty()){
pii u = bq.front();
bq.pop();
int nd = bs[u.first][u.second]+1;
for (int a=0,b=3;a<4;a++,b--){
int ni = u.first+aux[a], nj = u.second+aux[b];
if (bs[ni][nj] == 1e9 && g[ni][nj]){
bs[ni][nj] = nd;
bq.emplace(ni, nj);
}
}
}
g[en.first][en.second] = g[st.first][st.second] = 1;
bool tru = 0;
int in = 0, fi = 1e9, me;
while (in<=fi){
me = (in+fi)/2;
if (bs[st.first][st.second] <= me){
fi = me-1;
continue;
}
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) d[i][j] = 1e9;
d[st.first][st.second] = 0;
queue<pii> mq;
mq.push(st);
while (!mq.empty()){
pii u = mq.front();
mq.pop();
int nd = d[u.first][u.second]+1;
for (int a=0,b=3;a<4;a++,b--){
int ni = u.first+aux[a], nj = u.second+aux[b];
if (!g[ni][nj] || d[ni][nj] != 1e9) continue;
if (me + (nd+s-1)/s < bs[ni][nj] || (me + (nd+s-1)/s == bs[ni][nj] && nd%s != 0)){
d[ni][nj] = nd;
mq.emplace(ni, nj);
}
}
}
if (d[en.first][en.second] != 1e9){
tru=1, in = me+1;
}else fi = me-1;
}
if (!tru) cout << "-1\n";
else cout << in-1 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
46 ms |
7064 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
716 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
0 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
0 ms |
860 KB |
Output is correct |
33 |
Correct |
4 ms |
2908 KB |
Output is correct |
34 |
Correct |
7 ms |
2908 KB |
Output is correct |
35 |
Correct |
12 ms |
2908 KB |
Output is correct |
36 |
Correct |
4 ms |
3304 KB |
Output is correct |
37 |
Correct |
5 ms |
3420 KB |
Output is correct |
38 |
Correct |
15 ms |
3292 KB |
Output is correct |
39 |
Correct |
5 ms |
3676 KB |
Output is correct |
40 |
Correct |
6 ms |
3696 KB |
Output is correct |
41 |
Correct |
18 ms |
3880 KB |
Output is correct |
42 |
Correct |
6 ms |
4188 KB |
Output is correct |
43 |
Correct |
5 ms |
4188 KB |
Output is correct |
44 |
Correct |
24 ms |
4188 KB |
Output is correct |
45 |
Correct |
7 ms |
4688 KB |
Output is correct |
46 |
Correct |
7 ms |
4544 KB |
Output is correct |
47 |
Correct |
30 ms |
4696 KB |
Output is correct |
48 |
Correct |
9 ms |
4952 KB |
Output is correct |
49 |
Correct |
9 ms |
4960 KB |
Output is correct |
50 |
Correct |
35 ms |
4956 KB |
Output is correct |
51 |
Correct |
10 ms |
5468 KB |
Output is correct |
52 |
Correct |
11 ms |
5468 KB |
Output is correct |
53 |
Correct |
42 ms |
5376 KB |
Output is correct |
54 |
Correct |
13 ms |
5720 KB |
Output is correct |
55 |
Correct |
14 ms |
5724 KB |
Output is correct |
56 |
Correct |
57 ms |
5720 KB |
Output is correct |
57 |
Correct |
15 ms |
6232 KB |
Output is correct |
58 |
Correct |
12 ms |
6236 KB |
Output is correct |
59 |
Correct |
55 ms |
6368 KB |
Output is correct |
60 |
Correct |
13 ms |
6748 KB |
Output is correct |
61 |
Correct |
15 ms |
6748 KB |
Output is correct |
62 |
Correct |
62 ms |
6748 KB |
Output is correct |
63 |
Correct |
71 ms |
6736 KB |
Output is correct |
64 |
Correct |
116 ms |
6748 KB |
Output is correct |
65 |
Correct |
118 ms |
6748 KB |
Output is correct |
66 |
Correct |
78 ms |
6744 KB |
Output is correct |
67 |
Correct |
74 ms |
6564 KB |
Output is correct |
68 |
Correct |
36 ms |
6736 KB |
Output is correct |
69 |
Correct |
36 ms |
6740 KB |
Output is correct |
70 |
Correct |
28 ms |
6748 KB |
Output is correct |
71 |
Correct |
32 ms |
6748 KB |
Output is correct |
72 |
Correct |
27 ms |
6744 KB |
Output is correct |
73 |
Correct |
23 ms |
7004 KB |
Output is correct |
74 |
Correct |
33 ms |
7068 KB |
Output is correct |
75 |
Correct |
36 ms |
7000 KB |
Output is correct |
76 |
Correct |
43 ms |
6992 KB |
Output is correct |
77 |
Correct |
35 ms |
6996 KB |
Output is correct |
78 |
Correct |
37 ms |
7000 KB |
Output is correct |
79 |
Correct |
32 ms |
7004 KB |
Output is correct |
80 |
Correct |
34 ms |
7000 KB |
Output is correct |
81 |
Correct |
37 ms |
7004 KB |
Output is correct |
82 |
Correct |
32 ms |
7004 KB |
Output is correct |
83 |
Correct |
52 ms |
6996 KB |
Output is correct |
84 |
Correct |
41 ms |
6992 KB |
Output is correct |
85 |
Correct |
38 ms |
7004 KB |
Output is correct |
86 |
Correct |
41 ms |
6816 KB |
Output is correct |
87 |
Correct |
40 ms |
7004 KB |
Output is correct |
88 |
Correct |
43 ms |
6744 KB |
Output is correct |
89 |
Correct |
48 ms |
6948 KB |
Output is correct |
90 |
Correct |
46 ms |
6872 KB |
Output is correct |
91 |
Correct |
44 ms |
6748 KB |
Output is correct |
92 |
Correct |
43 ms |
6708 KB |
Output is correct |