#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 0;
//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]=='D'&&vis[dx][dy]==-1))
{
//cout<<"++";
vis[dx][dy]=vis[x][y]+1;
q.push(dx*N+dy);
}
}
}
return vis[ex][ey]!=-1;
}
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;
}
}
/*for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=n;j++) cout<<a[i][j];
cout<<endl;
}*/
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",ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
179 ms |
11868 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
528 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
888 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
888 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
632 KB |
Output is correct |
20 |
Correct |
2 ms |
632 KB |
Output is correct |
21 |
Correct |
3 ms |
764 KB |
Output is correct |
22 |
Correct |
2 ms |
760 KB |
Output is correct |
23 |
Correct |
2 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
760 KB |
Output is correct |
25 |
Correct |
3 ms |
888 KB |
Output is correct |
26 |
Correct |
3 ms |
888 KB |
Output is correct |
27 |
Correct |
3 ms |
1016 KB |
Output is correct |
28 |
Correct |
3 ms |
1016 KB |
Output is correct |
29 |
Correct |
3 ms |
1016 KB |
Output is correct |
30 |
Correct |
3 ms |
1016 KB |
Output is correct |
31 |
Correct |
3 ms |
1144 KB |
Output is correct |
32 |
Correct |
3 ms |
1020 KB |
Output is correct |
33 |
Correct |
20 ms |
5184 KB |
Output is correct |
34 |
Correct |
22 ms |
5112 KB |
Output is correct |
35 |
Correct |
40 ms |
5220 KB |
Output is correct |
36 |
Correct |
26 ms |
5880 KB |
Output is correct |
37 |
Correct |
29 ms |
5856 KB |
Output is correct |
38 |
Correct |
49 ms |
5852 KB |
Output is correct |
39 |
Correct |
32 ms |
6608 KB |
Output is correct |
40 |
Correct |
33 ms |
6556 KB |
Output is correct |
41 |
Correct |
78 ms |
6660 KB |
Output is correct |
42 |
Correct |
39 ms |
7328 KB |
Output is correct |
43 |
Correct |
39 ms |
7292 KB |
Output is correct |
44 |
Correct |
96 ms |
7376 KB |
Output is correct |
45 |
Correct |
49 ms |
8092 KB |
Output is correct |
46 |
Correct |
46 ms |
8016 KB |
Output is correct |
47 |
Correct |
118 ms |
8100 KB |
Output is correct |
48 |
Correct |
52 ms |
8824 KB |
Output is correct |
49 |
Correct |
54 ms |
8808 KB |
Output is correct |
50 |
Correct |
139 ms |
8832 KB |
Output is correct |
51 |
Correct |
60 ms |
9464 KB |
Output is correct |
52 |
Correct |
63 ms |
9464 KB |
Output is correct |
53 |
Correct |
175 ms |
9564 KB |
Output is correct |
54 |
Correct |
69 ms |
10212 KB |
Output is correct |
55 |
Correct |
76 ms |
10192 KB |
Output is correct |
56 |
Correct |
201 ms |
10304 KB |
Output is correct |
57 |
Correct |
81 ms |
11000 KB |
Output is correct |
58 |
Correct |
82 ms |
11020 KB |
Output is correct |
59 |
Correct |
230 ms |
11044 KB |
Output is correct |
60 |
Correct |
89 ms |
11792 KB |
Output is correct |
61 |
Correct |
99 ms |
11716 KB |
Output is correct |
62 |
Correct |
261 ms |
11828 KB |
Output is correct |
63 |
Correct |
199 ms |
11800 KB |
Output is correct |
64 |
Correct |
348 ms |
11896 KB |
Output is correct |
65 |
Correct |
316 ms |
11792 KB |
Output is correct |
66 |
Correct |
216 ms |
11768 KB |
Output is correct |
67 |
Correct |
213 ms |
11684 KB |
Output is correct |
68 |
Correct |
125 ms |
11804 KB |
Output is correct |
69 |
Correct |
124 ms |
11772 KB |
Output is correct |
70 |
Correct |
114 ms |
11768 KB |
Output is correct |
71 |
Correct |
116 ms |
11796 KB |
Output is correct |
72 |
Correct |
103 ms |
11828 KB |
Output is correct |
73 |
Correct |
108 ms |
12024 KB |
Output is correct |
74 |
Correct |
133 ms |
12000 KB |
Output is correct |
75 |
Correct |
142 ms |
12024 KB |
Output is correct |
76 |
Correct |
137 ms |
12072 KB |
Output is correct |
77 |
Correct |
135 ms |
11960 KB |
Output is correct |
78 |
Correct |
158 ms |
12124 KB |
Output is correct |
79 |
Correct |
121 ms |
12028 KB |
Output is correct |
80 |
Correct |
142 ms |
11968 KB |
Output is correct |
81 |
Correct |
150 ms |
12036 KB |
Output is correct |
82 |
Correct |
134 ms |
11984 KB |
Output is correct |
83 |
Correct |
207 ms |
11920 KB |
Output is correct |
84 |
Correct |
150 ms |
12024 KB |
Output is correct |
85 |
Correct |
169 ms |
11948 KB |
Output is correct |
86 |
Correct |
166 ms |
11896 KB |
Output is correct |
87 |
Correct |
169 ms |
12000 KB |
Output is correct |
88 |
Correct |
177 ms |
11896 KB |
Output is correct |
89 |
Correct |
198 ms |
12124 KB |
Output is correct |
90 |
Correct |
178 ms |
11920 KB |
Output is correct |
91 |
Correct |
190 ms |
11868 KB |
Output is correct |
92 |
Correct |
189 ms |
11896 KB |
Output is correct |