#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');
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;
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 |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
59 ms |
6772 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2572 KB |
Output is correct |
12 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
0 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2400 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2400 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
0 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2520 KB |
Output is correct |
25 |
Correct |
1 ms |
4708 KB |
Output is correct |
26 |
Correct |
1 ms |
4696 KB |
Output is correct |
27 |
Correct |
1 ms |
2656 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
2768 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
6 ms |
4024 KB |
Output is correct |
34 |
Correct |
6 ms |
4956 KB |
Output is correct |
35 |
Correct |
13 ms |
3936 KB |
Output is correct |
36 |
Correct |
7 ms |
4124 KB |
Output is correct |
37 |
Correct |
7 ms |
5212 KB |
Output is correct |
38 |
Correct |
18 ms |
4272 KB |
Output is correct |
39 |
Correct |
8 ms |
5216 KB |
Output is correct |
40 |
Correct |
9 ms |
4448 KB |
Output is correct |
41 |
Correct |
22 ms |
4452 KB |
Output is correct |
42 |
Correct |
10 ms |
4744 KB |
Output is correct |
43 |
Correct |
11 ms |
4708 KB |
Output is correct |
44 |
Correct |
28 ms |
4708 KB |
Output is correct |
45 |
Correct |
13 ms |
4956 KB |
Output is correct |
46 |
Correct |
13 ms |
4960 KB |
Output is correct |
47 |
Correct |
35 ms |
4952 KB |
Output is correct |
48 |
Correct |
14 ms |
5208 KB |
Output is correct |
49 |
Correct |
15 ms |
5212 KB |
Output is correct |
50 |
Correct |
40 ms |
5372 KB |
Output is correct |
51 |
Correct |
18 ms |
5720 KB |
Output is correct |
52 |
Correct |
18 ms |
5724 KB |
Output is correct |
53 |
Correct |
56 ms |
5724 KB |
Output is correct |
54 |
Correct |
20 ms |
6108 KB |
Output is correct |
55 |
Correct |
29 ms |
6236 KB |
Output is correct |
56 |
Correct |
54 ms |
6236 KB |
Output is correct |
57 |
Correct |
21 ms |
6544 KB |
Output is correct |
58 |
Correct |
21 ms |
6492 KB |
Output is correct |
59 |
Correct |
65 ms |
6492 KB |
Output is correct |
60 |
Correct |
23 ms |
6748 KB |
Output is correct |
61 |
Correct |
23 ms |
6748 KB |
Output is correct |
62 |
Correct |
75 ms |
6748 KB |
Output is correct |
63 |
Correct |
83 ms |
6744 KB |
Output is correct |
64 |
Correct |
122 ms |
6740 KB |
Output is correct |
65 |
Correct |
112 ms |
6740 KB |
Output is correct |
66 |
Correct |
86 ms |
6736 KB |
Output is correct |
67 |
Correct |
79 ms |
6748 KB |
Output is correct |
68 |
Correct |
44 ms |
6648 KB |
Output is correct |
69 |
Correct |
52 ms |
6740 KB |
Output is correct |
70 |
Correct |
37 ms |
6744 KB |
Output is correct |
71 |
Correct |
40 ms |
6684 KB |
Output is correct |
72 |
Incorrect |
37 ms |
6748 KB |
Output isn't correct |
73 |
Incorrect |
47 ms |
7004 KB |
Output isn't correct |
74 |
Correct |
45 ms |
6992 KB |
Output is correct |
75 |
Correct |
48 ms |
7004 KB |
Output is correct |
76 |
Correct |
50 ms |
6996 KB |
Output is correct |
77 |
Correct |
50 ms |
7004 KB |
Output is correct |
78 |
Correct |
52 ms |
7040 KB |
Output is correct |
79 |
Correct |
44 ms |
6996 KB |
Output is correct |
80 |
Correct |
44 ms |
6992 KB |
Output is correct |
81 |
Correct |
50 ms |
7004 KB |
Output is correct |
82 |
Correct |
51 ms |
6816 KB |
Output is correct |
83 |
Correct |
53 ms |
7020 KB |
Output is correct |
84 |
Correct |
57 ms |
7004 KB |
Output is correct |
85 |
Correct |
49 ms |
7000 KB |
Output is correct |
86 |
Correct |
53 ms |
7004 KB |
Output is correct |
87 |
Correct |
52 ms |
6992 KB |
Output is correct |
88 |
Correct |
54 ms |
6940 KB |
Output is correct |
89 |
Correct |
55 ms |
6732 KB |
Output is correct |
90 |
Correct |
56 ms |
6748 KB |
Output is correct |
91 |
Correct |
56 ms |
6748 KB |
Output is correct |
92 |
Correct |
57 ms |
6740 KB |
Output is correct |