#include<bits/stdc++.h>
using namespace std;
#define in insert
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define Endl "\n"
#define ENDL "\n"
#define fi first
#define se second
#define be begin()
#define en end()
#define pb push_back
#define mpa make_pair
typedef long long ll;
typedef long double ld;
const int MOD=1e9+7;
const int MAX=2e5+1;
ll n,s;
string arr[802][802];
bool vist[802][802];
ll mechoDist[802][802];
ll BeesDist[802][802];
pair<ll,ll> mecho;
pair<ll,ll> home;
ll dx[]={1,-1,0,0};
ll dy[]={0,0,1,-1};
bool valid (ll x,ll y){
return x>=1&&x<=n&&y>=1&&y<=n&&!vist[x][y]&&arr[x][y]!="T";
}
bool check =false;
void bfs (ll val){
vist[mecho.fi][mecho.se]=true;
queue<pair<ll,ll>>q;
q.push(mecho);
while (!q.empty()){
auto p =q.front();
ll x = p.fi;
ll y = p.se;
q.pop();
if(x==home.fi&&y==home.se)check=true;
if(val+mechoDist[x][y]>=BeesDist[x][y])continue;
for (ll i=0;i<4;i++){
ll nx = x+dx[i];
ll ny = y+dy[i];
if(valid(nx,ny)){
vist[nx][ny]=true;
q.push(mpa(nx,ny));
}
}
}
}
int main(){
fastio
//freopen("11.in","r",stdin);
//freopen("cl.out","w",stdout);
cin>>n>>s;
for (ll i=1;i<=n;i++){
string s;
cin>>s;
for (ll j=1;j<=n;j++){
arr[i][j]=s[j-1];
if(arr[i][j]=="M")mecho=mpa(i,j);
else if (arr[i][j]=="D")home = mpa(i,j);
}
}
queue <pair<ll,ll>> q;
q.push(mecho);
vist[mecho.fi][mecho.se]=true;
while(!q.empty()){
auto p = q.front();
ll x = p.fi;
ll y = p.se;
q.pop();
for (ll i=0;i<4;i++){
ll nx = x+dx[i];
ll ny = y+dy[i];
if(valid(nx,ny)&&arr[nx][ny]!="H"){
mechoDist[nx][ny]=mechoDist[x][y]+1;
vist[nx][ny]=true;
q.push(mpa(nx,ny));
}
}
}
for (ll i=1;i<=n;i++)for(ll j=1;j<=n;j++){
mechoDist[i][j]=mechoDist[i][j]/s;
vist[i][j]=false;
}
for (ll i=1;i<=n;i++)for (ll j=1;j<=n;j++){
if(arr[i][j]=="H"){
q.push(mpa(i,j));
vist[i][j]=true;
}
}
while(!q.empty()){
auto p = q.front();
ll x = p.fi;
ll y = p.se;
q.pop();
for (ll i=0;i<4;i++){
ll nx = x+dx[i];
ll ny = y+dy[i];
if(valid(nx,ny)&&arr[nx][ny]!="D"&&arr[nx][ny]!="H"){
BeesDist[nx][ny]=BeesDist[x][y]+1;
vist[nx][ny]=true;
q.push(mpa(nx,ny));
}
}
}
/*cout<<endl<<endl;
for(ll i=1;i<=n;i++){for (ll j=1;j<=n;j++){
if(arr[i][j]=="G")
cout<<mechoDist[i][j];
else cout<<arr[i][j];
}
cout<<endl;
}
cout<<endl<<endl;
for(ll i=1;i<=n;i++){for (ll j=1;j<=n;j++){
cout<<BeesDist[i][j];
}
cout<<endl;
}*/
ll l=0;
ll r = 1e9;
ll ans =-1;
while (l<=r){
ll mid = (l+r)/2;
for (ll i=1;i<=n;i++)for(ll j=1;j<=n;j++){
vist[i][j]=false;
}
check=false;
bfs(mid);
if(check){
l=mid+1;
ans = mid;
} else {
r=mid-1;
}
}
cout<<ans;
return 0;
}
unsigned long long power(unsigned long long x,
ll y, ll p)
{
unsigned long long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
unsigned long long modInverse(unsigned long long n,
ll p)
{
return power(n, p - 2, p);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20436 KB |
Output is correct |
2 |
Correct |
10 ms |
20436 KB |
Output is correct |
3 |
Correct |
10 ms |
20476 KB |
Output is correct |
4 |
Correct |
10 ms |
20476 KB |
Output is correct |
5 |
Correct |
10 ms |
20540 KB |
Output is correct |
6 |
Correct |
10 ms |
20564 KB |
Output is correct |
7 |
Correct |
336 ms |
31456 KB |
Output is correct |
8 |
Correct |
11 ms |
20564 KB |
Output is correct |
9 |
Correct |
11 ms |
20580 KB |
Output is correct |
10 |
Correct |
10 ms |
20564 KB |
Output is correct |
11 |
Correct |
10 ms |
20536 KB |
Output is correct |
12 |
Correct |
10 ms |
20948 KB |
Output is correct |
13 |
Correct |
11 ms |
20904 KB |
Output is correct |
14 |
Correct |
11 ms |
21056 KB |
Output is correct |
15 |
Correct |
11 ms |
20656 KB |
Output is correct |
16 |
Correct |
10 ms |
20592 KB |
Output is correct |
17 |
Correct |
10 ms |
20692 KB |
Output is correct |
18 |
Correct |
10 ms |
20692 KB |
Output is correct |
19 |
Correct |
11 ms |
20692 KB |
Output is correct |
20 |
Correct |
11 ms |
20752 KB |
Output is correct |
21 |
Correct |
12 ms |
20780 KB |
Output is correct |
22 |
Correct |
11 ms |
20860 KB |
Output is correct |
23 |
Correct |
11 ms |
20900 KB |
Output is correct |
24 |
Correct |
11 ms |
20948 KB |
Output is correct |
25 |
Correct |
12 ms |
20948 KB |
Output is correct |
26 |
Correct |
11 ms |
20948 KB |
Output is correct |
27 |
Correct |
13 ms |
21080 KB |
Output is correct |
28 |
Correct |
11 ms |
21120 KB |
Output is correct |
29 |
Correct |
11 ms |
21112 KB |
Output is correct |
30 |
Correct |
11 ms |
21076 KB |
Output is correct |
31 |
Correct |
11 ms |
21144 KB |
Output is correct |
32 |
Correct |
13 ms |
21184 KB |
Output is correct |
33 |
Correct |
25 ms |
24688 KB |
Output is correct |
34 |
Correct |
29 ms |
24864 KB |
Output is correct |
35 |
Incorrect |
87 ms |
25132 KB |
Output isn't correct |
36 |
Correct |
33 ms |
25284 KB |
Output is correct |
37 |
Correct |
34 ms |
25576 KB |
Output is correct |
38 |
Incorrect |
102 ms |
25724 KB |
Output isn't correct |
39 |
Correct |
34 ms |
25936 KB |
Output is correct |
40 |
Correct |
37 ms |
26264 KB |
Output is correct |
41 |
Incorrect |
156 ms |
26484 KB |
Output isn't correct |
42 |
Correct |
41 ms |
26572 KB |
Output is correct |
43 |
Correct |
41 ms |
26988 KB |
Output is correct |
44 |
Incorrect |
174 ms |
27084 KB |
Output isn't correct |
45 |
Correct |
49 ms |
27080 KB |
Output is correct |
46 |
Correct |
47 ms |
27724 KB |
Output is correct |
47 |
Incorrect |
221 ms |
27720 KB |
Output isn't correct |
48 |
Correct |
56 ms |
27768 KB |
Output is correct |
49 |
Correct |
53 ms |
28364 KB |
Output is correct |
50 |
Incorrect |
258 ms |
28448 KB |
Output isn't correct |
51 |
Correct |
62 ms |
28396 KB |
Output is correct |
52 |
Correct |
66 ms |
28960 KB |
Output is correct |
53 |
Incorrect |
290 ms |
29160 KB |
Output isn't correct |
54 |
Correct |
67 ms |
28900 KB |
Output is correct |
55 |
Correct |
73 ms |
29536 KB |
Output is correct |
56 |
Incorrect |
378 ms |
29836 KB |
Output isn't correct |
57 |
Correct |
88 ms |
29636 KB |
Output is correct |
58 |
Correct |
74 ms |
30116 KB |
Output is correct |
59 |
Incorrect |
384 ms |
30496 KB |
Output isn't correct |
60 |
Correct |
85 ms |
30116 KB |
Output is correct |
61 |
Correct |
83 ms |
30664 KB |
Output is correct |
62 |
Incorrect |
470 ms |
31160 KB |
Output isn't correct |
63 |
Correct |
339 ms |
31052 KB |
Output is correct |
64 |
Correct |
493 ms |
31160 KB |
Output is correct |
65 |
Correct |
449 ms |
31160 KB |
Output is correct |
66 |
Correct |
403 ms |
31152 KB |
Output is correct |
67 |
Correct |
311 ms |
31160 KB |
Output is correct |
68 |
Correct |
206 ms |
31140 KB |
Output is correct |
69 |
Correct |
180 ms |
31216 KB |
Output is correct |
70 |
Correct |
141 ms |
31096 KB |
Output is correct |
71 |
Correct |
148 ms |
31104 KB |
Output is correct |
72 |
Correct |
170 ms |
31208 KB |
Output is correct |
73 |
Correct |
236 ms |
31700 KB |
Output is correct |
74 |
Correct |
206 ms |
31628 KB |
Output is correct |
75 |
Correct |
224 ms |
31656 KB |
Output is correct |
76 |
Correct |
227 ms |
31608 KB |
Output is correct |
77 |
Correct |
240 ms |
31692 KB |
Output is correct |
78 |
Correct |
224 ms |
31712 KB |
Output is correct |
79 |
Correct |
186 ms |
31636 KB |
Output is correct |
80 |
Correct |
257 ms |
31564 KB |
Output is correct |
81 |
Correct |
244 ms |
31604 KB |
Output is correct |
82 |
Correct |
196 ms |
31616 KB |
Output is correct |
83 |
Correct |
295 ms |
31556 KB |
Output is correct |
84 |
Correct |
290 ms |
31732 KB |
Output is correct |
85 |
Correct |
276 ms |
31568 KB |
Output is correct |
86 |
Correct |
322 ms |
31568 KB |
Output is correct |
87 |
Correct |
301 ms |
31560 KB |
Output is correct |
88 |
Correct |
322 ms |
31392 KB |
Output is correct |
89 |
Correct |
315 ms |
31720 KB |
Output is correct |
90 |
Correct |
340 ms |
31396 KB |
Output is correct |
91 |
Correct |
300 ms |
31464 KB |
Output is correct |
92 |
Correct |
331 ms |
31468 KB |
Output is correct |