#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=2000;
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 |
2 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 |
268 KB |
Output isn't correct |
7 |
Incorrect |
78 ms |
6744 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 |
316 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
21 |
Correct |
1 ms |
212 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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
33 |
Incorrect |
17 ms |
1492 KB |
Output isn't correct |
34 |
Incorrect |
32 ms |
1492 KB |
Output isn't correct |
35 |
Incorrect |
25 ms |
1580 KB |
Output isn't correct |
36 |
Incorrect |
22 ms |
1876 KB |
Output isn't correct |
37 |
Incorrect |
40 ms |
1876 KB |
Output isn't correct |
38 |
Incorrect |
37 ms |
1968 KB |
Output isn't correct |
39 |
Incorrect |
27 ms |
2344 KB |
Output isn't correct |
40 |
Incorrect |
51 ms |
2364 KB |
Output isn't correct |
41 |
Incorrect |
41 ms |
2356 KB |
Output isn't correct |
42 |
Incorrect |
34 ms |
2824 KB |
Output isn't correct |
43 |
Incorrect |
63 ms |
2772 KB |
Output isn't correct |
44 |
Incorrect |
55 ms |
2840 KB |
Output isn't correct |
45 |
Incorrect |
39 ms |
3284 KB |
Output isn't correct |
46 |
Incorrect |
81 ms |
3272 KB |
Output isn't correct |
47 |
Incorrect |
68 ms |
3352 KB |
Output isn't correct |
48 |
Incorrect |
45 ms |
3940 KB |
Output isn't correct |
49 |
Incorrect |
94 ms |
3932 KB |
Output isn't correct |
50 |
Incorrect |
80 ms |
3928 KB |
Output isn't correct |
51 |
Incorrect |
52 ms |
4436 KB |
Output isn't correct |
52 |
Incorrect |
107 ms |
4544 KB |
Output isn't correct |
53 |
Incorrect |
94 ms |
4556 KB |
Output isn't correct |
54 |
Incorrect |
64 ms |
5212 KB |
Output isn't correct |
55 |
Incorrect |
128 ms |
5196 KB |
Output isn't correct |
56 |
Incorrect |
110 ms |
5224 KB |
Output isn't correct |
57 |
Incorrect |
67 ms |
5924 KB |
Output isn't correct |
58 |
Incorrect |
143 ms |
5832 KB |
Output isn't correct |
59 |
Incorrect |
138 ms |
5928 KB |
Output isn't correct |
60 |
Incorrect |
79 ms |
6612 KB |
Output isn't correct |
61 |
Incorrect |
169 ms |
6720 KB |
Output isn't correct |
62 |
Incorrect |
146 ms |
6700 KB |
Output isn't correct |
63 |
Incorrect |
39 ms |
6716 KB |
Output isn't correct |
64 |
Incorrect |
88 ms |
6720 KB |
Output isn't correct |
65 |
Incorrect |
62 ms |
6712 KB |
Output isn't correct |
66 |
Incorrect |
37 ms |
6772 KB |
Output isn't correct |
67 |
Incorrect |
57 ms |
6772 KB |
Output isn't correct |
68 |
Incorrect |
30 ms |
6756 KB |
Output isn't correct |
69 |
Incorrect |
37 ms |
6736 KB |
Output isn't correct |
70 |
Incorrect |
32 ms |
6756 KB |
Output isn't correct |
71 |
Incorrect |
39 ms |
6720 KB |
Output isn't correct |
72 |
Correct |
29 ms |
6744 KB |
Output is correct |
73 |
Correct |
35 ms |
6732 KB |
Output is correct |
74 |
Correct |
57 ms |
6744 KB |
Output is correct |
75 |
Incorrect |
61 ms |
6784 KB |
Output isn't correct |
76 |
Correct |
57 ms |
6752 KB |
Output is correct |
77 |
Incorrect |
58 ms |
6748 KB |
Output isn't correct |
78 |
Correct |
63 ms |
6740 KB |
Output is correct |
79 |
Incorrect |
52 ms |
6740 KB |
Output isn't correct |
80 |
Incorrect |
57 ms |
6740 KB |
Output isn't correct |
81 |
Incorrect |
61 ms |
6764 KB |
Output isn't correct |
82 |
Incorrect |
56 ms |
6720 KB |
Output isn't correct |
83 |
Incorrect |
72 ms |
6744 KB |
Output isn't correct |
84 |
Correct |
65 ms |
6740 KB |
Output is correct |
85 |
Incorrect |
65 ms |
6740 KB |
Output isn't correct |
86 |
Incorrect |
69 ms |
6748 KB |
Output isn't correct |
87 |
Incorrect |
78 ms |
6904 KB |
Output isn't correct |
88 |
Incorrect |
74 ms |
6728 KB |
Output isn't correct |
89 |
Correct |
74 ms |
6724 KB |
Output is correct |
90 |
Incorrect |
77 ms |
6704 KB |
Output isn't correct |
91 |
Incorrect |
74 ms |
6740 KB |
Output isn't correct |
92 |
Incorrect |
75 ms |
6740 KB |
Output isn't correct |