#include <iostream>
#include <fstream>
using namespace std;
const int nmax=805;
int d1[]={-1,0,1,0};
int d2[]={0,-1,0,1};
int d[nmax][nmax],a[nmax][nmax];
string s;
pair<int,int> q[nmax*nmax];
int p,u,n,m,i,j,li,ci,lf,cf,l1,l2,c1,c2,S,ans;
bool ok(int X,int Y)
{
return (X>=1&&Y>=1&&X<=n&&Y<=n);
}
void bfs()
{
p=1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]==0)
q[++u]={i,j};
for(p=1;p<=u;p++)
{
li=q[p].first;ci=q[p].second;
for(i=0;i<4;i++)
{
lf=li+d1[i];cf=ci+d2[i];
if(ok(lf,cf)&&a[li][ci]+1<a[lf][cf])
{
a[lf][cf]=a[li][ci]+1;
q[++u]={lf,cf};
}
}
}
}
bool check(int val)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(d[i][j]!=-1)
d[i][j]=0;
d[l1][c1]=1;
q[p=u=1]={l1,c1};
for(p=1;p<=u;p++)
{
li=q[p].first;ci=q[p].second;
if(li==l2&&ci==c2) return 1;
if((d[li][ci]-1)/S+val>=a[li][ci]) continue;
for(i=0;i<4;i++)
{
lf=li+d1[i];cf=ci+d2[i];
if(ok(lf,cf)&&(!d[lf][cf]))
{
d[lf][cf]=d[li][ci]+1;
q[++u]={lf,cf};
}
}
}
return 0;
}
int main()
{
//freopen("data.in","r",stdin);
cin>>n>>S;
for(i=0;i<=n+1;i++)
for(j=0;j<=n+1;j++)
a[i][j]=(1<<30);
for(i=1;i<=n;i++)
{
cin>>s;
for(j=0;j<n;j++)
{
if(s[j]=='T') d[i][j+1]=a[i][j+1]=-1;
if(s[j]=='M') l1=i,c1=j+1;
if(s[j]=='D') l2=i,c2=j+1;
if(s[j]=='H') a[i][j+1]=0;
}
}
bfs();
for(int pu=20;pu>=0;pu--)
if(check(ans+(1<<pu)))
ans+=(1<<pu);
if(!check(0)) ans=-1;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
612 KB |
Output is correct |
5 |
Correct |
2 ms |
612 KB |
Output is correct |
6 |
Correct |
2 ms |
612 KB |
Output is correct |
7 |
Correct |
167 ms |
10668 KB |
Output is correct |
8 |
Correct |
3 ms |
10668 KB |
Output is correct |
9 |
Correct |
3 ms |
10668 KB |
Output is correct |
10 |
Correct |
3 ms |
10668 KB |
Output is correct |
11 |
Correct |
3 ms |
10668 KB |
Output is correct |
12 |
Incorrect |
3 ms |
10668 KB |
Output isn't correct |
13 |
Correct |
3 ms |
10668 KB |
Output is correct |
14 |
Correct |
4 ms |
10668 KB |
Output is correct |
15 |
Correct |
3 ms |
10668 KB |
Output is correct |
16 |
Correct |
2 ms |
10668 KB |
Output is correct |
17 |
Correct |
3 ms |
10668 KB |
Output is correct |
18 |
Correct |
3 ms |
10668 KB |
Output is correct |
19 |
Correct |
3 ms |
10668 KB |
Output is correct |
20 |
Correct |
3 ms |
10668 KB |
Output is correct |
21 |
Correct |
2 ms |
10668 KB |
Output is correct |
22 |
Correct |
3 ms |
10668 KB |
Output is correct |
23 |
Correct |
4 ms |
10668 KB |
Output is correct |
24 |
Correct |
3 ms |
10668 KB |
Output is correct |
25 |
Correct |
3 ms |
10668 KB |
Output is correct |
26 |
Correct |
3 ms |
10668 KB |
Output is correct |
27 |
Correct |
3 ms |
10668 KB |
Output is correct |
28 |
Correct |
4 ms |
10668 KB |
Output is correct |
29 |
Correct |
4 ms |
10668 KB |
Output is correct |
30 |
Correct |
4 ms |
10668 KB |
Output is correct |
31 |
Correct |
4 ms |
10668 KB |
Output is correct |
32 |
Correct |
4 ms |
10668 KB |
Output is correct |
33 |
Correct |
13 ms |
10668 KB |
Output is correct |
34 |
Correct |
12 ms |
10668 KB |
Output is correct |
35 |
Correct |
44 ms |
10668 KB |
Output is correct |
36 |
Correct |
20 ms |
10668 KB |
Output is correct |
37 |
Correct |
16 ms |
10668 KB |
Output is correct |
38 |
Correct |
56 ms |
10668 KB |
Output is correct |
39 |
Correct |
22 ms |
10668 KB |
Output is correct |
40 |
Correct |
25 ms |
10668 KB |
Output is correct |
41 |
Correct |
64 ms |
10668 KB |
Output is correct |
42 |
Correct |
28 ms |
10668 KB |
Output is correct |
43 |
Correct |
26 ms |
10668 KB |
Output is correct |
44 |
Correct |
77 ms |
10668 KB |
Output is correct |
45 |
Correct |
30 ms |
10668 KB |
Output is correct |
46 |
Correct |
32 ms |
10668 KB |
Output is correct |
47 |
Correct |
130 ms |
10668 KB |
Output is correct |
48 |
Correct |
36 ms |
10668 KB |
Output is correct |
49 |
Correct |
53 ms |
10668 KB |
Output is correct |
50 |
Correct |
125 ms |
10668 KB |
Output is correct |
51 |
Correct |
43 ms |
10668 KB |
Output is correct |
52 |
Correct |
40 ms |
10668 KB |
Output is correct |
53 |
Correct |
169 ms |
10668 KB |
Output is correct |
54 |
Correct |
60 ms |
10668 KB |
Output is correct |
55 |
Correct |
48 ms |
10668 KB |
Output is correct |
56 |
Correct |
183 ms |
10668 KB |
Output is correct |
57 |
Correct |
48 ms |
10668 KB |
Output is correct |
58 |
Correct |
49 ms |
10668 KB |
Output is correct |
59 |
Correct |
237 ms |
10668 KB |
Output is correct |
60 |
Correct |
58 ms |
10668 KB |
Output is correct |
61 |
Correct |
58 ms |
10668 KB |
Output is correct |
62 |
Correct |
299 ms |
10668 KB |
Output is correct |
63 |
Correct |
264 ms |
10668 KB |
Output is correct |
64 |
Correct |
411 ms |
10668 KB |
Output is correct |
65 |
Correct |
310 ms |
10668 KB |
Output is correct |
66 |
Correct |
274 ms |
10668 KB |
Output is correct |
67 |
Correct |
252 ms |
10668 KB |
Output is correct |
68 |
Correct |
150 ms |
10668 KB |
Output is correct |
69 |
Correct |
177 ms |
10668 KB |
Output is correct |
70 |
Correct |
179 ms |
10668 KB |
Output is correct |
71 |
Correct |
153 ms |
10668 KB |
Output is correct |
72 |
Correct |
162 ms |
10668 KB |
Output is correct |
73 |
Correct |
91 ms |
10700 KB |
Output is correct |
74 |
Correct |
146 ms |
10700 KB |
Output is correct |
75 |
Correct |
148 ms |
10700 KB |
Output is correct |
76 |
Correct |
169 ms |
10740 KB |
Output is correct |
77 |
Correct |
166 ms |
10740 KB |
Output is correct |
78 |
Correct |
214 ms |
10740 KB |
Output is correct |
79 |
Correct |
139 ms |
10740 KB |
Output is correct |
80 |
Correct |
151 ms |
10748 KB |
Output is correct |
81 |
Correct |
161 ms |
10748 KB |
Output is correct |
82 |
Correct |
143 ms |
10748 KB |
Output is correct |
83 |
Correct |
142 ms |
10748 KB |
Output is correct |
84 |
Correct |
216 ms |
10748 KB |
Output is correct |
85 |
Correct |
140 ms |
10748 KB |
Output is correct |
86 |
Correct |
172 ms |
10748 KB |
Output is correct |
87 |
Correct |
160 ms |
10748 KB |
Output is correct |
88 |
Correct |
160 ms |
10748 KB |
Output is correct |
89 |
Correct |
185 ms |
10748 KB |
Output is correct |
90 |
Correct |
161 ms |
10748 KB |
Output is correct |
91 |
Correct |
161 ms |
10748 KB |
Output is correct |
92 |
Correct |
196 ms |
10748 KB |
Output is correct |