#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define f first
#define s second
#define int ll
using namespace std;
const int inf = 1e18,N = 805;
char a[N][N];
int dis[N][N],xm,ym,n,S;
bool fix[N][N];
vector <pair<int,int> > neighbours = {{0,1}, {0,-1},{1,0}, {-1,0}};
bool check(int t){
if (dis[xm][ym] <= t*S) return 0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
fix[i][j]=0;
queue <pair <int,pair<int,int> > > q;
while (q.size()) q.pop();
q.push({t*S,{xm,ym}});
fix[xm][ym] = 1;
while (q.size()){
int T = q.front().f,x = q.front().s.f,y = q.front().s.s;
q.pop();
if (a[x][y] == 'D') return 1;
for (auto [dx,dy]: neighbours){
int X = x+dx,Y = y+dy;
if (X < 1 || Y < 1 || X > n || Y > n || fix[X][Y]) continue;
if (a[X][Y] == 'T' || a[X][Y] == 'H') continue;
if (dis[X][Y] <= T + 1) continue;
fix[X][Y] = 1;
q.push({T+1,{X,Y}});
}
}
return 0;
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
cin>>n>>S;
queue <pair<int,pair<int,int> > > q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++){
cin >> a[i][j];
dis[i][j] = inf;
if (a[i][j] == 'H') dis[i][j] = 0,q.push({0,{i,j}}),fix[i][j] = 1;
if (a[i][j] == 'M') xm = i,ym = j;
}
while (q.size()){
int d = q.front().f,x = q.front().s.f,y = q.front().s.s;
q.pop();
for (auto [dx,dy]: neighbours){
int X = x+dx,Y = y+dy;
if (X < 1 || Y < 1 || X > n || Y > n) continue;
if (fix[X][Y] || (a[X][Y] == 'T' || a[X][Y] == 'D')) continue;
dis[X][Y] = d + S;
fix[X][Y] = 1;
q.push({d+S,{X,Y}});
}
}
int l = 0,r = 1e9,ans=-1;
while (l <= r){
int mid = (l + r)>>1;
if (check(mid)) {
ans = mid,l = mid + 1;
}else{
r = mid - 1;
}
}
cout<<ans;
}
Compilation message
mecho.cpp: In function 'bool check(long long int)':
mecho.cpp:31:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for (auto [dx,dy]: neighbours){
| ^
mecho.cpp: In function 'int main()':
mecho.cpp:58:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for (auto [dx,dy]: neighbours){
| ^
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
94 ms |
7528 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
452 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
3028 KB |
Output is correct |
34 |
Correct |
4 ms |
3156 KB |
Output is correct |
35 |
Correct |
19 ms |
3228 KB |
Output is correct |
36 |
Correct |
5 ms |
3412 KB |
Output is correct |
37 |
Correct |
6 ms |
3540 KB |
Output is correct |
38 |
Correct |
40 ms |
3640 KB |
Output is correct |
39 |
Correct |
9 ms |
3856 KB |
Output is correct |
40 |
Correct |
7 ms |
4052 KB |
Output is correct |
41 |
Correct |
34 ms |
4076 KB |
Output is correct |
42 |
Correct |
8 ms |
4180 KB |
Output is correct |
43 |
Correct |
8 ms |
4436 KB |
Output is correct |
44 |
Correct |
47 ms |
4432 KB |
Output is correct |
45 |
Correct |
9 ms |
4564 KB |
Output is correct |
46 |
Correct |
10 ms |
4880 KB |
Output is correct |
47 |
Correct |
55 ms |
4948 KB |
Output is correct |
48 |
Correct |
10 ms |
4948 KB |
Output is correct |
49 |
Correct |
11 ms |
5408 KB |
Output is correct |
50 |
Correct |
66 ms |
5332 KB |
Output is correct |
51 |
Correct |
13 ms |
5432 KB |
Output is correct |
52 |
Correct |
13 ms |
5844 KB |
Output is correct |
53 |
Correct |
83 ms |
5844 KB |
Output is correct |
54 |
Correct |
19 ms |
5844 KB |
Output is correct |
55 |
Correct |
15 ms |
6296 KB |
Output is correct |
56 |
Correct |
87 ms |
6228 KB |
Output is correct |
57 |
Correct |
15 ms |
6180 KB |
Output is correct |
58 |
Correct |
16 ms |
6768 KB |
Output is correct |
59 |
Correct |
99 ms |
6800 KB |
Output is correct |
60 |
Correct |
19 ms |
6612 KB |
Output is correct |
61 |
Correct |
20 ms |
7256 KB |
Output is correct |
62 |
Correct |
124 ms |
7268 KB |
Output is correct |
63 |
Correct |
97 ms |
7252 KB |
Output is correct |
64 |
Correct |
175 ms |
7256 KB |
Output is correct |
65 |
Correct |
171 ms |
7260 KB |
Output is correct |
66 |
Correct |
127 ms |
7372 KB |
Output is correct |
67 |
Correct |
107 ms |
7252 KB |
Output is correct |
68 |
Correct |
51 ms |
7336 KB |
Output is correct |
69 |
Correct |
49 ms |
7336 KB |
Output is correct |
70 |
Correct |
42 ms |
7252 KB |
Output is correct |
71 |
Correct |
38 ms |
7328 KB |
Output is correct |
72 |
Correct |
34 ms |
7340 KB |
Output is correct |
73 |
Correct |
46 ms |
8080 KB |
Output is correct |
74 |
Correct |
73 ms |
8072 KB |
Output is correct |
75 |
Correct |
78 ms |
8076 KB |
Output is correct |
76 |
Correct |
60 ms |
8020 KB |
Output is correct |
77 |
Correct |
66 ms |
8080 KB |
Output is correct |
78 |
Correct |
70 ms |
7992 KB |
Output is correct |
79 |
Correct |
61 ms |
7964 KB |
Output is correct |
80 |
Correct |
61 ms |
8000 KB |
Output is correct |
81 |
Correct |
80 ms |
7992 KB |
Output is correct |
82 |
Correct |
54 ms |
7996 KB |
Output is correct |
83 |
Correct |
80 ms |
7756 KB |
Output is correct |
84 |
Correct |
94 ms |
7760 KB |
Output is correct |
85 |
Correct |
69 ms |
7772 KB |
Output is correct |
86 |
Correct |
76 ms |
7864 KB |
Output is correct |
87 |
Correct |
75 ms |
7888 KB |
Output is correct |
88 |
Correct |
82 ms |
7728 KB |
Output is correct |
89 |
Correct |
82 ms |
7856 KB |
Output is correct |
90 |
Correct |
85 ms |
7732 KB |
Output is correct |
91 |
Correct |
78 ms |
7728 KB |
Output is correct |
92 |
Correct |
87 ms |
7748 KB |
Output is correct |