#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'){
if (d[i][j]<=x*s)
return false;
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]&&!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'&&m[u][v]!='D'&&!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]?(d[i][j]-1)*s:1e18);
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:33:1: warning: control reaches end of non-void function [-Wreturn-type]
33 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
6748 KB |
Output is correct |
3 |
Correct |
4 ms |
6748 KB |
Output is correct |
4 |
Correct |
4 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6748 KB |
Output is correct |
6 |
Correct |
3 ms |
6744 KB |
Output is correct |
7 |
Correct |
76 ms |
11396 KB |
Output is correct |
8 |
Correct |
3 ms |
6636 KB |
Output is correct |
9 |
Correct |
4 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
4 ms |
6788 KB |
Output is correct |
12 |
Correct |
4 ms |
6748 KB |
Output is correct |
13 |
Correct |
4 ms |
6748 KB |
Output is correct |
14 |
Correct |
4 ms |
7000 KB |
Output is correct |
15 |
Correct |
4 ms |
6748 KB |
Output is correct |
16 |
Correct |
4 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 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 |
3 ms |
6748 KB |
Output is correct |
23 |
Correct |
4 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 |
4 ms |
6944 KB |
Output is correct |
27 |
Correct |
4 ms |
6748 KB |
Output is correct |
28 |
Correct |
4 ms |
6748 KB |
Output is correct |
29 |
Correct |
4 ms |
6744 KB |
Output is correct |
30 |
Correct |
4 ms |
6748 KB |
Output is correct |
31 |
Correct |
4 ms |
6748 KB |
Output is correct |
32 |
Correct |
4 ms |
6744 KB |
Output is correct |
33 |
Correct |
7 ms |
8976 KB |
Output is correct |
34 |
Correct |
8 ms |
7772 KB |
Output is correct |
35 |
Correct |
20 ms |
8984 KB |
Output is correct |
36 |
Correct |
7 ms |
8796 KB |
Output is correct |
37 |
Correct |
9 ms |
8028 KB |
Output is correct |
38 |
Correct |
23 ms |
8796 KB |
Output is correct |
39 |
Correct |
9 ms |
9052 KB |
Output is correct |
40 |
Correct |
11 ms |
8540 KB |
Output is correct |
41 |
Correct |
33 ms |
8916 KB |
Output is correct |
42 |
Correct |
9 ms |
9052 KB |
Output is correct |
43 |
Correct |
12 ms |
8908 KB |
Output is correct |
44 |
Correct |
40 ms |
9048 KB |
Output is correct |
45 |
Correct |
11 ms |
9052 KB |
Output is correct |
46 |
Correct |
13 ms |
9276 KB |
Output is correct |
47 |
Correct |
50 ms |
9052 KB |
Output is correct |
48 |
Correct |
13 ms |
9564 KB |
Output is correct |
49 |
Correct |
20 ms |
9660 KB |
Output is correct |
50 |
Correct |
63 ms |
9812 KB |
Output is correct |
51 |
Correct |
15 ms |
9820 KB |
Output is correct |
52 |
Correct |
19 ms |
9816 KB |
Output is correct |
53 |
Correct |
96 ms |
10020 KB |
Output is correct |
54 |
Correct |
18 ms |
10332 KB |
Output is correct |
55 |
Correct |
23 ms |
10332 KB |
Output is correct |
56 |
Correct |
77 ms |
10332 KB |
Output is correct |
57 |
Correct |
18 ms |
10840 KB |
Output is correct |
58 |
Correct |
21 ms |
10588 KB |
Output is correct |
59 |
Correct |
112 ms |
10800 KB |
Output is correct |
60 |
Correct |
18 ms |
11096 KB |
Output is correct |
61 |
Correct |
26 ms |
11100 KB |
Output is correct |
62 |
Correct |
140 ms |
11188 KB |
Output is correct |
63 |
Correct |
97 ms |
11180 KB |
Output is correct |
64 |
Correct |
154 ms |
11188 KB |
Output is correct |
65 |
Correct |
145 ms |
11180 KB |
Output is correct |
66 |
Correct |
111 ms |
11100 KB |
Output is correct |
67 |
Correct |
98 ms |
11096 KB |
Output is correct |
68 |
Correct |
46 ms |
11100 KB |
Output is correct |
69 |
Correct |
50 ms |
11240 KB |
Output is correct |
70 |
Correct |
37 ms |
11096 KB |
Output is correct |
71 |
Correct |
38 ms |
11252 KB |
Output is correct |
72 |
Correct |
35 ms |
11104 KB |
Output is correct |
73 |
Correct |
33 ms |
11612 KB |
Output is correct |
74 |
Correct |
58 ms |
11704 KB |
Output is correct |
75 |
Correct |
60 ms |
11612 KB |
Output is correct |
76 |
Correct |
49 ms |
11680 KB |
Output is correct |
77 |
Correct |
54 ms |
11608 KB |
Output is correct |
78 |
Correct |
64 ms |
11664 KB |
Output is correct |
79 |
Correct |
54 ms |
11612 KB |
Output is correct |
80 |
Correct |
59 ms |
11600 KB |
Output is correct |
81 |
Correct |
66 ms |
11672 KB |
Output is correct |
82 |
Correct |
52 ms |
11664 KB |
Output is correct |
83 |
Correct |
74 ms |
11600 KB |
Output is correct |
84 |
Correct |
67 ms |
11856 KB |
Output is correct |
85 |
Correct |
54 ms |
11612 KB |
Output is correct |
86 |
Correct |
60 ms |
11608 KB |
Output is correct |
87 |
Correct |
62 ms |
11612 KB |
Output is correct |
88 |
Correct |
66 ms |
11356 KB |
Output is correct |
89 |
Correct |
83 ms |
11608 KB |
Output is correct |
90 |
Correct |
70 ms |
11496 KB |
Output is correct |
91 |
Correct |
78 ms |
11500 KB |
Output is correct |
92 |
Correct |
76 ms |
11608 KB |
Output is correct |