#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 = 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: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 |
5468 KB |
Output is correct |
2 |
Correct |
3 ms |
5468 KB |
Output is correct |
3 |
Correct |
3 ms |
5464 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5432 KB |
Output is correct |
6 |
Correct |
3 ms |
5516 KB |
Output is correct |
7 |
Correct |
94 ms |
12072 KB |
Output is correct |
8 |
Incorrect |
3 ms |
5468 KB |
Output isn't correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 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 |
Incorrect |
3 ms |
5468 KB |
Output isn't correct |
15 |
Correct |
4 ms |
5724 KB |
Output is correct |
16 |
Correct |
3 ms |
5468 KB |
Output is correct |
17 |
Correct |
3 ms |
5468 KB |
Output is correct |
18 |
Correct |
2 ms |
5468 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 |
5580 KB |
Output is correct |
23 |
Correct |
3 ms |
5464 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 |
5720 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 |
4 ms |
5468 KB |
Output is correct |
32 |
Correct |
3 ms |
5464 KB |
Output is correct |
33 |
Correct |
10 ms |
6792 KB |
Output is correct |
34 |
Correct |
12 ms |
6780 KB |
Output is correct |
35 |
Correct |
24 ms |
6856 KB |
Output is correct |
36 |
Correct |
11 ms |
7164 KB |
Output is correct |
37 |
Correct |
11 ms |
7172 KB |
Output is correct |
38 |
Correct |
29 ms |
7180 KB |
Output is correct |
39 |
Correct |
14 ms |
7644 KB |
Output is correct |
40 |
Correct |
14 ms |
7908 KB |
Output is correct |
41 |
Correct |
42 ms |
7648 KB |
Output is correct |
42 |
Correct |
18 ms |
8100 KB |
Output is correct |
43 |
Correct |
18 ms |
8100 KB |
Output is correct |
44 |
Correct |
45 ms |
8092 KB |
Output is correct |
45 |
Correct |
18 ms |
8596 KB |
Output is correct |
46 |
Correct |
19 ms |
8604 KB |
Output is correct |
47 |
Correct |
75 ms |
8588 KB |
Output is correct |
48 |
Correct |
29 ms |
9236 KB |
Output is correct |
49 |
Correct |
23 ms |
9272 KB |
Output is correct |
50 |
Correct |
70 ms |
9260 KB |
Output is correct |
51 |
Correct |
27 ms |
9776 KB |
Output is correct |
52 |
Correct |
27 ms |
9796 KB |
Output is correct |
53 |
Correct |
81 ms |
9808 KB |
Output is correct |
54 |
Correct |
37 ms |
10600 KB |
Output is correct |
55 |
Correct |
33 ms |
10476 KB |
Output is correct |
56 |
Correct |
97 ms |
10496 KB |
Output is correct |
57 |
Correct |
35 ms |
11284 KB |
Output is correct |
58 |
Correct |
34 ms |
11256 KB |
Output is correct |
59 |
Correct |
111 ms |
11228 KB |
Output is correct |
60 |
Correct |
42 ms |
12148 KB |
Output is correct |
61 |
Correct |
39 ms |
12036 KB |
Output is correct |
62 |
Correct |
127 ms |
12048 KB |
Output is correct |
63 |
Correct |
99 ms |
11992 KB |
Output is correct |
64 |
Correct |
153 ms |
11936 KB |
Output is correct |
65 |
Correct |
149 ms |
11976 KB |
Output is correct |
66 |
Correct |
132 ms |
11932 KB |
Output is correct |
67 |
Incorrect |
107 ms |
11968 KB |
Output isn't correct |
68 |
Correct |
60 ms |
12012 KB |
Output is correct |
69 |
Correct |
57 ms |
12016 KB |
Output is correct |
70 |
Correct |
56 ms |
12100 KB |
Output is correct |
71 |
Correct |
52 ms |
12084 KB |
Output is correct |
72 |
Incorrect |
50 ms |
12060 KB |
Output isn't correct |
73 |
Incorrect |
73 ms |
12068 KB |
Output isn't correct |
74 |
Correct |
73 ms |
12056 KB |
Output is correct |
75 |
Correct |
79 ms |
12108 KB |
Output is correct |
76 |
Correct |
70 ms |
12024 KB |
Output is correct |
77 |
Correct |
70 ms |
12032 KB |
Output is correct |
78 |
Incorrect |
75 ms |
12028 KB |
Output isn't correct |
79 |
Correct |
88 ms |
11900 KB |
Output is correct |
80 |
Correct |
70 ms |
12036 KB |
Output is correct |
81 |
Correct |
75 ms |
11996 KB |
Output is correct |
82 |
Correct |
75 ms |
11996 KB |
Output is correct |
83 |
Correct |
78 ms |
11976 KB |
Output is correct |
84 |
Correct |
77 ms |
12084 KB |
Output is correct |
85 |
Correct |
81 ms |
12072 KB |
Output is correct |
86 |
Correct |
82 ms |
12000 KB |
Output is correct |
87 |
Correct |
87 ms |
12000 KB |
Output is correct |
88 |
Correct |
79 ms |
11972 KB |
Output is correct |
89 |
Correct |
88 ms |
12012 KB |
Output is correct |
90 |
Correct |
90 ms |
12068 KB |
Output is correct |
91 |
Correct |
81 ms |
12000 KB |
Output is correct |
92 |
Correct |
86 ms |
12016 KB |
Output is correct |