#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
using namespace std;
int n,S,vis[805][805],tim[805][805];
pair<int,int> st,ed,bck[805][805];
char c[805][805];
vector<pair<int,int>> d,path;
queue<pair<pair<int,int>,pair<int,int>>> q;
bool ok(int x,int y){
return (1<=x && x<=n && 1<=y && y<=n);
}
bool pass(int T){
T*=S;
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) vis[i][j]=0;
queue<pair<pair<int,int>,int>> t; t.push(mp(st,T));
while (!t.empty()){
auto x=t.front(); t.pop();
if (x.f==ed) vis[x.f.f][x.f.s]=x.s;
else if (x.s>=(tim[x.f.f][x.f.s]-1)*S || vis[x.f.f][x.f.s]) continue;
vis[x.f.f][x.f.s]=x.s;
for (auto k:d){
int a=x.f.f+k.f; int b=x.f.s+k.s;
if (ok(a,b) && c[a][b]!='T') t.push(mp(mp(a,b),x.s+1));
}
}
return vis[ed.f][ed.s];
}
int main() {
cin>>n>>S;
d.push_back({1,0}); d.push_back({-1,0});
d.push_back({0,-1}); d.push_back({0,1});
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
cin>>c[i][j];
if (c[i][j]=='D') ed=mp(i,j);
if (c[i][j]=='M') st=mp(i,j);
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if (c[i][j]=='H') q.push(mp(mp(i,j),mp(0,0)));
}
}
while (!q.empty()){
auto x=q.front(); q.pop();
if (tim[x.f.f][x.f.s]) continue;
tim[x.f.f][x.f.s]=tim[x.s.f][x.s.s]+1;
for (auto k:d){
int a=x.f.f+k.f; int b=x.f.s+k.s;
if (ok(a,b) && c[a][b]!='T' && c[a][b]!='D') q.push(mp(mp(a,b),x.f));
}
}
if (pass(0)){
int ans=0;
int l=0; int r=n*n+5;
while (l<=r){
int mid=(l+r)/2;
if (pass(mid)){
ans=mid; l=mid+1;
}
else r=mid-1;
}
cout<<ans<<"\n";
}
else cout<<"-1\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
190 ms |
6752 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
12 ms |
2772 KB |
Output is correct |
34 |
Correct |
13 ms |
2772 KB |
Output is correct |
35 |
Correct |
43 ms |
2748 KB |
Output is correct |
36 |
Correct |
15 ms |
3156 KB |
Output is correct |
37 |
Correct |
16 ms |
3168 KB |
Output is correct |
38 |
Correct |
56 ms |
3076 KB |
Output is correct |
39 |
Correct |
18 ms |
3412 KB |
Output is correct |
40 |
Correct |
20 ms |
3504 KB |
Output is correct |
41 |
Correct |
77 ms |
3480 KB |
Output is correct |
42 |
Correct |
23 ms |
3856 KB |
Output is correct |
43 |
Correct |
25 ms |
3828 KB |
Output is correct |
44 |
Correct |
98 ms |
3880 KB |
Output is correct |
45 |
Correct |
27 ms |
4180 KB |
Output is correct |
46 |
Correct |
33 ms |
4280 KB |
Output is correct |
47 |
Correct |
105 ms |
4196 KB |
Output is correct |
48 |
Correct |
32 ms |
4464 KB |
Output is correct |
49 |
Correct |
35 ms |
4556 KB |
Output is correct |
50 |
Correct |
129 ms |
4604 KB |
Output is correct |
51 |
Correct |
37 ms |
4852 KB |
Output is correct |
52 |
Correct |
41 ms |
4820 KB |
Output is correct |
53 |
Correct |
161 ms |
4956 KB |
Output is correct |
54 |
Correct |
43 ms |
5196 KB |
Output is correct |
55 |
Correct |
46 ms |
5176 KB |
Output is correct |
56 |
Correct |
181 ms |
5344 KB |
Output is correct |
57 |
Correct |
49 ms |
5616 KB |
Output is correct |
58 |
Correct |
54 ms |
5620 KB |
Output is correct |
59 |
Correct |
221 ms |
5668 KB |
Output is correct |
60 |
Correct |
55 ms |
5960 KB |
Output is correct |
61 |
Correct |
61 ms |
5964 KB |
Output is correct |
62 |
Correct |
235 ms |
6032 KB |
Output is correct |
63 |
Correct |
162 ms |
5992 KB |
Output is correct |
64 |
Correct |
266 ms |
5988 KB |
Output is correct |
65 |
Correct |
209 ms |
5964 KB |
Output is correct |
66 |
Correct |
175 ms |
5892 KB |
Output is correct |
67 |
Correct |
64 ms |
5964 KB |
Output is correct |
68 |
Correct |
87 ms |
6100 KB |
Output is correct |
69 |
Correct |
82 ms |
6004 KB |
Output is correct |
70 |
Correct |
68 ms |
6020 KB |
Output is correct |
71 |
Correct |
74 ms |
6008 KB |
Output is correct |
72 |
Correct |
67 ms |
6172 KB |
Output is correct |
73 |
Correct |
91 ms |
8140 KB |
Output is correct |
74 |
Correct |
154 ms |
8060 KB |
Output is correct |
75 |
Correct |
157 ms |
8132 KB |
Output is correct |
76 |
Correct |
145 ms |
7900 KB |
Output is correct |
77 |
Correct |
141 ms |
7900 KB |
Output is correct |
78 |
Correct |
78 ms |
7884 KB |
Output is correct |
79 |
Correct |
140 ms |
7884 KB |
Output is correct |
80 |
Correct |
140 ms |
7960 KB |
Output is correct |
81 |
Correct |
159 ms |
8016 KB |
Output is correct |
82 |
Correct |
154 ms |
7872 KB |
Output is correct |
83 |
Correct |
173 ms |
7632 KB |
Output is correct |
84 |
Correct |
180 ms |
7640 KB |
Output is correct |
85 |
Correct |
173 ms |
7640 KB |
Output is correct |
86 |
Correct |
165 ms |
7516 KB |
Output is correct |
87 |
Correct |
167 ms |
7576 KB |
Output is correct |
88 |
Correct |
182 ms |
7184 KB |
Output is correct |
89 |
Correct |
193 ms |
7244 KB |
Output is correct |
90 |
Correct |
188 ms |
6952 KB |
Output is correct |
91 |
Correct |
187 ms |
7264 KB |
Output is correct |
92 |
Correct |
193 ms |
7300 KB |
Output is correct |