#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((d[li][ci]-1)/S+val>=a[li][ci]) continue;
if(li==l2&&ci==c2) return 1;
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 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Incorrect |
3 ms |
480 KB |
Output isn't correct |
5 |
Correct |
3 ms |
480 KB |
Output is correct |
6 |
Incorrect |
2 ms |
480 KB |
Output isn't correct |
7 |
Incorrect |
146 ms |
10680 KB |
Output isn't correct |
8 |
Correct |
3 ms |
10680 KB |
Output is correct |
9 |
Correct |
2 ms |
10680 KB |
Output is correct |
10 |
Correct |
2 ms |
10680 KB |
Output is correct |
11 |
Correct |
2 ms |
10680 KB |
Output is correct |
12 |
Incorrect |
4 ms |
10680 KB |
Output isn't correct |
13 |
Correct |
3 ms |
10680 KB |
Output is correct |
14 |
Correct |
4 ms |
10680 KB |
Output is correct |
15 |
Correct |
3 ms |
10680 KB |
Output is correct |
16 |
Correct |
3 ms |
10680 KB |
Output is correct |
17 |
Correct |
2 ms |
10680 KB |
Output is correct |
18 |
Correct |
2 ms |
10680 KB |
Output is correct |
19 |
Correct |
3 ms |
10680 KB |
Output is correct |
20 |
Correct |
3 ms |
10680 KB |
Output is correct |
21 |
Correct |
3 ms |
10680 KB |
Output is correct |
22 |
Correct |
6 ms |
10680 KB |
Output is correct |
23 |
Correct |
7 ms |
10680 KB |
Output is correct |
24 |
Correct |
3 ms |
10680 KB |
Output is correct |
25 |
Correct |
3 ms |
10680 KB |
Output is correct |
26 |
Correct |
3 ms |
10680 KB |
Output is correct |
27 |
Correct |
4 ms |
10680 KB |
Output is correct |
28 |
Correct |
4 ms |
10680 KB |
Output is correct |
29 |
Correct |
3 ms |
10680 KB |
Output is correct |
30 |
Correct |
3 ms |
10680 KB |
Output is correct |
31 |
Correct |
3 ms |
10680 KB |
Output is correct |
32 |
Correct |
4 ms |
10680 KB |
Output is correct |
33 |
Correct |
16 ms |
10680 KB |
Output is correct |
34 |
Correct |
19 ms |
10680 KB |
Output is correct |
35 |
Correct |
36 ms |
10680 KB |
Output is correct |
36 |
Correct |
14 ms |
10680 KB |
Output is correct |
37 |
Correct |
27 ms |
10680 KB |
Output is correct |
38 |
Correct |
47 ms |
10680 KB |
Output is correct |
39 |
Correct |
27 ms |
10680 KB |
Output is correct |
40 |
Correct |
30 ms |
10680 KB |
Output is correct |
41 |
Correct |
98 ms |
10680 KB |
Output is correct |
42 |
Correct |
26 ms |
10680 KB |
Output is correct |
43 |
Correct |
22 ms |
10680 KB |
Output is correct |
44 |
Correct |
75 ms |
10680 KB |
Output is correct |
45 |
Correct |
28 ms |
10680 KB |
Output is correct |
46 |
Correct |
45 ms |
10680 KB |
Output is correct |
47 |
Correct |
91 ms |
10680 KB |
Output is correct |
48 |
Correct |
35 ms |
10680 KB |
Output is correct |
49 |
Correct |
39 ms |
10680 KB |
Output is correct |
50 |
Correct |
139 ms |
10680 KB |
Output is correct |
51 |
Correct |
44 ms |
10680 KB |
Output is correct |
52 |
Correct |
38 ms |
10680 KB |
Output is correct |
53 |
Correct |
219 ms |
10680 KB |
Output is correct |
54 |
Correct |
42 ms |
10680 KB |
Output is correct |
55 |
Correct |
48 ms |
10680 KB |
Output is correct |
56 |
Correct |
277 ms |
10680 KB |
Output is correct |
57 |
Correct |
46 ms |
10680 KB |
Output is correct |
58 |
Correct |
47 ms |
10680 KB |
Output is correct |
59 |
Correct |
234 ms |
10680 KB |
Output is correct |
60 |
Correct |
50 ms |
10680 KB |
Output is correct |
61 |
Correct |
59 ms |
10680 KB |
Output is correct |
62 |
Correct |
257 ms |
10680 KB |
Output is correct |
63 |
Correct |
255 ms |
10680 KB |
Output is correct |
64 |
Correct |
319 ms |
10680 KB |
Output is correct |
65 |
Correct |
369 ms |
10680 KB |
Output is correct |
66 |
Correct |
278 ms |
10680 KB |
Output is correct |
67 |
Correct |
284 ms |
10680 KB |
Output is correct |
68 |
Correct |
161 ms |
10680 KB |
Output is correct |
69 |
Correct |
169 ms |
10680 KB |
Output is correct |
70 |
Correct |
145 ms |
10680 KB |
Output is correct |
71 |
Correct |
136 ms |
10680 KB |
Output is correct |
72 |
Correct |
144 ms |
10680 KB |
Output is correct |
73 |
Correct |
75 ms |
10680 KB |
Output is correct |
74 |
Correct |
114 ms |
10720 KB |
Output is correct |
75 |
Correct |
157 ms |
10720 KB |
Output is correct |
76 |
Correct |
131 ms |
10748 KB |
Output is correct |
77 |
Correct |
143 ms |
10748 KB |
Output is correct |
78 |
Correct |
164 ms |
10748 KB |
Output is correct |
79 |
Correct |
120 ms |
10748 KB |
Output is correct |
80 |
Correct |
128 ms |
10748 KB |
Output is correct |
81 |
Correct |
148 ms |
10748 KB |
Output is correct |
82 |
Correct |
128 ms |
10748 KB |
Output is correct |
83 |
Correct |
156 ms |
10748 KB |
Output is correct |
84 |
Correct |
138 ms |
10748 KB |
Output is correct |
85 |
Correct |
156 ms |
10748 KB |
Output is correct |
86 |
Correct |
194 ms |
10748 KB |
Output is correct |
87 |
Correct |
169 ms |
10748 KB |
Output is correct |
88 |
Correct |
158 ms |
10748 KB |
Output is correct |
89 |
Correct |
188 ms |
10748 KB |
Output is correct |
90 |
Correct |
195 ms |
10748 KB |
Output is correct |
91 |
Correct |
184 ms |
10748 KB |
Output is correct |
92 |
Correct |
171 ms |
10748 KB |
Output is correct |