#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int n,s;
cin>>n>>s;
vector<string> v(n);
for (int i=0;i<n;i++){
cin>>v[i];
}
int inf=(int)1e12;
vector<vector<int>> dis(n,vector<int>(n,inf));
queue<pair<int,int>> q;
pair<int,int> start={-1,-1};
pair<int,int> end={-1,-1};
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (v[i][j]=='H'){
q.push({i,j});
dis[i][j]=0;
}
if (v[i][j]=='M'){
start={i,j};
}
if (v[i][j]=='D'){
end={i,j};
}
}
}
auto valid = [&] (int x,int y){
return x>=0 && x<n && y>=0 && y<n;
};
vector<int> dx={0,-1,0,1};
vector<int> dy={-1,0,1,0};
while(!q.empty()){
auto [x,y]=q.front();
q.pop();
for (int i=0;i<4;i++){
if (valid(x+dx[i],y+dy[i]) && dis[x+dx[i]][y+dy[i]]==inf){
if (v[x+dx[i]][y+dy[i]]=='G'){
dis[x+dx[i]][y+dy[i]]=1+dis[x][y];
q.push({x+dx[i],y+dy[i]});
}
if (v[x+dx[i]][y+dy[i]]=='M'){
dis[x+dx[i]][y+dy[i]]=1+dis[x][y];
}
}
}
}
int low=0,high=inf;
int ans=-1;
while(low<=high){
int mid=(low+high)/2;
vector<vector<int>> dis2(n,vector<int>(n,inf));
q.push(start);
if (mid>=dis[start.first][start.second]){
q.pop();
}
dis2[start.first][start.second]=mid*s;
while(!q.empty()){
auto [x,y]=q.front();
q.pop();
for (int i=0;i<4;i++){
if (valid(x+dx[i],y+dy[i]) && v[x+dx[i]][y+dy[i]]!='T'){
if ((dis2[x][y]+1)/s<dis[x+dx[i]][y+dy[i]] && dis2[x+dx[i]][y+dy[i]]==inf){
dis2[x+dx[i]][y+dy[i]]=1+dis2[x][y];
q.push({x+dx[i],y+dy[i]});
}
}
}
}
if (dis2[end.first][end.second]==inf){
high=mid-1;
}
else{
ans=max(ans,mid);
low=mid+1;
}
}
cout<<ans<<"\n";
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
132 ms |
11284 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
10 ms |
2536 KB |
Output is correct |
34 |
Correct |
11 ms |
2404 KB |
Output is correct |
35 |
Correct |
28 ms |
2408 KB |
Output is correct |
36 |
Correct |
14 ms |
3168 KB |
Output is correct |
37 |
Correct |
14 ms |
3036 KB |
Output is correct |
38 |
Correct |
45 ms |
3036 KB |
Output is correct |
39 |
Correct |
18 ms |
3888 KB |
Output is correct |
40 |
Correct |
18 ms |
3748 KB |
Output is correct |
41 |
Correct |
47 ms |
3868 KB |
Output is correct |
42 |
Correct |
22 ms |
4704 KB |
Output is correct |
43 |
Correct |
22 ms |
4548 KB |
Output is correct |
44 |
Correct |
81 ms |
4556 KB |
Output is correct |
45 |
Correct |
28 ms |
5548 KB |
Output is correct |
46 |
Correct |
27 ms |
5576 KB |
Output is correct |
47 |
Correct |
72 ms |
5544 KB |
Output is correct |
48 |
Correct |
39 ms |
6612 KB |
Output is correct |
49 |
Correct |
33 ms |
6508 KB |
Output is correct |
50 |
Correct |
85 ms |
6660 KB |
Output is correct |
51 |
Correct |
39 ms |
7776 KB |
Output is correct |
52 |
Correct |
39 ms |
7600 KB |
Output is correct |
53 |
Correct |
103 ms |
7548 KB |
Output is correct |
54 |
Correct |
44 ms |
8752 KB |
Output is correct |
55 |
Correct |
45 ms |
8760 KB |
Output is correct |
56 |
Correct |
133 ms |
8772 KB |
Output is correct |
57 |
Correct |
51 ms |
9952 KB |
Output is correct |
58 |
Correct |
48 ms |
10028 KB |
Output is correct |
59 |
Correct |
146 ms |
10012 KB |
Output is correct |
60 |
Correct |
58 ms |
11424 KB |
Output is correct |
61 |
Correct |
67 ms |
11256 KB |
Output is correct |
62 |
Correct |
164 ms |
11284 KB |
Output is correct |
63 |
Correct |
134 ms |
11360 KB |
Output is correct |
64 |
Correct |
187 ms |
11260 KB |
Output is correct |
65 |
Correct |
201 ms |
11496 KB |
Output is correct |
66 |
Correct |
155 ms |
11268 KB |
Output is correct |
67 |
Correct |
137 ms |
11196 KB |
Output is correct |
68 |
Correct |
88 ms |
11444 KB |
Output is correct |
69 |
Correct |
81 ms |
11380 KB |
Output is correct |
70 |
Correct |
69 ms |
11304 KB |
Output is correct |
71 |
Correct |
73 ms |
11336 KB |
Output is correct |
72 |
Correct |
73 ms |
11416 KB |
Output is correct |
73 |
Correct |
75 ms |
11320 KB |
Output is correct |
74 |
Correct |
97 ms |
11368 KB |
Output is correct |
75 |
Correct |
116 ms |
11324 KB |
Output is correct |
76 |
Correct |
101 ms |
11300 KB |
Output is correct |
77 |
Correct |
100 ms |
11460 KB |
Output is correct |
78 |
Correct |
104 ms |
11344 KB |
Output is correct |
79 |
Correct |
90 ms |
11424 KB |
Output is correct |
80 |
Correct |
108 ms |
11260 KB |
Output is correct |
81 |
Correct |
107 ms |
11320 KB |
Output is correct |
82 |
Correct |
92 ms |
11292 KB |
Output is correct |
83 |
Correct |
117 ms |
11304 KB |
Output is correct |
84 |
Correct |
112 ms |
11292 KB |
Output is correct |
85 |
Correct |
116 ms |
11388 KB |
Output is correct |
86 |
Correct |
131 ms |
11408 KB |
Output is correct |
87 |
Correct |
109 ms |
11344 KB |
Output is correct |
88 |
Correct |
120 ms |
11304 KB |
Output is correct |
89 |
Correct |
127 ms |
11340 KB |
Output is correct |
90 |
Correct |
139 ms |
11352 KB |
Output is correct |
91 |
Correct |
140 ms |
11356 KB |
Output is correct |
92 |
Correct |
130 ms |
11260 KB |
Output is correct |