#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 801;
int n, s;
vector<string> field(mxN);
int d1[mxN][mxN], d2[mxN][mxN];
bool vis1[mxN][mxN], vis2[mxN][mxN];
vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
void restart() {
memset(vis1, false, sizeof vis1);
memset(vis2, false, sizeof vis2);
memset(d1, 0, sizeof d1);
memset(d2, 0, sizeof d2);
}
bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < n && (field[x][y] == 'G' ||
field[x][y] == 'M'));
}
bool reached(int a, int b) {
return a/s < b;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
array<int, 2> start, finish;
vector<array<int, 2>> hives;
for(int i = 0; i < n; i++) {
cin >> field[i];
for(int j = 0; j < n; j++) {
if(field[i][j] == 'M') start = {i, j};
else if(field[i][j] == 'H') hives.push_back({i, j});
else if(field[i][j] == 'D') finish = {i, j};
}
}
int l = 0, r = n * n;
while(l <= r) {
int mid = (r+l)/2;
restart();
queue<array<int, 2>> q;
for(auto it : hives) {
q.push(it);
vis1[it[0]][it[1]] = true;
}
while(!q.empty()) {
int x = q.front()[0], y = q.front()[1];
q.pop();
for(int k = 0; k < 4; k++) {
int a = x + dx[k], b = y + dy[k];
if(valid(a, b) && !vis1[a][b]) {
vis1[a][b] = true;
d1[a][b] = d1[x][y]+1;
q.push({a, b});
}
}
}
q.push(start);
vis2[start[0]][start[1]] = true;
if(d1[start[0]][start[1]] <= mid) q.pop();
while(!q.empty()) {
int x = q.front()[0], y = q.front()[1];
q.pop();
for(int k = 0; k < 4; k++) {
int a = x + dx[k], b = y + dy[k];
if(valid(a, b) && !vis2[a][b] && reached(d2[x][y]+1,
d1[a][b]-mid)) {
vis2[a][b] = true;
q.push({a, b});
d2[a][b] = d2[x][y]+1;
}
}
}
bool can = false;
for(int k = 0; k < 4; k++) {
int a = finish[0]+dx[k], b = finish[1]+dy[k];
if(valid(a, b) && reached(d2[a][b], d1[a][b]-mid) && vis2[a][b]) {
can = true;
}
}
if(can) l = mid+1;
else r = mid-1;
}
cout << l-1;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:84:35: warning: 'finish.std::array<int, 2>::_M_elems[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | int a = finish[0]+dx[k], b = finish[1]+dy[k];
| ^
mecho.cpp:84:56: warning: 'finish.std::array<int, 2>::_M_elems[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | int a = finish[0]+dx[k], b = finish[1]+dy[k];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
3 ms |
6748 KB |
Output is correct |
7 |
Correct |
384 ms |
7536 KB |
Output is correct |
8 |
Correct |
3 ms |
6744 KB |
Output is correct |
9 |
Correct |
3 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
3 ms |
6748 KB |
Output is correct |
12 |
Correct |
4 ms |
6748 KB |
Output is correct |
13 |
Correct |
4 ms |
6776 KB |
Output is correct |
14 |
Correct |
4 ms |
6748 KB |
Output is correct |
15 |
Correct |
3 ms |
6748 KB |
Output is correct |
16 |
Correct |
3 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
6748 KB |
Output is correct |
19 |
Correct |
3 ms |
6748 KB |
Output is correct |
20 |
Correct |
3 ms |
6748 KB |
Output is correct |
21 |
Correct |
3 ms |
6748 KB |
Output is correct |
22 |
Correct |
3 ms |
6748 KB |
Output is correct |
23 |
Correct |
4 ms |
6748 KB |
Output is correct |
24 |
Correct |
4 ms |
6572 KB |
Output is correct |
25 |
Correct |
4 ms |
6748 KB |
Output is correct |
26 |
Correct |
4 ms |
6748 KB |
Output is correct |
27 |
Correct |
4 ms |
6748 KB |
Output is correct |
28 |
Correct |
4 ms |
6748 KB |
Output is correct |
29 |
Correct |
4 ms |
6748 KB |
Output is correct |
30 |
Correct |
4 ms |
6748 KB |
Output is correct |
31 |
Correct |
4 ms |
6752 KB |
Output is correct |
32 |
Correct |
4 ms |
6748 KB |
Output is correct |
33 |
Correct |
31 ms |
6868 KB |
Output is correct |
34 |
Correct |
26 ms |
6748 KB |
Output is correct |
35 |
Correct |
60 ms |
6748 KB |
Output is correct |
36 |
Correct |
35 ms |
6744 KB |
Output is correct |
37 |
Correct |
37 ms |
7000 KB |
Output is correct |
38 |
Correct |
81 ms |
6744 KB |
Output is correct |
39 |
Correct |
47 ms |
6744 KB |
Output is correct |
40 |
Correct |
44 ms |
6748 KB |
Output is correct |
41 |
Correct |
100 ms |
6956 KB |
Output is correct |
42 |
Correct |
56 ms |
7004 KB |
Output is correct |
43 |
Correct |
59 ms |
7004 KB |
Output is correct |
44 |
Correct |
121 ms |
7000 KB |
Output is correct |
45 |
Correct |
70 ms |
6916 KB |
Output is correct |
46 |
Correct |
61 ms |
7004 KB |
Output is correct |
47 |
Correct |
158 ms |
7068 KB |
Output is correct |
48 |
Correct |
97 ms |
7004 KB |
Output is correct |
49 |
Correct |
77 ms |
7000 KB |
Output is correct |
50 |
Correct |
169 ms |
7004 KB |
Output is correct |
51 |
Correct |
99 ms |
7004 KB |
Output is correct |
52 |
Correct |
88 ms |
7004 KB |
Output is correct |
53 |
Correct |
200 ms |
7004 KB |
Output is correct |
54 |
Correct |
115 ms |
7256 KB |
Output is correct |
55 |
Correct |
114 ms |
7256 KB |
Output is correct |
56 |
Correct |
237 ms |
7256 KB |
Output is correct |
57 |
Correct |
126 ms |
7260 KB |
Output is correct |
58 |
Correct |
125 ms |
7352 KB |
Output is correct |
59 |
Correct |
314 ms |
7508 KB |
Output is correct |
60 |
Correct |
155 ms |
7256 KB |
Output is correct |
61 |
Correct |
142 ms |
7412 KB |
Output is correct |
62 |
Correct |
325 ms |
7448 KB |
Output is correct |
63 |
Correct |
368 ms |
7508 KB |
Output is correct |
64 |
Correct |
500 ms |
7420 KB |
Output is correct |
65 |
Correct |
411 ms |
7416 KB |
Output is correct |
66 |
Correct |
393 ms |
7256 KB |
Output is correct |
67 |
Correct |
379 ms |
7508 KB |
Output is correct |
68 |
Correct |
364 ms |
7460 KB |
Output is correct |
69 |
Correct |
381 ms |
7452 KB |
Output is correct |
70 |
Correct |
352 ms |
7444 KB |
Output is correct |
71 |
Correct |
398 ms |
7444 KB |
Output is correct |
72 |
Correct |
334 ms |
7260 KB |
Output is correct |
73 |
Correct |
322 ms |
7724 KB |
Output is correct |
74 |
Correct |
351 ms |
7512 KB |
Output is correct |
75 |
Correct |
388 ms |
7516 KB |
Output is correct |
76 |
Correct |
341 ms |
7716 KB |
Output is correct |
77 |
Correct |
348 ms |
7724 KB |
Output is correct |
78 |
Correct |
368 ms |
7680 KB |
Output is correct |
79 |
Correct |
339 ms |
7676 KB |
Output is correct |
80 |
Correct |
370 ms |
7516 KB |
Output is correct |
81 |
Correct |
363 ms |
7684 KB |
Output is correct |
82 |
Correct |
342 ms |
7684 KB |
Output is correct |
83 |
Correct |
389 ms |
7760 KB |
Output is correct |
84 |
Correct |
365 ms |
7660 KB |
Output is correct |
85 |
Correct |
400 ms |
7512 KB |
Output is correct |
86 |
Correct |
381 ms |
7644 KB |
Output is correct |
87 |
Correct |
378 ms |
7640 KB |
Output is correct |
88 |
Correct |
364 ms |
7584 KB |
Output is correct |
89 |
Correct |
384 ms |
7516 KB |
Output is correct |
90 |
Correct |
381 ms |
7512 KB |
Output is correct |
91 |
Correct |
372 ms |
7760 KB |
Output is correct |
92 |
Correct |
390 ms |
7516 KB |
Output is correct |