#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' || v[x+dx[i]][y+dy[i]]=='D'){
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])){
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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Incorrect |
118 ms |
11224 KB |
Output isn't correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't 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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
33 |
Correct |
11 ms |
2532 KB |
Output is correct |
34 |
Incorrect |
16 ms |
2404 KB |
Output isn't correct |
35 |
Incorrect |
33 ms |
2532 KB |
Output isn't correct |
36 |
Correct |
16 ms |
3032 KB |
Output is correct |
37 |
Incorrect |
24 ms |
3032 KB |
Output isn't correct |
38 |
Incorrect |
40 ms |
3164 KB |
Output isn't correct |
39 |
Correct |
21 ms |
3884 KB |
Output is correct |
40 |
Incorrect |
35 ms |
3756 KB |
Output isn't correct |
41 |
Incorrect |
49 ms |
3760 KB |
Output isn't correct |
42 |
Correct |
23 ms |
4704 KB |
Output is correct |
43 |
Incorrect |
33 ms |
4676 KB |
Output isn't correct |
44 |
Incorrect |
65 ms |
4660 KB |
Output isn't correct |
45 |
Correct |
28 ms |
5552 KB |
Output is correct |
46 |
Incorrect |
40 ms |
5560 KB |
Output isn't correct |
47 |
Incorrect |
82 ms |
5528 KB |
Output isn't correct |
48 |
Correct |
35 ms |
6596 KB |
Output is correct |
49 |
Incorrect |
74 ms |
6480 KB |
Output isn't correct |
50 |
Incorrect |
99 ms |
6628 KB |
Output isn't correct |
51 |
Correct |
43 ms |
7628 KB |
Output is correct |
52 |
Incorrect |
73 ms |
7536 KB |
Output isn't correct |
53 |
Incorrect |
113 ms |
7664 KB |
Output isn't correct |
54 |
Correct |
42 ms |
8796 KB |
Output is correct |
55 |
Incorrect |
70 ms |
8724 KB |
Output isn't correct |
56 |
Incorrect |
138 ms |
8788 KB |
Output isn't correct |
57 |
Correct |
68 ms |
9908 KB |
Output is correct |
58 |
Incorrect |
100 ms |
9976 KB |
Output isn't correct |
59 |
Incorrect |
162 ms |
10000 KB |
Output isn't correct |
60 |
Correct |
65 ms |
11284 KB |
Output is correct |
61 |
Incorrect |
115 ms |
11304 KB |
Output isn't correct |
62 |
Incorrect |
238 ms |
11312 KB |
Output isn't correct |
63 |
Incorrect |
68 ms |
11236 KB |
Output isn't correct |
64 |
Incorrect |
72 ms |
11392 KB |
Output isn't correct |
65 |
Incorrect |
70 ms |
11300 KB |
Output isn't correct |
66 |
Incorrect |
66 ms |
11456 KB |
Output isn't correct |
67 |
Incorrect |
78 ms |
11260 KB |
Output isn't correct |
68 |
Incorrect |
71 ms |
11320 KB |
Output isn't correct |
69 |
Incorrect |
77 ms |
11440 KB |
Output isn't correct |
70 |
Incorrect |
72 ms |
11276 KB |
Output isn't correct |
71 |
Incorrect |
75 ms |
11452 KB |
Output isn't correct |
72 |
Correct |
67 ms |
11380 KB |
Output is correct |
73 |
Correct |
74 ms |
11328 KB |
Output is correct |
74 |
Correct |
93 ms |
11480 KB |
Output is correct |
75 |
Incorrect |
99 ms |
11352 KB |
Output isn't correct |
76 |
Correct |
96 ms |
11268 KB |
Output is correct |
77 |
Incorrect |
99 ms |
11344 KB |
Output isn't correct |
78 |
Correct |
103 ms |
11268 KB |
Output is correct |
79 |
Incorrect |
92 ms |
11316 KB |
Output isn't correct |
80 |
Incorrect |
95 ms |
11260 KB |
Output isn't correct |
81 |
Incorrect |
101 ms |
11332 KB |
Output isn't correct |
82 |
Incorrect |
86 ms |
11388 KB |
Output isn't correct |
83 |
Incorrect |
110 ms |
11312 KB |
Output isn't correct |
84 |
Correct |
116 ms |
11288 KB |
Output is correct |
85 |
Incorrect |
109 ms |
11300 KB |
Output isn't correct |
86 |
Incorrect |
111 ms |
11288 KB |
Output isn't correct |
87 |
Incorrect |
108 ms |
11328 KB |
Output isn't correct |
88 |
Incorrect |
115 ms |
11324 KB |
Output isn't correct |
89 |
Correct |
137 ms |
11428 KB |
Output is correct |
90 |
Incorrect |
116 ms |
11328 KB |
Output isn't correct |
91 |
Incorrect |
114 ms |
11340 KB |
Output isn't correct |
92 |
Incorrect |
116 ms |
11292 KB |
Output isn't correct |