/*
*
* ^v^
*
*/
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <iomanip>
#include <map>
#include <numeric>
#include <functional>
#include <algorithm>
#include <set>
#include <queue>
#include <climits>
#include <cstdlib>
#include <chrono>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define iamtefu ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long int
#define ld long double
#define sc(a) scanf("%lld", &a);
#define scv(a, n) vector<ll> a(n);fl(i,0,n){sc(a[i])}
#define prl(a) fl(i,0,a.size()){printf("%lld ", a[i]);}printf("\n");
#define fl(i,a,n) for (ll i(a); i<n; i++)
#define rfl(i,a,n) for (ll i(n-1); i>=a; i--)
#define print(a) for (auto x:a){cout<<x<<" ";} cout<<"\n";
#define tt int tt; cin>>tt; for(;tt--;)
ll gcd(ll a, ll b){
if (b==0){
return a;
}
return gcd(b, a%b);
}
ll pw(ll a, ll b, ll m){
ll res=1;
a%=m;
while (b){
if (b&1){
res=(res*a)%m;
}
a=(a*a)%m;
b/=2;
}
return res;
}
void scn(){
ll n, s; cin>>n>>s;
vector <string> c(n);
vector <vector <ll>> a(n+1, vector <ll>(n+1, 1e9));
queue <vector <ll>> q;
fl(i,0,n){
cin>>c[i];
fl(j,0,n){
if (c[i][j]=='H'){
q.push({i, j});
a[i][j]=0;
}
}
}
vector <ll> di={1, 1, 0, -1, -1, -1, 0, 1};
vector <ll> dj={0, 1, 1, 1, 0, -1, -1, -1};
while (!q.empty()){
auto j = q.front();
q.pop();
for (int d=0; d<8; d+=2){
ll i1 = j[0]+di[d], j1 = j[1]+dj[d];
if (i1>=0 && j1>=0 && i1<n && j1<n && a[i1][j1]==1e9 && (c[i1][j1]=='G' || c[i1][j1]=='M')){
q.push({i1, j1});
a[i1][j1]=a[j[0]][j[1]]+1;
}
}
}
vector <vector <ll>> dis(n+1, vector <ll>(n+1, 1e9));
vector <ll> hij;
fl(i,0,n){
fl(j,0,n){
if (c[i][j]=='M'){
q.push({i, j});
dis[i][j]=0;
hij={i, j};
}
}
}
// while (!q.empty()){
// auto j = q.front();
// q.pop();
// for (int d=0; d<8; d+=2){
// ll i1 = j[0]+di[d], j1 = j[1]+dj[d];
// if (i1>=0 && j1>=0 && i1<n && j1<n && dis[i1][j1]==1e9 && (c[i1][j1]=='G' || c[i1][j1]=='D')){
// dis[i1][j1]=dis[j[0]][j[1]]+1;
// q.push({i1, j1});
// }
// }
// }
// for (auto &y:dis){
// for (auto &x:y){
// if (x!=1e9){
// x=(x)/s;
// }
// }
// }
if (a[hij[0]][hij[1]]==0){
cout<<"-1\n";
return;
}
// for (auto x:dis){
// print(x)
// }
// cout<<'\n';
// for (auto x:a){
// print(x)
// }
auto ist=[&](ll mid){
vector <vector <ll>> vis(n+1, vector <ll>(n+1));
queue <vector <ll>> q;
q.push({hij[0], hij[1]});
vis[hij[0]][hij[1]]++;
while (!q.empty()){
auto j = q.front();
q.pop();
if (dis[j[0]][j[1]]/s+mid>=a[j[0]][j[1]]){
// cout<<dis[j[0]][j[1]]<<" "<<mid<<" "<<a[j[0]][j[1]]<<" pl1\n";
continue;
}
for (int d=0; d<8; d+=2){
ll i1 = j[0]+di[d], j1 = j[1]+dj[d];
if (i1>=0 && j1>=0 && i1<n && j1<n && c[i1][j1]=='D'){
return true;
}
}
// cout<<c[j[0]][j[1]]<<" pl\n";
for (int d=0; d<8; d+=2){
ll i1 = j[0]+di[d], j1 = j[1]+dj[d];
if (i1>=0 && i1<n && j1>=0 && j1<n && (c[i1][j1]=='D' || c[i1][j1]=='G') && vis[i1][j1]==0){
q.push({i1, j1});
dis[i1][j1]=dis[j[0]][j[1]]+1;
vis[i1][j1]++;
}
}
}
return false;
};
ll l = 0, r = 1e18;
// cout<<ist(3)<<" o\n";
while (l<=r){
// cout<<l<<" "<<r<<'\n';
ll mid = (l+r)/2;
// cout<<mid<<" "<<ist(mid)<<" pl\n";
if (ist(mid)){
l = mid+1;
} else {
r = mid-1;
}
}
if (ist(0)==0){
cout<<"-1\n";
return;
}
cout<<max(0ll,l-1)<<'\n';
// not necessarily distinct
}
int main(){
iamtefu;
#if defined(airths)
auto t1=chrono::high_resolution_clock::now();
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
//
#endif
// tt
{
scn();
}
#if defined(airths)
auto t2=chrono::high_resolution_clock::now();
ld ti=chrono::duration_cast<chrono::nanoseconds>(t2-t1).count();
ti*=1e-6;
cerr<<"Time: "<<setprecision(12)<<ti;
cerr<<"ms\n";
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
430 ms |
16408 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
22 ms |
3604 KB |
Output is correct |
34 |
Correct |
22 ms |
3388 KB |
Output is correct |
35 |
Correct |
83 ms |
3604 KB |
Output is correct |
36 |
Correct |
33 ms |
4496 KB |
Output is correct |
37 |
Correct |
31 ms |
4300 KB |
Output is correct |
38 |
Correct |
112 ms |
4504 KB |
Output is correct |
39 |
Correct |
37 ms |
5368 KB |
Output is correct |
40 |
Correct |
40 ms |
5592 KB |
Output is correct |
41 |
Correct |
136 ms |
5384 KB |
Output is correct |
42 |
Correct |
52 ms |
6720 KB |
Output is correct |
43 |
Correct |
54 ms |
6644 KB |
Output is correct |
44 |
Correct |
187 ms |
6668 KB |
Output is correct |
45 |
Correct |
53 ms |
7964 KB |
Output is correct |
46 |
Correct |
68 ms |
7916 KB |
Output is correct |
47 |
Correct |
244 ms |
7992 KB |
Output is correct |
48 |
Correct |
69 ms |
9360 KB |
Output is correct |
49 |
Correct |
66 ms |
9464 KB |
Output is correct |
50 |
Correct |
280 ms |
9396 KB |
Output is correct |
51 |
Correct |
81 ms |
11016 KB |
Output is correct |
52 |
Correct |
86 ms |
11068 KB |
Output is correct |
53 |
Correct |
330 ms |
10916 KB |
Output is correct |
54 |
Correct |
130 ms |
12516 KB |
Output is correct |
55 |
Correct |
103 ms |
12660 KB |
Output is correct |
56 |
Correct |
374 ms |
12720 KB |
Output is correct |
57 |
Correct |
121 ms |
14420 KB |
Output is correct |
58 |
Correct |
107 ms |
14496 KB |
Output is correct |
59 |
Correct |
447 ms |
14544 KB |
Output is correct |
60 |
Correct |
122 ms |
16276 KB |
Output is correct |
61 |
Correct |
121 ms |
16352 KB |
Output is correct |
62 |
Correct |
574 ms |
16376 KB |
Output is correct |
63 |
Correct |
397 ms |
16280 KB |
Output is correct |
64 |
Correct |
614 ms |
16356 KB |
Output is correct |
65 |
Correct |
516 ms |
16280 KB |
Output is correct |
66 |
Correct |
445 ms |
16340 KB |
Output is correct |
67 |
Correct |
405 ms |
16308 KB |
Output is correct |
68 |
Correct |
247 ms |
16328 KB |
Output is correct |
69 |
Correct |
178 ms |
16396 KB |
Output is correct |
70 |
Correct |
202 ms |
16356 KB |
Output is correct |
71 |
Correct |
185 ms |
16308 KB |
Output is correct |
72 |
Correct |
152 ms |
16460 KB |
Output is correct |
73 |
Correct |
222 ms |
16536 KB |
Output is correct |
74 |
Correct |
344 ms |
16596 KB |
Output is correct |
75 |
Correct |
354 ms |
16488 KB |
Output is correct |
76 |
Correct |
346 ms |
16588 KB |
Output is correct |
77 |
Correct |
342 ms |
16568 KB |
Output is correct |
78 |
Correct |
340 ms |
16460 KB |
Output is correct |
79 |
Correct |
328 ms |
16680 KB |
Output is correct |
80 |
Correct |
327 ms |
16504 KB |
Output is correct |
81 |
Correct |
360 ms |
16580 KB |
Output is correct |
82 |
Correct |
310 ms |
16596 KB |
Output is correct |
83 |
Correct |
392 ms |
16472 KB |
Output is correct |
84 |
Correct |
385 ms |
16388 KB |
Output is correct |
85 |
Correct |
374 ms |
16444 KB |
Output is correct |
86 |
Correct |
389 ms |
16512 KB |
Output is correct |
87 |
Correct |
395 ms |
16412 KB |
Output is correct |
88 |
Correct |
422 ms |
16532 KB |
Output is correct |
89 |
Correct |
412 ms |
16504 KB |
Output is correct |
90 |
Correct |
432 ms |
16524 KB |
Output is correct |
91 |
Correct |
423 ms |
16464 KB |
Output is correct |
92 |
Correct |
425 ms |
16444 KB |
Output is correct |