#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]&&!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:31:1: warning: control reaches end of non-void function [-Wreturn-type]
31 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7000 KB |
Output is correct |
2 |
Correct |
3 ms |
6744 KB |
Output is correct |
3 |
Correct |
4 ms |
6748 KB |
Output is correct |
4 |
Correct |
3 ms |
6944 KB |
Output is correct |
5 |
Correct |
4 ms |
6744 KB |
Output is correct |
6 |
Correct |
3 ms |
6744 KB |
Output is correct |
7 |
Correct |
77 ms |
11356 KB |
Output is correct |
8 |
Correct |
4 ms |
6744 KB |
Output is correct |
9 |
Correct |
4 ms |
6744 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
3 ms |
6744 KB |
Output is correct |
12 |
Incorrect |
4 ms |
6744 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
6748 KB |
Output isn't correct |
14 |
Correct |
3 ms |
6744 KB |
Output is correct |
15 |
Correct |
4 ms |
6744 KB |
Output is correct |
16 |
Correct |
3 ms |
7000 KB |
Output is correct |
17 |
Correct |
3 ms |
7000 KB |
Output is correct |
18 |
Correct |
4 ms |
6744 KB |
Output is correct |
19 |
Correct |
3 ms |
6744 KB |
Output is correct |
20 |
Correct |
3 ms |
6744 KB |
Output is correct |
21 |
Correct |
4 ms |
6744 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 |
6744 KB |
Output is correct |
25 |
Correct |
3 ms |
6744 KB |
Output is correct |
26 |
Correct |
4 ms |
6748 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 |
7000 KB |
Output is correct |
31 |
Correct |
4 ms |
6744 KB |
Output is correct |
32 |
Correct |
4 ms |
6744 KB |
Output is correct |
33 |
Correct |
9 ms |
8792 KB |
Output is correct |
34 |
Correct |
9 ms |
8024 KB |
Output is correct |
35 |
Correct |
21 ms |
8792 KB |
Output is correct |
36 |
Correct |
9 ms |
8792 KB |
Output is correct |
37 |
Correct |
10 ms |
8024 KB |
Output is correct |
38 |
Correct |
26 ms |
8792 KB |
Output is correct |
39 |
Correct |
10 ms |
9052 KB |
Output is correct |
40 |
Correct |
12 ms |
8792 KB |
Output is correct |
41 |
Correct |
34 ms |
9048 KB |
Output is correct |
42 |
Correct |
11 ms |
9048 KB |
Output is correct |
43 |
Correct |
16 ms |
8792 KB |
Output is correct |
44 |
Correct |
40 ms |
8960 KB |
Output is correct |
45 |
Correct |
13 ms |
9048 KB |
Output is correct |
46 |
Correct |
15 ms |
9048 KB |
Output is correct |
47 |
Correct |
49 ms |
9048 KB |
Output is correct |
48 |
Correct |
16 ms |
9560 KB |
Output is correct |
49 |
Correct |
20 ms |
9816 KB |
Output is correct |
50 |
Correct |
72 ms |
9652 KB |
Output is correct |
51 |
Correct |
18 ms |
9816 KB |
Output is correct |
52 |
Correct |
22 ms |
9820 KB |
Output is correct |
53 |
Correct |
79 ms |
9816 KB |
Output is correct |
54 |
Correct |
20 ms |
10328 KB |
Output is correct |
55 |
Correct |
26 ms |
10328 KB |
Output is correct |
56 |
Correct |
81 ms |
10328 KB |
Output is correct |
57 |
Correct |
24 ms |
10800 KB |
Output is correct |
58 |
Correct |
25 ms |
10584 KB |
Output is correct |
59 |
Correct |
119 ms |
10804 KB |
Output is correct |
60 |
Correct |
24 ms |
11096 KB |
Output is correct |
61 |
Correct |
29 ms |
11100 KB |
Output is correct |
62 |
Correct |
114 ms |
11184 KB |
Output is correct |
63 |
Correct |
97 ms |
11096 KB |
Output is correct |
64 |
Correct |
151 ms |
11096 KB |
Output is correct |
65 |
Correct |
143 ms |
11096 KB |
Output is correct |
66 |
Correct |
104 ms |
11096 KB |
Output is correct |
67 |
Correct |
97 ms |
11176 KB |
Output is correct |
68 |
Correct |
53 ms |
11096 KB |
Output is correct |
69 |
Correct |
52 ms |
11096 KB |
Output is correct |
70 |
Correct |
41 ms |
11084 KB |
Output is correct |
71 |
Correct |
45 ms |
11096 KB |
Output is correct |
72 |
Incorrect |
41 ms |
11084 KB |
Output isn't correct |
73 |
Incorrect |
45 ms |
11600 KB |
Output isn't correct |
74 |
Correct |
60 ms |
11576 KB |
Output is correct |
75 |
Correct |
63 ms |
11608 KB |
Output is correct |
76 |
Correct |
70 ms |
11608 KB |
Output is correct |
77 |
Correct |
69 ms |
11608 KB |
Output is correct |
78 |
Correct |
74 ms |
11608 KB |
Output is correct |
79 |
Correct |
57 ms |
11608 KB |
Output is correct |
80 |
Correct |
60 ms |
11784 KB |
Output is correct |
81 |
Correct |
79 ms |
11684 KB |
Output is correct |
82 |
Correct |
58 ms |
11672 KB |
Output is correct |
83 |
Correct |
77 ms |
11608 KB |
Output is correct |
84 |
Correct |
70 ms |
11612 KB |
Output is correct |
85 |
Correct |
66 ms |
11608 KB |
Output is correct |
86 |
Correct |
76 ms |
11608 KB |
Output is correct |
87 |
Correct |
67 ms |
11608 KB |
Output is correct |
88 |
Correct |
81 ms |
11504 KB |
Output is correct |
89 |
Correct |
71 ms |
11508 KB |
Output is correct |
90 |
Correct |
78 ms |
11496 KB |
Output is correct |
91 |
Correct |
95 ms |
11504 KB |
Output is correct |
92 |
Correct |
81 ms |
11504 KB |
Output is correct |