#include <bits/stdc++.h>
#define N 810
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll read()
{
char c=getchar();bool flag=0;ll x=0;
while(c<'0'||c>'9'){if(c=='-')flag=1;c=getchar();}
while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
return flag?-x:x;
}
queue<ll> q;
char a[N][N];
ll b[N][N],vis[N][N];
int ax[4]={1,0,-1,0},n,s,sx,sy,ex,ey;
int ay[4]={0,1,0,-1};
void bfs()
{
while(q.size()!=0)
{
ll x=q.front()/N,y=q.front()%N;
q.pop();
for(ll i=0;i<4;i++)
{
//cout<<"++";
ll dx=x+ax[i];
ll dy=y+ay[i];
if(a[dx][dy]=='G'&&b[dx][dy]==-1)
{
//cout<<"++";
b[dx][dy]=b[x][y]+s;
q.push(dx*N+dy);
}
}
}
}
bool check(ll mid)
{
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=n;j++) vis[i][j]=-1;
}
//cout<<"++";
if(b[sx][sy]<=mid*s) return false;
//cout<<"++";
vis[sx][sy]=mid*s;
q.push(sx*N+sy);
while(q.size()!=0)
{
ll x=q.front()/N,y=q.front()%N;
q.pop();
//cout<<"++";
for(ll i=0;i<4;i++)
{
ll dx=x+ax[i];
ll dy=y+ay[i];
if((a[dx][dy]=='G'&&vis[dx][dy]==-1 && b[dx][dy]>vis[x][y]+1)||(a[dx][dy]=='G'&&vis[dx][dy]==-1))
{
//cout<<"++";
if(dx==ex&&dy==ey) return true;
vis[dx][dy]=vis[x][y]+1;
q.push(dx*N+dy);
}
}
}
return false;
}
int main()
{
n=read(),s=read();
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=n;j++) b[i][j]=-1;
}
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]=='M') sx=i,sy=j,a[i][j]='G';
if(a[i][j]=='D') ex=i,ey=j;
if(a[i][j]=='H') q.push(i*N+j),b[i][j]=0;
}
}
bfs();
ll r=n*n,l=0,ans=-1;
while(l<=r)
{
ll mid=(l+r)/2;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%lld\n",ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
6 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
7 |
Incorrect |
265 ms |
11356 KB |
Output isn't correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
10 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
12 |
Incorrect |
3 ms |
888 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
14 |
Correct |
3 ms |
888 KB |
Output is correct |
15 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
18 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
19 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
20 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
21 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
22 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
23 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
24 |
Incorrect |
3 ms |
760 KB |
Output isn't correct |
25 |
Incorrect |
3 ms |
888 KB |
Output isn't correct |
26 |
Incorrect |
3 ms |
888 KB |
Output isn't correct |
27 |
Incorrect |
3 ms |
1052 KB |
Output isn't correct |
28 |
Incorrect |
3 ms |
1016 KB |
Output isn't correct |
29 |
Incorrect |
4 ms |
1016 KB |
Output isn't correct |
30 |
Incorrect |
3 ms |
1016 KB |
Output isn't correct |
31 |
Incorrect |
4 ms |
1144 KB |
Output isn't correct |
32 |
Incorrect |
4 ms |
1144 KB |
Output isn't correct |
33 |
Incorrect |
36 ms |
5160 KB |
Output isn't correct |
34 |
Incorrect |
36 ms |
4984 KB |
Output isn't correct |
35 |
Incorrect |
37 ms |
5112 KB |
Output isn't correct |
36 |
Incorrect |
48 ms |
5768 KB |
Output isn't correct |
37 |
Incorrect |
48 ms |
5788 KB |
Output isn't correct |
38 |
Incorrect |
47 ms |
5752 KB |
Output isn't correct |
39 |
Incorrect |
59 ms |
6392 KB |
Output isn't correct |
40 |
Incorrect |
59 ms |
6440 KB |
Output isn't correct |
41 |
Incorrect |
58 ms |
6392 KB |
Output isn't correct |
42 |
Incorrect |
72 ms |
7032 KB |
Output isn't correct |
43 |
Incorrect |
72 ms |
7032 KB |
Output isn't correct |
44 |
Incorrect |
70 ms |
7004 KB |
Output isn't correct |
45 |
Incorrect |
92 ms |
7900 KB |
Output isn't correct |
46 |
Incorrect |
88 ms |
7672 KB |
Output isn't correct |
47 |
Incorrect |
88 ms |
7672 KB |
Output isn't correct |
48 |
Incorrect |
109 ms |
8464 KB |
Output isn't correct |
49 |
Incorrect |
106 ms |
8440 KB |
Output isn't correct |
50 |
Incorrect |
102 ms |
8508 KB |
Output isn't correct |
51 |
Incorrect |
125 ms |
9080 KB |
Output isn't correct |
52 |
Incorrect |
123 ms |
9080 KB |
Output isn't correct |
53 |
Incorrect |
125 ms |
9176 KB |
Output isn't correct |
54 |
Incorrect |
146 ms |
9852 KB |
Output isn't correct |
55 |
Incorrect |
143 ms |
9928 KB |
Output isn't correct |
56 |
Incorrect |
148 ms |
9848 KB |
Output isn't correct |
57 |
Incorrect |
171 ms |
10488 KB |
Output isn't correct |
58 |
Incorrect |
171 ms |
10484 KB |
Output isn't correct |
59 |
Incorrect |
186 ms |
10488 KB |
Output isn't correct |
60 |
Incorrect |
192 ms |
11128 KB |
Output isn't correct |
61 |
Incorrect |
219 ms |
11040 KB |
Output isn't correct |
62 |
Incorrect |
213 ms |
11352 KB |
Output isn't correct |
63 |
Incorrect |
327 ms |
11164 KB |
Output isn't correct |
64 |
Incorrect |
348 ms |
11256 KB |
Output isn't correct |
65 |
Incorrect |
341 ms |
11184 KB |
Output isn't correct |
66 |
Incorrect |
324 ms |
11164 KB |
Output isn't correct |
67 |
Correct |
347 ms |
11176 KB |
Output is correct |
68 |
Incorrect |
263 ms |
11128 KB |
Output isn't correct |
69 |
Incorrect |
265 ms |
11128 KB |
Output isn't correct |
70 |
Incorrect |
305 ms |
11104 KB |
Output isn't correct |
71 |
Incorrect |
264 ms |
11128 KB |
Output isn't correct |
72 |
Incorrect |
303 ms |
11384 KB |
Output isn't correct |
73 |
Incorrect |
132 ms |
11384 KB |
Output isn't correct |
74 |
Incorrect |
223 ms |
11512 KB |
Output isn't correct |
75 |
Incorrect |
220 ms |
11512 KB |
Output isn't correct |
76 |
Incorrect |
228 ms |
11524 KB |
Output isn't correct |
77 |
Incorrect |
220 ms |
11316 KB |
Output isn't correct |
78 |
Correct |
257 ms |
11328 KB |
Output is correct |
79 |
Incorrect |
255 ms |
11472 KB |
Output isn't correct |
80 |
Incorrect |
262 ms |
11340 KB |
Output isn't correct |
81 |
Incorrect |
261 ms |
11384 KB |
Output isn't correct |
82 |
Incorrect |
268 ms |
11484 KB |
Output isn't correct |
83 |
Incorrect |
269 ms |
11484 KB |
Output isn't correct |
84 |
Incorrect |
267 ms |
11384 KB |
Output isn't correct |
85 |
Incorrect |
258 ms |
11484 KB |
Output isn't correct |
86 |
Incorrect |
267 ms |
11472 KB |
Output isn't correct |
87 |
Incorrect |
262 ms |
11400 KB |
Output isn't correct |
88 |
Incorrect |
260 ms |
11324 KB |
Output isn't correct |
89 |
Incorrect |
244 ms |
11256 KB |
Output isn't correct |
90 |
Incorrect |
250 ms |
11512 KB |
Output isn't correct |
91 |
Incorrect |
242 ms |
11460 KB |
Output isn't correct |
92 |
Incorrect |
262 ms |
11320 KB |
Output isn't correct |