#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 801;
int n, s;
vector<string> field(mxN);
bool vis1[mxN][mxN], vis2[mxN][mxN];
int d1[mxN][mxN], d2[mxN][mxN];
vector<int> xy = {1, 0, -1, 0}, yx = {0, 1, 0, -1};
bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < n && (field[x][y] == 'G'
|| field[x][y] == 'M'));
}
void bfs1(queue<array<int, 2>> &q) {
while(!q.empty()) {
auto x = q.front()[0], y = q.front()[1];
q.pop();
for(int k = 0; k < 4; k++) {
int a = x + xy[k], b = y + yx[k];
if(valid(a, b) && !vis1[a][b]) {
vis1[a][b] = true; d1[a][b] = d1[x][y]+1;
q.push({a, b});
}
}
}
}
bool reaches(int a, int b) {
return a/s < b;
}
void bfs2(queue<array<int, 2>> &q, int t) {
while(!q.empty()) {
auto x = q.front()[0], y = q.front()[1];
q.pop();
for(int k = 0; k < 4; k++) {
int a = x + xy[k], b = y + yx[k];
if(valid(a, b) && !vis2[a][b] && reaches(d2[x][y]+1, d1[a][b] - t)) {
vis2[a][b] = true;
d2[a][b] = d2[x][y] + 1;
q.push({a, b});
}
}
}
}
void restart() {
memset(vis2, false, sizeof vis2);
memset(d2, 0, sizeof d2);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
vector<array<int, 2>> hives;
array<int, 2> start, finish;
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};
}
}
}
queue<array<int, 2>> q;
for(auto it : hives) {
q.push(it);
vis1[it[0]][it[1]] = true;
}
bfs1(q);
int l = 0, r = n*n;
while(l <= r) {
int mid = (l+r)/2;
restart();
q.push(start);
if(d1[start[0]][start[1]] <= mid) q.pop();
vis2[start[0]][start[1]] = true;
bfs2(q, mid);
bool can = false;
for(int k = 0; k < 4; k++) {
int a = finish[0] + xy[k], b = finish[1] + yx[k];
if(valid(a, b) && vis2[a][b] &&
reaches(d2[a][b], d1[a][b]-mid)) {
can = true;
}
}
if(can) l = mid+1;
else r = mid-1;
}
cout << l-1;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:93:60: warning: 'finish.std::array<int, 2>::_M_elems[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
93 | int a = finish[0] + xy[k], b = finish[1] + yx[k];
| ^
mecho.cpp:93:37: warning: 'finish.std::array<int, 2>::_M_elems[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
93 | int a = finish[0] + xy[k], b = finish[1] + yx[k];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
54 ms |
7476 KB |
Output is correct |
8 |
Correct |
2 ms |
3672 KB |
Output is correct |
9 |
Correct |
2 ms |
3676 KB |
Output is correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
2 ms |
3676 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
13 |
Correct |
2 ms |
3676 KB |
Output is correct |
14 |
Correct |
2 ms |
3676 KB |
Output is correct |
15 |
Correct |
2 ms |
3676 KB |
Output is correct |
16 |
Correct |
2 ms |
3676 KB |
Output is correct |
17 |
Correct |
2 ms |
3676 KB |
Output is correct |
18 |
Correct |
2 ms |
3676 KB |
Output is correct |
19 |
Correct |
2 ms |
3676 KB |
Output is correct |
20 |
Correct |
2 ms |
3676 KB |
Output is correct |
21 |
Correct |
2 ms |
3676 KB |
Output is correct |
22 |
Correct |
2 ms |
3676 KB |
Output is correct |
23 |
Correct |
2 ms |
3676 KB |
Output is correct |
24 |
Correct |
2 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
3676 KB |
Output is correct |
26 |
Correct |
2 ms |
3676 KB |
Output is correct |
27 |
Correct |
2 ms |
3676 KB |
Output is correct |
28 |
Correct |
2 ms |
3676 KB |
Output is correct |
29 |
Correct |
2 ms |
3928 KB |
Output is correct |
30 |
Correct |
2 ms |
3676 KB |
Output is correct |
31 |
Correct |
2 ms |
3932 KB |
Output is correct |
32 |
Correct |
2 ms |
3932 KB |
Output is correct |
33 |
Correct |
5 ms |
4956 KB |
Output is correct |
34 |
Correct |
5 ms |
4952 KB |
Output is correct |
35 |
Correct |
16 ms |
4952 KB |
Output is correct |
36 |
Correct |
6 ms |
5212 KB |
Output is correct |
37 |
Correct |
6 ms |
5212 KB |
Output is correct |
38 |
Correct |
22 ms |
5368 KB |
Output is correct |
39 |
Correct |
8 ms |
5464 KB |
Output is correct |
40 |
Correct |
7 ms |
5468 KB |
Output is correct |
41 |
Correct |
27 ms |
5624 KB |
Output is correct |
42 |
Correct |
7 ms |
5724 KB |
Output is correct |
43 |
Correct |
8 ms |
5724 KB |
Output is correct |
44 |
Correct |
37 ms |
5724 KB |
Output is correct |
45 |
Correct |
8 ms |
5980 KB |
Output is correct |
46 |
Correct |
9 ms |
5980 KB |
Output is correct |
47 |
Correct |
44 ms |
6100 KB |
Output is correct |
48 |
Correct |
9 ms |
6236 KB |
Output is correct |
49 |
Correct |
11 ms |
6236 KB |
Output is correct |
50 |
Correct |
53 ms |
6484 KB |
Output is correct |
51 |
Correct |
10 ms |
6492 KB |
Output is correct |
52 |
Correct |
11 ms |
6492 KB |
Output is correct |
53 |
Correct |
58 ms |
6492 KB |
Output is correct |
54 |
Correct |
12 ms |
6744 KB |
Output is correct |
55 |
Correct |
12 ms |
6744 KB |
Output is correct |
56 |
Correct |
65 ms |
6876 KB |
Output is correct |
57 |
Correct |
13 ms |
7004 KB |
Output is correct |
58 |
Correct |
15 ms |
7000 KB |
Output is correct |
59 |
Correct |
71 ms |
7000 KB |
Output is correct |
60 |
Correct |
14 ms |
7260 KB |
Output is correct |
61 |
Correct |
15 ms |
7260 KB |
Output is correct |
62 |
Correct |
91 ms |
7420 KB |
Output is correct |
63 |
Correct |
82 ms |
7416 KB |
Output is correct |
64 |
Correct |
157 ms |
7412 KB |
Output is correct |
65 |
Correct |
110 ms |
7256 KB |
Output is correct |
66 |
Correct |
87 ms |
7424 KB |
Output is correct |
67 |
Correct |
83 ms |
7256 KB |
Output is correct |
68 |
Correct |
34 ms |
7260 KB |
Output is correct |
69 |
Correct |
43 ms |
7252 KB |
Output is correct |
70 |
Correct |
32 ms |
7260 KB |
Output is correct |
71 |
Correct |
37 ms |
7256 KB |
Output is correct |
72 |
Correct |
27 ms |
7252 KB |
Output is correct |
73 |
Correct |
21 ms |
7512 KB |
Output is correct |
74 |
Correct |
57 ms |
7768 KB |
Output is correct |
75 |
Correct |
48 ms |
7716 KB |
Output is correct |
76 |
Correct |
40 ms |
7516 KB |
Output is correct |
77 |
Correct |
48 ms |
7516 KB |
Output is correct |
78 |
Correct |
50 ms |
7512 KB |
Output is correct |
79 |
Correct |
37 ms |
7512 KB |
Output is correct |
80 |
Correct |
44 ms |
7516 KB |
Output is correct |
81 |
Correct |
50 ms |
7576 KB |
Output is correct |
82 |
Correct |
43 ms |
7636 KB |
Output is correct |
83 |
Correct |
61 ms |
7640 KB |
Output is correct |
84 |
Correct |
55 ms |
7512 KB |
Output is correct |
85 |
Correct |
63 ms |
7640 KB |
Output is correct |
86 |
Correct |
61 ms |
7512 KB |
Output is correct |
87 |
Correct |
67 ms |
7640 KB |
Output is correct |
88 |
Correct |
67 ms |
7580 KB |
Output is correct |
89 |
Correct |
60 ms |
7384 KB |
Output is correct |
90 |
Correct |
73 ms |
7580 KB |
Output is correct |
91 |
Correct |
63 ms |
7576 KB |
Output is correct |
92 |
Correct |
68 ms |
7580 KB |
Output is correct |