#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,a[i][j+1]=-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 |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
448 KB |
Output is correct |
5 |
Correct |
2 ms |
448 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
196 ms |
10736 KB |
Output is correct |
8 |
Correct |
3 ms |
10736 KB |
Output is correct |
9 |
Correct |
3 ms |
10736 KB |
Output is correct |
10 |
Correct |
2 ms |
10736 KB |
Output is correct |
11 |
Correct |
3 ms |
10736 KB |
Output is correct |
12 |
Correct |
3 ms |
10736 KB |
Output is correct |
13 |
Correct |
2 ms |
10736 KB |
Output is correct |
14 |
Correct |
3 ms |
10736 KB |
Output is correct |
15 |
Correct |
2 ms |
10736 KB |
Output is correct |
16 |
Correct |
2 ms |
10736 KB |
Output is correct |
17 |
Correct |
2 ms |
10736 KB |
Output is correct |
18 |
Correct |
2 ms |
10736 KB |
Output is correct |
19 |
Correct |
2 ms |
10736 KB |
Output is correct |
20 |
Correct |
3 ms |
10736 KB |
Output is correct |
21 |
Correct |
3 ms |
10736 KB |
Output is correct |
22 |
Correct |
3 ms |
10736 KB |
Output is correct |
23 |
Correct |
2 ms |
10736 KB |
Output is correct |
24 |
Correct |
6 ms |
10736 KB |
Output is correct |
25 |
Correct |
3 ms |
10736 KB |
Output is correct |
26 |
Correct |
4 ms |
10736 KB |
Output is correct |
27 |
Correct |
3 ms |
10736 KB |
Output is correct |
28 |
Correct |
3 ms |
10736 KB |
Output is correct |
29 |
Correct |
2 ms |
10736 KB |
Output is correct |
30 |
Correct |
3 ms |
10736 KB |
Output is correct |
31 |
Correct |
3 ms |
10736 KB |
Output is correct |
32 |
Correct |
3 ms |
10736 KB |
Output is correct |
33 |
Correct |
16 ms |
10736 KB |
Output is correct |
34 |
Correct |
16 ms |
10736 KB |
Output is correct |
35 |
Correct |
42 ms |
10736 KB |
Output is correct |
36 |
Correct |
34 ms |
10736 KB |
Output is correct |
37 |
Correct |
37 ms |
10736 KB |
Output is correct |
38 |
Correct |
54 ms |
10736 KB |
Output is correct |
39 |
Correct |
31 ms |
10736 KB |
Output is correct |
40 |
Correct |
22 ms |
10736 KB |
Output is correct |
41 |
Correct |
76 ms |
10736 KB |
Output is correct |
42 |
Correct |
22 ms |
10736 KB |
Output is correct |
43 |
Correct |
22 ms |
10736 KB |
Output is correct |
44 |
Correct |
78 ms |
10736 KB |
Output is correct |
45 |
Correct |
37 ms |
10736 KB |
Output is correct |
46 |
Correct |
40 ms |
10736 KB |
Output is correct |
47 |
Correct |
125 ms |
10736 KB |
Output is correct |
48 |
Correct |
36 ms |
10736 KB |
Output is correct |
49 |
Correct |
34 ms |
10736 KB |
Output is correct |
50 |
Correct |
123 ms |
10736 KB |
Output is correct |
51 |
Correct |
37 ms |
10736 KB |
Output is correct |
52 |
Correct |
38 ms |
10736 KB |
Output is correct |
53 |
Correct |
156 ms |
10736 KB |
Output is correct |
54 |
Correct |
51 ms |
10736 KB |
Output is correct |
55 |
Correct |
47 ms |
10736 KB |
Output is correct |
56 |
Correct |
190 ms |
10736 KB |
Output is correct |
57 |
Correct |
62 ms |
10736 KB |
Output is correct |
58 |
Correct |
92 ms |
10736 KB |
Output is correct |
59 |
Correct |
214 ms |
10736 KB |
Output is correct |
60 |
Correct |
57 ms |
10736 KB |
Output is correct |
61 |
Correct |
56 ms |
10736 KB |
Output is correct |
62 |
Correct |
265 ms |
10736 KB |
Output is correct |
63 |
Correct |
245 ms |
10736 KB |
Output is correct |
64 |
Correct |
363 ms |
10736 KB |
Output is correct |
65 |
Correct |
323 ms |
10736 KB |
Output is correct |
66 |
Correct |
284 ms |
10736 KB |
Output is correct |
67 |
Correct |
260 ms |
10736 KB |
Output is correct |
68 |
Correct |
224 ms |
10736 KB |
Output is correct |
69 |
Correct |
166 ms |
10736 KB |
Output is correct |
70 |
Correct |
161 ms |
10736 KB |
Output is correct |
71 |
Correct |
168 ms |
10736 KB |
Output is correct |
72 |
Correct |
215 ms |
10736 KB |
Output is correct |
73 |
Correct |
84 ms |
10736 KB |
Output is correct |
74 |
Correct |
122 ms |
10748 KB |
Output is correct |
75 |
Correct |
144 ms |
10748 KB |
Output is correct |
76 |
Correct |
182 ms |
10748 KB |
Output is correct |
77 |
Correct |
128 ms |
10748 KB |
Output is correct |
78 |
Correct |
162 ms |
10748 KB |
Output is correct |
79 |
Correct |
126 ms |
10748 KB |
Output is correct |
80 |
Correct |
112 ms |
10748 KB |
Output is correct |
81 |
Correct |
219 ms |
10748 KB |
Output is correct |
82 |
Correct |
124 ms |
10748 KB |
Output is correct |
83 |
Correct |
134 ms |
10748 KB |
Output is correct |
84 |
Correct |
213 ms |
10748 KB |
Output is correct |
85 |
Correct |
183 ms |
10748 KB |
Output is correct |
86 |
Correct |
171 ms |
10748 KB |
Output is correct |
87 |
Correct |
177 ms |
10748 KB |
Output is correct |
88 |
Correct |
144 ms |
10748 KB |
Output is correct |
89 |
Correct |
234 ms |
10748 KB |
Output is correct |
90 |
Correct |
185 ms |
10748 KB |
Output is correct |
91 |
Correct |
177 ms |
10748 KB |
Output is correct |
92 |
Correct |
173 ms |
10748 KB |
Output is correct |