#include <bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
int n, m;
int C[]{0,1,0,-1}, R[]{1,0,-1,0};
pair<int,int> a, b; queue<pair<int,int>> q;
vector<vector<int>> g(805, vector<int>(805, 1e9));
vector<string> v(805);
bool bfs(int k) {
vector<vector<int>> dis(n, vector<int>(n, 1e9));
q.push(a); dis[a.f][a.s] = 0;
while(!q.empty()) {
pair<int,int> r = q.front(); q.pop();
for(int i=0;i<4;i++) {
int x = r.f+R[i], y = r.s+C[i];
if(0 <= x && x < n && 0 <= y && y < n && ((dis[r.f][r.s]+1)/m < g[x][y]-k && v[x][y] != 'D' || (dis[r.f][r.s]+1)/m <= g[r.f][r.s]-k && v[x][y] == 'D'))
if(dis[r.f][r.s]+1 < dis[x][y] && (v[x][y] == 'G' || v[x][y] == 'D')) {
dis[x][y] = dis[r.f][r.s]+1;
q.push({x,y});
}
}
}
if(dis[b.f][b.s] == 1e9) return false;
return true;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m; for(int i=0;i<n;i++) cin >> v[i];
for(int i=0;i<n;i++) for(int j=0;j<n;j++) {
if(v[i][j] == 'M') a.f = i, a.s = j;
if(v[i][j] == 'D') b.f = i, b.s = j;
if(v[i][j] == 'H') {q.push({i,j}); g[i][j] = 0;}
}
while(!q.empty()) {
pair<int,int> r = q.front(); q.pop();
for(int i=0;i<4;i++) {
int x = r.f+R[i], y = r.s+C[i];
if(0 <= x && x < n && 0 <= y && y < n) if(g[r.f][r.s]+1 < g[x][y] && (v[x][y] == 'G' || v[x][y] == 'M')) {
g[x][y] = g[r.f][r.s]+1;
q.push({x,y});
}
}
}
int l = -1, r = 1e6;
while(l<r) {
int d = (l+r+1)/2;
if(bfs(d)) l = d;
else r = d-1;
}
cout << l;
}
Compilation message
mecho.cpp: In function 'bool bfs(long long int)':
mecho.cpp:18:81: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
18 | if(0 <= x && x < n && 0 <= y && y < n && ((dis[r.f][r.s]+1)/m < g[x][y]-k && v[x][y] != 'D' || (dis[r.f][r.s]+1)/m <= g[r.f][r.s]-k && v[x][y] == 'D'))
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
3 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Correct |
2 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
88 ms |
11484 KB |
Output is correct |
8 |
Correct |
3 ms |
5464 KB |
Output is correct |
9 |
Correct |
2 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5468 KB |
Output is correct |
13 |
Incorrect |
3 ms |
5468 KB |
Output isn't correct |
14 |
Correct |
3 ms |
5468 KB |
Output is correct |
15 |
Correct |
3 ms |
5372 KB |
Output is correct |
16 |
Correct |
2 ms |
5468 KB |
Output is correct |
17 |
Correct |
2 ms |
5468 KB |
Output is correct |
18 |
Correct |
2 ms |
5468 KB |
Output is correct |
19 |
Correct |
2 ms |
5468 KB |
Output is correct |
20 |
Correct |
3 ms |
5468 KB |
Output is correct |
21 |
Correct |
3 ms |
5572 KB |
Output is correct |
22 |
Correct |
3 ms |
5468 KB |
Output is correct |
23 |
Correct |
3 ms |
5468 KB |
Output is correct |
24 |
Correct |
3 ms |
5468 KB |
Output is correct |
25 |
Correct |
3 ms |
5468 KB |
Output is correct |
26 |
Correct |
3 ms |
5468 KB |
Output is correct |
27 |
Correct |
3 ms |
5588 KB |
Output is correct |
28 |
Correct |
3 ms |
5468 KB |
Output is correct |
29 |
Correct |
3 ms |
5428 KB |
Output is correct |
30 |
Correct |
3 ms |
5468 KB |
Output is correct |
31 |
Correct |
3 ms |
5468 KB |
Output is correct |
32 |
Correct |
3 ms |
5468 KB |
Output is correct |
33 |
Correct |
10 ms |
6676 KB |
Output is correct |
34 |
Correct |
17 ms |
6844 KB |
Output is correct |
35 |
Correct |
23 ms |
6688 KB |
Output is correct |
36 |
Correct |
11 ms |
7080 KB |
Output is correct |
37 |
Correct |
11 ms |
7064 KB |
Output is correct |
38 |
Correct |
29 ms |
7004 KB |
Output is correct |
39 |
Correct |
15 ms |
7388 KB |
Output is correct |
40 |
Correct |
13 ms |
7436 KB |
Output is correct |
41 |
Correct |
36 ms |
7428 KB |
Output is correct |
42 |
Correct |
17 ms |
7844 KB |
Output is correct |
43 |
Correct |
17 ms |
7836 KB |
Output is correct |
44 |
Correct |
46 ms |
7864 KB |
Output is correct |
45 |
Correct |
25 ms |
8300 KB |
Output is correct |
46 |
Correct |
19 ms |
8300 KB |
Output is correct |
47 |
Correct |
58 ms |
8300 KB |
Output is correct |
48 |
Correct |
23 ms |
8872 KB |
Output is correct |
49 |
Correct |
23 ms |
8872 KB |
Output is correct |
50 |
Correct |
61 ms |
8840 KB |
Output is correct |
51 |
Correct |
32 ms |
9392 KB |
Output is correct |
52 |
Correct |
24 ms |
9480 KB |
Output is correct |
53 |
Correct |
71 ms |
9432 KB |
Output is correct |
54 |
Correct |
28 ms |
10068 KB |
Output is correct |
55 |
Correct |
32 ms |
10072 KB |
Output is correct |
56 |
Correct |
118 ms |
9968 KB |
Output is correct |
57 |
Correct |
41 ms |
10688 KB |
Output is correct |
58 |
Correct |
30 ms |
10700 KB |
Output is correct |
59 |
Correct |
122 ms |
10688 KB |
Output is correct |
60 |
Correct |
35 ms |
11452 KB |
Output is correct |
61 |
Correct |
34 ms |
11368 KB |
Output is correct |
62 |
Correct |
140 ms |
11388 KB |
Output is correct |
63 |
Correct |
106 ms |
11420 KB |
Output is correct |
64 |
Correct |
152 ms |
11380 KB |
Output is correct |
65 |
Correct |
135 ms |
11624 KB |
Output is correct |
66 |
Correct |
157 ms |
11312 KB |
Output is correct |
67 |
Correct |
107 ms |
11396 KB |
Output is correct |
68 |
Correct |
63 ms |
11464 KB |
Output is correct |
69 |
Correct |
65 ms |
11408 KB |
Output is correct |
70 |
Correct |
58 ms |
11436 KB |
Output is correct |
71 |
Correct |
53 ms |
11396 KB |
Output is correct |
72 |
Incorrect |
51 ms |
11424 KB |
Output isn't correct |
73 |
Incorrect |
49 ms |
11468 KB |
Output isn't correct |
74 |
Correct |
92 ms |
11400 KB |
Output is correct |
75 |
Correct |
76 ms |
11408 KB |
Output is correct |
76 |
Correct |
71 ms |
11392 KB |
Output is correct |
77 |
Correct |
91 ms |
11392 KB |
Output is correct |
78 |
Correct |
78 ms |
11384 KB |
Output is correct |
79 |
Correct |
67 ms |
11304 KB |
Output is correct |
80 |
Correct |
73 ms |
11412 KB |
Output is correct |
81 |
Correct |
98 ms |
11420 KB |
Output is correct |
82 |
Correct |
64 ms |
11364 KB |
Output is correct |
83 |
Correct |
85 ms |
11392 KB |
Output is correct |
84 |
Correct |
78 ms |
11396 KB |
Output is correct |
85 |
Correct |
74 ms |
11356 KB |
Output is correct |
86 |
Correct |
85 ms |
11344 KB |
Output is correct |
87 |
Correct |
87 ms |
11476 KB |
Output is correct |
88 |
Correct |
88 ms |
11384 KB |
Output is correct |
89 |
Correct |
87 ms |
11372 KB |
Output is correct |
90 |
Correct |
89 ms |
11428 KB |
Output is correct |
91 |
Correct |
77 ms |
11336 KB |
Output is correct |
92 |
Correct |
83 ms |
11344 KB |
Output is correct |