#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 || (dis[r.f][r.s]+1)/m <= g[x][y]-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') {
g[x][y] = g[r.f][r.s]+1;
q.push({x,y});
}
}
}
int l = 0, 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:117: 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 || (dis[r.f][r.s]+1)/m <= g[x][y]-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 |
5316 KB |
Output is correct |
4 |
Correct |
3 ms |
5720 KB |
Output is correct |
5 |
Correct |
2 ms |
5468 KB |
Output is correct |
6 |
Correct |
3 ms |
5468 KB |
Output is correct |
7 |
Correct |
96 ms |
12124 KB |
Output is correct |
8 |
Incorrect |
3 ms |
5556 KB |
Output isn't correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
5508 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Incorrect |
3 ms |
5468 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
5468 KB |
Output isn't correct |
14 |
Incorrect |
3 ms |
5468 KB |
Output isn't correct |
15 |
Correct |
3 ms |
5468 KB |
Output is correct |
16 |
Correct |
3 ms |
5468 KB |
Output is correct |
17 |
Correct |
2 ms |
5468 KB |
Output is correct |
18 |
Correct |
4 ms |
5720 KB |
Output is correct |
19 |
Correct |
4 ms |
5468 KB |
Output is correct |
20 |
Correct |
3 ms |
5468 KB |
Output is correct |
21 |
Correct |
2 ms |
5468 KB |
Output is correct |
22 |
Correct |
3 ms |
5468 KB |
Output is correct |
23 |
Correct |
3 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5468 KB |
Output is correct |
25 |
Correct |
3 ms |
5468 KB |
Output is correct |
26 |
Correct |
2 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 |
5564 KB |
Output is correct |
30 |
Correct |
3 ms |
5568 KB |
Output is correct |
31 |
Correct |
3 ms |
5572 KB |
Output is correct |
32 |
Correct |
3 ms |
5468 KB |
Output is correct |
33 |
Correct |
11 ms |
6816 KB |
Output is correct |
34 |
Correct |
11 ms |
6860 KB |
Output is correct |
35 |
Correct |
26 ms |
6896 KB |
Output is correct |
36 |
Correct |
14 ms |
7168 KB |
Output is correct |
37 |
Correct |
15 ms |
7216 KB |
Output is correct |
38 |
Correct |
32 ms |
7212 KB |
Output is correct |
39 |
Correct |
16 ms |
7572 KB |
Output is correct |
40 |
Correct |
17 ms |
7576 KB |
Output is correct |
41 |
Correct |
36 ms |
7632 KB |
Output is correct |
42 |
Correct |
19 ms |
8312 KB |
Output is correct |
43 |
Correct |
25 ms |
8312 KB |
Output is correct |
44 |
Correct |
51 ms |
8332 KB |
Output is correct |
45 |
Correct |
22 ms |
8816 KB |
Output is correct |
46 |
Correct |
26 ms |
8812 KB |
Output is correct |
47 |
Correct |
58 ms |
8828 KB |
Output is correct |
48 |
Correct |
24 ms |
9388 KB |
Output is correct |
49 |
Correct |
23 ms |
9388 KB |
Output is correct |
50 |
Correct |
69 ms |
9364 KB |
Output is correct |
51 |
Correct |
28 ms |
10008 KB |
Output is correct |
52 |
Correct |
27 ms |
10012 KB |
Output is correct |
53 |
Correct |
73 ms |
9940 KB |
Output is correct |
54 |
Correct |
41 ms |
10592 KB |
Output is correct |
55 |
Correct |
41 ms |
10660 KB |
Output is correct |
56 |
Correct |
110 ms |
10636 KB |
Output is correct |
57 |
Correct |
41 ms |
11356 KB |
Output is correct |
58 |
Correct |
60 ms |
11304 KB |
Output is correct |
59 |
Correct |
119 ms |
11340 KB |
Output is correct |
60 |
Correct |
49 ms |
12112 KB |
Output is correct |
61 |
Correct |
48 ms |
12196 KB |
Output is correct |
62 |
Correct |
141 ms |
12056 KB |
Output is correct |
63 |
Correct |
105 ms |
12096 KB |
Output is correct |
64 |
Correct |
176 ms |
11988 KB |
Output is correct |
65 |
Correct |
151 ms |
12160 KB |
Output is correct |
66 |
Correct |
144 ms |
12112 KB |
Output is correct |
67 |
Incorrect |
114 ms |
12020 KB |
Output isn't correct |
68 |
Correct |
65 ms |
12172 KB |
Output is correct |
69 |
Correct |
68 ms |
12108 KB |
Output is correct |
70 |
Correct |
66 ms |
12164 KB |
Output is correct |
71 |
Correct |
59 ms |
12140 KB |
Output is correct |
72 |
Incorrect |
61 ms |
12096 KB |
Output isn't correct |
73 |
Incorrect |
81 ms |
12112 KB |
Output isn't correct |
74 |
Correct |
76 ms |
12152 KB |
Output is correct |
75 |
Correct |
83 ms |
12104 KB |
Output is correct |
76 |
Correct |
98 ms |
12124 KB |
Output is correct |
77 |
Correct |
82 ms |
12036 KB |
Output is correct |
78 |
Incorrect |
84 ms |
12176 KB |
Output isn't correct |
79 |
Correct |
74 ms |
12108 KB |
Output is correct |
80 |
Correct |
85 ms |
12096 KB |
Output is correct |
81 |
Correct |
84 ms |
12092 KB |
Output is correct |
82 |
Correct |
80 ms |
12092 KB |
Output is correct |
83 |
Correct |
85 ms |
12080 KB |
Output is correct |
84 |
Correct |
104 ms |
12080 KB |
Output is correct |
85 |
Correct |
82 ms |
12088 KB |
Output is correct |
86 |
Correct |
87 ms |
12108 KB |
Output is correct |
87 |
Correct |
94 ms |
12068 KB |
Output is correct |
88 |
Correct |
103 ms |
12076 KB |
Output is correct |
89 |
Correct |
100 ms |
12128 KB |
Output is correct |
90 |
Correct |
98 ms |
12056 KB |
Output is correct |
91 |
Correct |
90 ms |
12088 KB |
Output is correct |
92 |
Correct |
96 ms |
12056 KB |
Output is correct |