#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,s,dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},d[801][801],d2[801][801];
string m[801];
queue <pair <int, int>> q;
bool check(int x){
memset(d2,0,sizeof(d2));
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
if (m[i][j]=='M'){
q.push({i,j});
d2[i][j]=1;
}
while (!q.empty()){
auto [i,j]=q.front();
q.pop();
for (int k=0;k<4;k++){
int u=i+dx[k],v=j+dy[k];
if (min(u,v)>=0&&max(u,v)<n)
if (m[u][v]!='T'&&(d[u][v]-x*s>d2[i][j]||(d[u][v]-x*s>=d2[i][j]&&m[u][v]=='D'))&&!d2[u][v]){
d2[u][v]=d2[i][j]+1;
q.push({u,v});
}
}
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
if (m[i][j]=='D')
return d2[i][j];
}
int32_t main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n >> s;
for (int i=0;i<n;i++){
cin >> m[i];
for (int j=0;j<n;j++)
if (m[i][j]=='H'){
q.push({i,j});
d[i][j]=1;
}
}
while (!q.empty()){
auto [i,j]=q.front();
q.pop();
for (int k=0;k<4;k++){
int u=i+dx[k],v=j+dy[k];
if (min(u,v)>=0&&max(u,v)<n)
if (m[u][v]!='T'&&!d[u][v]){
d[u][v]=d[i][j]+1;
q.push({u,v});
}
}
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
d[i][j]=(d[i][j]-1)*s;
int l=0,r=1e9,kq=-1;
while (l<=r){
int mid=(l+r)>>1;
if (check(mid)){
kq=mid;
l=mid+1;
}
else
r=mid-1;
}
cout << kq;
}
Compilation message
mecho.cpp: In function 'bool check(long long int)':
mecho.cpp:31:1: warning: control reaches end of non-void function [-Wreturn-type]
31 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6744 KB |
Output is correct |
2 |
Incorrect |
3 ms |
6744 KB |
Output isn't correct |
3 |
Correct |
4 ms |
6748 KB |
Output is correct |
4 |
Incorrect |
3 ms |
6744 KB |
Output isn't correct |
5 |
Correct |
4 ms |
6744 KB |
Output is correct |
6 |
Incorrect |
4 ms |
6748 KB |
Output isn't correct |
7 |
Incorrect |
85 ms |
11356 KB |
Output isn't correct |
8 |
Correct |
3 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
6748 KB |
Output is correct |
10 |
Correct |
4 ms |
6748 KB |
Output is correct |
11 |
Correct |
4 ms |
6748 KB |
Output is correct |
12 |
Incorrect |
4 ms |
6804 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
6748 KB |
Output isn't correct |
14 |
Correct |
4 ms |
6748 KB |
Output is correct |
15 |
Correct |
4 ms |
6748 KB |
Output is correct |
16 |
Correct |
4 ms |
6748 KB |
Output is correct |
17 |
Correct |
4 ms |
6800 KB |
Output is correct |
18 |
Correct |
4 ms |
6748 KB |
Output is correct |
19 |
Correct |
3 ms |
6748 KB |
Output is correct |
20 |
Correct |
3 ms |
6748 KB |
Output is correct |
21 |
Correct |
4 ms |
6748 KB |
Output is correct |
22 |
Correct |
4 ms |
6748 KB |
Output is correct |
23 |
Correct |
3 ms |
6748 KB |
Output is correct |
24 |
Correct |
4 ms |
6748 KB |
Output is correct |
25 |
Correct |
4 ms |
6748 KB |
Output is correct |
26 |
Correct |
3 ms |
6748 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
4 ms |
6748 KB |
Output is correct |
29 |
Correct |
4 ms |
6808 KB |
Output is correct |
30 |
Correct |
4 ms |
6748 KB |
Output is correct |
31 |
Correct |
4 ms |
6648 KB |
Output is correct |
32 |
Correct |
3 ms |
6748 KB |
Output is correct |
33 |
Correct |
8 ms |
8816 KB |
Output is correct |
34 |
Correct |
10 ms |
7768 KB |
Output is correct |
35 |
Correct |
23 ms |
8796 KB |
Output is correct |
36 |
Correct |
9 ms |
8796 KB |
Output is correct |
37 |
Correct |
11 ms |
8028 KB |
Output is correct |
38 |
Correct |
28 ms |
8796 KB |
Output is correct |
39 |
Correct |
10 ms |
9052 KB |
Output is correct |
40 |
Correct |
14 ms |
8540 KB |
Output is correct |
41 |
Correct |
37 ms |
9052 KB |
Output is correct |
42 |
Correct |
11 ms |
9052 KB |
Output is correct |
43 |
Correct |
15 ms |
8796 KB |
Output is correct |
44 |
Correct |
46 ms |
9096 KB |
Output is correct |
45 |
Correct |
14 ms |
9048 KB |
Output is correct |
46 |
Correct |
19 ms |
9052 KB |
Output is correct |
47 |
Correct |
55 ms |
9048 KB |
Output is correct |
48 |
Correct |
16 ms |
9564 KB |
Output is correct |
49 |
Correct |
22 ms |
9564 KB |
Output is correct |
50 |
Correct |
67 ms |
9564 KB |
Output is correct |
51 |
Correct |
18 ms |
9820 KB |
Output is correct |
52 |
Correct |
25 ms |
9820 KB |
Output is correct |
53 |
Correct |
78 ms |
9820 KB |
Output is correct |
54 |
Correct |
23 ms |
10328 KB |
Output is correct |
55 |
Correct |
30 ms |
10332 KB |
Output is correct |
56 |
Correct |
122 ms |
10400 KB |
Output is correct |
57 |
Correct |
21 ms |
10840 KB |
Output is correct |
58 |
Correct |
29 ms |
10588 KB |
Output is correct |
59 |
Correct |
103 ms |
10588 KB |
Output is correct |
60 |
Correct |
24 ms |
11100 KB |
Output is correct |
61 |
Correct |
35 ms |
11100 KB |
Output is correct |
62 |
Correct |
153 ms |
11188 KB |
Output is correct |
63 |
Correct |
112 ms |
11180 KB |
Output is correct |
64 |
Correct |
177 ms |
11100 KB |
Output is correct |
65 |
Correct |
162 ms |
11176 KB |
Output is correct |
66 |
Correct |
112 ms |
11100 KB |
Output is correct |
67 |
Correct |
104 ms |
11184 KB |
Output is correct |
68 |
Correct |
59 ms |
11096 KB |
Output is correct |
69 |
Correct |
58 ms |
11236 KB |
Output is correct |
70 |
Correct |
46 ms |
11424 KB |
Output is correct |
71 |
Correct |
50 ms |
11232 KB |
Output is correct |
72 |
Incorrect |
54 ms |
11356 KB |
Output isn't correct |
73 |
Incorrect |
46 ms |
11608 KB |
Output isn't correct |
74 |
Correct |
76 ms |
11684 KB |
Output is correct |
75 |
Correct |
74 ms |
11744 KB |
Output is correct |
76 |
Correct |
65 ms |
11680 KB |
Output is correct |
77 |
Correct |
76 ms |
11736 KB |
Output is correct |
78 |
Correct |
69 ms |
11608 KB |
Output is correct |
79 |
Correct |
62 ms |
11628 KB |
Output is correct |
80 |
Correct |
73 ms |
11672 KB |
Output is correct |
81 |
Correct |
72 ms |
11612 KB |
Output is correct |
82 |
Correct |
61 ms |
11664 KB |
Output is correct |
83 |
Correct |
79 ms |
11608 KB |
Output is correct |
84 |
Correct |
75 ms |
11612 KB |
Output is correct |
85 |
Correct |
72 ms |
11612 KB |
Output is correct |
86 |
Correct |
82 ms |
11612 KB |
Output is correct |
87 |
Correct |
83 ms |
11596 KB |
Output is correct |
88 |
Correct |
79 ms |
11352 KB |
Output is correct |
89 |
Correct |
79 ms |
11508 KB |
Output is correct |
90 |
Correct |
101 ms |
11356 KB |
Output is correct |
91 |
Correct |
86 ms |
11496 KB |
Output is correct |
92 |
Correct |
82 ms |
11504 KB |
Output is correct |