#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 = g[a.f][a.s]-1;
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 |
3 ms |
5464 KB |
Output is correct |
2 |
Correct |
3 ms |
5468 KB |
Output is correct |
3 |
Correct |
3 ms |
5560 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
3 ms |
5436 KB |
Output is correct |
7 |
Correct |
82 ms |
12036 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
4 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
2 ms |
5464 KB |
Output is correct |
13 |
Correct |
3 ms |
5468 KB |
Output is correct |
14 |
Correct |
3 ms |
5464 KB |
Output is correct |
15 |
Correct |
3 ms |
5468 KB |
Output is correct |
16 |
Correct |
3 ms |
5468 KB |
Output is correct |
17 |
Correct |
3 ms |
5464 KB |
Output is correct |
18 |
Correct |
3 ms |
5464 KB |
Output is correct |
19 |
Correct |
3 ms |
5464 KB |
Output is correct |
20 |
Correct |
3 ms |
5464 KB |
Output is correct |
21 |
Correct |
3 ms |
5468 KB |
Output is correct |
22 |
Correct |
3 ms |
5468 KB |
Output is correct |
23 |
Correct |
3 ms |
5464 KB |
Output is correct |
24 |
Correct |
3 ms |
5464 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 |
5468 KB |
Output is correct |
28 |
Correct |
3 ms |
5468 KB |
Output is correct |
29 |
Correct |
3 ms |
5468 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 |
11 ms |
6804 KB |
Output is correct |
34 |
Correct |
10 ms |
6820 KB |
Output is correct |
35 |
Correct |
22 ms |
6788 KB |
Output is correct |
36 |
Correct |
14 ms |
7172 KB |
Output is correct |
37 |
Correct |
17 ms |
7172 KB |
Output is correct |
38 |
Correct |
40 ms |
7192 KB |
Output is correct |
39 |
Correct |
16 ms |
7576 KB |
Output is correct |
40 |
Correct |
16 ms |
7560 KB |
Output is correct |
41 |
Correct |
36 ms |
7580 KB |
Output is correct |
42 |
Correct |
19 ms |
8236 KB |
Output is correct |
43 |
Correct |
19 ms |
8312 KB |
Output is correct |
44 |
Correct |
43 ms |
8264 KB |
Output is correct |
45 |
Correct |
21 ms |
8808 KB |
Output is correct |
46 |
Correct |
23 ms |
8796 KB |
Output is correct |
47 |
Correct |
52 ms |
8932 KB |
Output is correct |
48 |
Correct |
28 ms |
9380 KB |
Output is correct |
49 |
Correct |
28 ms |
9328 KB |
Output is correct |
50 |
Correct |
61 ms |
9308 KB |
Output is correct |
51 |
Correct |
31 ms |
9920 KB |
Output is correct |
52 |
Correct |
32 ms |
10004 KB |
Output is correct |
53 |
Correct |
92 ms |
9932 KB |
Output is correct |
54 |
Correct |
37 ms |
10672 KB |
Output is correct |
55 |
Correct |
45 ms |
10636 KB |
Output is correct |
56 |
Correct |
83 ms |
10588 KB |
Output is correct |
57 |
Correct |
41 ms |
11348 KB |
Output is correct |
58 |
Correct |
41 ms |
11312 KB |
Output is correct |
59 |
Correct |
98 ms |
11244 KB |
Output is correct |
60 |
Correct |
47 ms |
12056 KB |
Output is correct |
61 |
Correct |
65 ms |
12088 KB |
Output is correct |
62 |
Correct |
120 ms |
11976 KB |
Output is correct |
63 |
Correct |
104 ms |
11980 KB |
Output is correct |
64 |
Correct |
167 ms |
12048 KB |
Output is correct |
65 |
Correct |
155 ms |
11980 KB |
Output is correct |
66 |
Correct |
121 ms |
12004 KB |
Output is correct |
67 |
Correct |
113 ms |
11980 KB |
Output is correct |
68 |
Correct |
54 ms |
12004 KB |
Output is correct |
69 |
Correct |
50 ms |
12016 KB |
Output is correct |
70 |
Correct |
57 ms |
12048 KB |
Output is correct |
71 |
Correct |
67 ms |
12016 KB |
Output is correct |
72 |
Correct |
45 ms |
12012 KB |
Output is correct |
73 |
Correct |
51 ms |
12048 KB |
Output is correct |
74 |
Correct |
81 ms |
12196 KB |
Output is correct |
75 |
Correct |
65 ms |
12052 KB |
Output is correct |
76 |
Correct |
67 ms |
12052 KB |
Output is correct |
77 |
Correct |
71 ms |
12016 KB |
Output is correct |
78 |
Correct |
88 ms |
12032 KB |
Output is correct |
79 |
Correct |
74 ms |
12036 KB |
Output is correct |
80 |
Correct |
81 ms |
12236 KB |
Output is correct |
81 |
Correct |
71 ms |
12040 KB |
Output is correct |
82 |
Correct |
66 ms |
12036 KB |
Output is correct |
83 |
Correct |
127 ms |
11996 KB |
Output is correct |
84 |
Correct |
71 ms |
12028 KB |
Output is correct |
85 |
Correct |
94 ms |
12016 KB |
Output is correct |
86 |
Correct |
77 ms |
12016 KB |
Output is correct |
87 |
Correct |
77 ms |
12024 KB |
Output is correct |
88 |
Correct |
96 ms |
11996 KB |
Output is correct |
89 |
Correct |
86 ms |
12012 KB |
Output is correct |
90 |
Correct |
123 ms |
12024 KB |
Output is correct |
91 |
Correct |
98 ms |
12024 KB |
Output is correct |
92 |
Correct |
87 ms |
11984 KB |
Output is correct |