#include<bits/stdc++.h>
using namespace std;
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)1e9;
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 |
1 ms |
212 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
7 |
Incorrect |
89 ms |
6816 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 |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
21 |
Correct |
1 ms |
316 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 |
324 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 |
9 ms |
1556 KB |
Output is correct |
34 |
Incorrect |
15 ms |
1552 KB |
Output isn't correct |
35 |
Incorrect |
23 ms |
1564 KB |
Output isn't correct |
36 |
Correct |
8 ms |
1880 KB |
Output is correct |
37 |
Incorrect |
16 ms |
1876 KB |
Output isn't correct |
38 |
Incorrect |
31 ms |
2076 KB |
Output isn't correct |
39 |
Correct |
14 ms |
2356 KB |
Output is correct |
40 |
Incorrect |
22 ms |
2360 KB |
Output isn't correct |
41 |
Incorrect |
42 ms |
2420 KB |
Output isn't correct |
42 |
Correct |
11 ms |
2952 KB |
Output is correct |
43 |
Incorrect |
19 ms |
2944 KB |
Output isn't correct |
44 |
Incorrect |
55 ms |
2904 KB |
Output isn't correct |
45 |
Correct |
16 ms |
3444 KB |
Output is correct |
46 |
Incorrect |
32 ms |
3412 KB |
Output isn't correct |
47 |
Incorrect |
69 ms |
3428 KB |
Output isn't correct |
48 |
Correct |
21 ms |
4024 KB |
Output is correct |
49 |
Incorrect |
50 ms |
3936 KB |
Output isn't correct |
50 |
Incorrect |
83 ms |
4108 KB |
Output isn't correct |
51 |
Correct |
25 ms |
4540 KB |
Output is correct |
52 |
Incorrect |
59 ms |
4596 KB |
Output isn't correct |
53 |
Incorrect |
93 ms |
4628 KB |
Output isn't correct |
54 |
Correct |
35 ms |
5272 KB |
Output is correct |
55 |
Incorrect |
78 ms |
5244 KB |
Output isn't correct |
56 |
Incorrect |
107 ms |
5308 KB |
Output isn't correct |
57 |
Correct |
27 ms |
6024 KB |
Output is correct |
58 |
Incorrect |
49 ms |
5996 KB |
Output isn't correct |
59 |
Incorrect |
136 ms |
5996 KB |
Output isn't correct |
60 |
Correct |
33 ms |
6796 KB |
Output is correct |
61 |
Incorrect |
91 ms |
6756 KB |
Output isn't correct |
62 |
Incorrect |
141 ms |
6852 KB |
Output isn't correct |
63 |
Incorrect |
38 ms |
6804 KB |
Output isn't correct |
64 |
Incorrect |
43 ms |
6920 KB |
Output isn't correct |
65 |
Incorrect |
40 ms |
6792 KB |
Output isn't correct |
66 |
Incorrect |
37 ms |
6916 KB |
Output isn't correct |
67 |
Incorrect |
44 ms |
6804 KB |
Output isn't correct |
68 |
Incorrect |
42 ms |
6820 KB |
Output isn't correct |
69 |
Incorrect |
43 ms |
6800 KB |
Output isn't correct |
70 |
Incorrect |
47 ms |
6800 KB |
Output isn't correct |
71 |
Incorrect |
43 ms |
6800 KB |
Output isn't correct |
72 |
Correct |
40 ms |
6796 KB |
Output is correct |
73 |
Correct |
46 ms |
6928 KB |
Output is correct |
74 |
Correct |
66 ms |
6804 KB |
Output is correct |
75 |
Incorrect |
76 ms |
6780 KB |
Output isn't correct |
76 |
Correct |
74 ms |
6980 KB |
Output is correct |
77 |
Incorrect |
73 ms |
6812 KB |
Output isn't correct |
78 |
Correct |
78 ms |
6812 KB |
Output is correct |
79 |
Incorrect |
63 ms |
6796 KB |
Output isn't correct |
80 |
Incorrect |
66 ms |
6832 KB |
Output isn't correct |
81 |
Incorrect |
74 ms |
6796 KB |
Output isn't correct |
82 |
Incorrect |
66 ms |
6800 KB |
Output isn't correct |
83 |
Incorrect |
92 ms |
6864 KB |
Output isn't correct |
84 |
Correct |
77 ms |
6868 KB |
Output is correct |
85 |
Incorrect |
78 ms |
6804 KB |
Output isn't correct |
86 |
Incorrect |
92 ms |
6812 KB |
Output isn't correct |
87 |
Incorrect |
80 ms |
6816 KB |
Output isn't correct |
88 |
Incorrect |
83 ms |
6912 KB |
Output isn't correct |
89 |
Correct |
87 ms |
6796 KB |
Output is correct |
90 |
Incorrect |
89 ms |
6776 KB |
Output isn't correct |
91 |
Incorrect |
88 ms |
6796 KB |
Output isn't correct |
92 |
Incorrect |
86 ms |
6800 KB |
Output isn't correct |