#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define endl "\n"
#define ll long long
//#define int long long
void fopn(string name){
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
const ll INF=1e18,mod=1e9,N=1e7+5;
int n,s;
char a[805][805];
pair<int,int> cur,last;
int dist[805][805][2];
vector<int> dx={1,-1,0, 0},
dy={0, 0,1,-1};
bool okh(int x, int y){
return (x>0 && x<=n && y>0 && y<=n && (a[x][y]=='M' || a[x][y]=='G' || a[x][y]=='H'));
}
bool okm(int x, int y){
return (x>0 && x<=n && y>0 && y<=n && (a[x][y]=='M' || a[x][y]=='G' || a[x][y]=='D'));
}
queue<pair<int,int>> dq;
bool check(int D){
if(dist[cur.fr][cur.sc][0]<=D) return 0;
while(!dq.empty()) dq.pop();
dq.push({cur.fr,cur.sc});
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
dist[i][j][1]=0;
}
dist[cur.fr][cur.sc][1]=1;
while(!dq.empty()){
pair<int,int> a=dq.front();
dq.pop();
for(int i=0;i<4;i++){
int x=a.fr+dx[i],y=a.sc+dy[i];
int d=dist[a.fr][a.sc][1]+1;
if(okm(x,y) && !dist[x][y][1]){
if((d-1)/s+1+D<=dist[x][y][0]){
dq.push({x,y});
dist[x][y][1]=d;
}
if(x==last.fr && y==last.sc && (d-1)/s+D-((d-1)%s==0)<=dist[x][y][0]) return 1;
}
}
}
return dist[last.fr][last.sc][1];
}
void solve(){
queue<pair<int,int>> dq;
cin>>n>>s;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
if(a[i][j]=='H'){
dq.push({i,j});
dist[i][j][0]=1;
}
else if(a[i][j]=='M')
cur={i,j};
else if(a[i][j]=='D')
last={i,j};
}
}
while(!dq.empty()){
pair<int,int> a=dq.front();
dq.pop();
for(int i=0;i<4;i++){
int x=a.fr+dx[i],y=a.sc+dy[i];
if(okh(x,y) && !dist[x][y][0]){
dist[x][y][0]=dist[a.fr][a.sc][0]+1;
dq.push({x,y});
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j][0]-=(dist[i][j][0]>0);
}
}
dist[last.fr][last.sc][0]=2e9;
int l=-1,r=1e9;
while(l<r){
int md=(l+r)/2;
if(check(md)) l=md;
else r=md-1;
}
cout<<l<<endl;
}
main(){
ios;
int T=1;
//cin>>T;
for(int i=1;i<=T;i++){
//cout<<"Case #"<<i<<": ";
solve();
}
}
Compilation message
mecho.cpp:97:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
97 | main(){
| ^~~~
mecho.cpp: In function 'void fopn(std::string)':
mecho.cpp:13:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:14:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Execution timed out |
1099 ms |
6036 KB |
Time limit exceeded |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Execution timed out |
1083 ms |
340 KB |
Time limit exceeded |
11 |
Execution timed out |
1086 ms |
340 KB |
Time limit exceeded |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Execution timed out |
1088 ms |
340 KB |
Time limit exceeded |
16 |
Execution timed out |
1075 ms |
340 KB |
Time limit exceeded |
17 |
Execution timed out |
1079 ms |
340 KB |
Time limit exceeded |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Execution timed out |
1091 ms |
468 KB |
Time limit exceeded |
21 |
Execution timed out |
1079 ms |
468 KB |
Time limit exceeded |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Execution timed out |
1081 ms |
468 KB |
Time limit exceeded |
25 |
Execution timed out |
1071 ms |
596 KB |
Time limit exceeded |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Execution timed out |
1081 ms |
596 KB |
Time limit exceeded |
28 |
Execution timed out |
1082 ms |
536 KB |
Time limit exceeded |
29 |
Execution timed out |
1084 ms |
596 KB |
Time limit exceeded |
30 |
Execution timed out |
1083 ms |
596 KB |
Time limit exceeded |
31 |
Execution timed out |
1086 ms |
724 KB |
Time limit exceeded |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Execution timed out |
1081 ms |
2720 KB |
Time limit exceeded |
34 |
Correct |
4 ms |
2772 KB |
Output is correct |
35 |
Execution timed out |
1089 ms |
2772 KB |
Time limit exceeded |
36 |
Execution timed out |
1063 ms |
3136 KB |
Time limit exceeded |
37 |
Execution timed out |
1071 ms |
3156 KB |
Time limit exceeded |
38 |
Execution timed out |
1082 ms |
3156 KB |
Time limit exceeded |
39 |
Correct |
9 ms |
3428 KB |
Output is correct |
40 |
Execution timed out |
1078 ms |
3412 KB |
Time limit exceeded |
41 |
Execution timed out |
1064 ms |
3412 KB |
Time limit exceeded |
42 |
Execution timed out |
1062 ms |
3796 KB |
Time limit exceeded |
43 |
Execution timed out |
1061 ms |
3796 KB |
Time limit exceeded |
44 |
Execution timed out |
1085 ms |
3812 KB |
Time limit exceeded |
45 |
Execution timed out |
1076 ms |
4180 KB |
Time limit exceeded |
46 |
Execution timed out |
1075 ms |
4180 KB |
Time limit exceeded |
47 |
Execution timed out |
1072 ms |
4136 KB |
Time limit exceeded |
48 |
Execution timed out |
1085 ms |
4492 KB |
Time limit exceeded |
49 |
Execution timed out |
1083 ms |
4564 KB |
Time limit exceeded |
50 |
Execution timed out |
1063 ms |
4564 KB |
Time limit exceeded |
51 |
Execution timed out |
1077 ms |
4820 KB |
Time limit exceeded |
52 |
Correct |
20 ms |
4820 KB |
Output is correct |
53 |
Execution timed out |
1081 ms |
4872 KB |
Time limit exceeded |
54 |
Execution timed out |
1075 ms |
5236 KB |
Time limit exceeded |
55 |
Correct |
16 ms |
5236 KB |
Output is correct |
56 |
Execution timed out |
1061 ms |
5248 KB |
Time limit exceeded |
57 |
Correct |
24 ms |
5632 KB |
Output is correct |
58 |
Execution timed out |
1079 ms |
5548 KB |
Time limit exceeded |
59 |
Execution timed out |
1090 ms |
5548 KB |
Time limit exceeded |
60 |
Execution timed out |
1074 ms |
5884 KB |
Time limit exceeded |
61 |
Execution timed out |
1071 ms |
5880 KB |
Time limit exceeded |
62 |
Execution timed out |
1072 ms |
5924 KB |
Time limit exceeded |
63 |
Correct |
117 ms |
5988 KB |
Output is correct |
64 |
Correct |
211 ms |
5992 KB |
Output is correct |
65 |
Execution timed out |
1063 ms |
5984 KB |
Time limit exceeded |
66 |
Correct |
119 ms |
5992 KB |
Output is correct |
67 |
Correct |
106 ms |
5988 KB |
Output is correct |
68 |
Correct |
58 ms |
5972 KB |
Output is correct |
69 |
Execution timed out |
1069 ms |
5972 KB |
Time limit exceeded |
70 |
Execution timed out |
1075 ms |
5928 KB |
Time limit exceeded |
71 |
Execution timed out |
1073 ms |
5968 KB |
Time limit exceeded |
72 |
Correct |
30 ms |
5972 KB |
Output is correct |
73 |
Correct |
34 ms |
6268 KB |
Output is correct |
74 |
Execution timed out |
1083 ms |
6284 KB |
Time limit exceeded |
75 |
Correct |
92 ms |
6272 KB |
Output is correct |
76 |
Execution timed out |
1091 ms |
6264 KB |
Time limit exceeded |
77 |
Execution timed out |
1080 ms |
6356 KB |
Time limit exceeded |
78 |
Correct |
75 ms |
6244 KB |
Output is correct |
79 |
Correct |
54 ms |
6244 KB |
Output is correct |
80 |
Execution timed out |
1090 ms |
6176 KB |
Time limit exceeded |
81 |
Correct |
77 ms |
6228 KB |
Output is correct |
82 |
Execution timed out |
1088 ms |
6184 KB |
Time limit exceeded |
83 |
Correct |
86 ms |
6212 KB |
Output is correct |
84 |
Execution timed out |
1076 ms |
6144 KB |
Time limit exceeded |
85 |
Execution timed out |
1062 ms |
6208 KB |
Time limit exceeded |
86 |
Execution timed out |
1073 ms |
6180 KB |
Time limit exceeded |
87 |
Correct |
83 ms |
6208 KB |
Output is correct |
88 |
Correct |
97 ms |
6144 KB |
Output is correct |
89 |
Execution timed out |
1092 ms |
6140 KB |
Time limit exceeded |
90 |
Execution timed out |
1076 ms |
6032 KB |
Time limit exceeded |
91 |
Execution timed out |
1066 ms |
6096 KB |
Time limit exceeded |
92 |
Execution timed out |
1081 ms |
6100 KB |
Time limit exceeded |