#pragma optimize("unroll-loops,O3")
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 800 + 5;
char matrix[MAXN][MAXN];
int distBee[MAXN][MAXN];
int distBear[MAXN][MAXN];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
#define ONLINE_JUDGE
void solve() {
memset(distBee, -1, sizeof(distBee));
int n, s;
cin >> n >> s;
pair <int, int> initial;
pair <int, int> home;
queue <array <int, 3>> q;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cin >> matrix[i][j];
if(matrix[i][j] == 'H') {
q.push({0, i, j});
} else if(matrix[i][j] == 'M') {
initial = {i, j};
} else if(matrix[i][j] == 'D') {
home = {i, j};
}
}
}
auto in = [&](int x, int y) -> bool {
return 1 <= min(x, y) && max(x, y) <= n;
};
while(!q.empty()) {
auto [d, x, y] = q.front();
q.pop();
if(distBee[x][y] != -1) {
continue;
}
distBee[x][y] = d;
for(int dir = 0; dir < 4; dir++) {
int _x = x + dx[dir], _y = y + dy[dir];
if(in(_x, _y) && matrix[_x][_y] == 'G') {
q.push({d +1, _x, _y});
}
}
}
auto check = [&](int k) -> bool {
memset(distBear, -1, sizeof(distBear));
q.push({s * k, initial.first, initial.second});
while(!q.empty()) {
auto [d, x, y] = q.front();
q.pop();
if(distBear[x][y] != -1 || (distBee[x][y] != -1 && d / s >= distBee[x][y])) {
continue;
}
//cerr << d << " :: " << x << " " << y << " | " << distBee[x][y] << "\n";
distBear[x][y] = d;
for(int dir = 0; dir < 4; dir++) {
int _x = x + dx[dir], _y = y + dy[dir];
if(in(_x, _y) && (matrix[_x][_y] == 'G' || matrix[_x][_y] == 'D') && distBear[_x][_y] == -1) {
q.push({d +1, _x, _y});
}
}
}
if(distBear[home.first][home.second] == -1) {
return false;
}
return true;
};
int l = 0, r = n * n, ans = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(check(mid)) {
ans = mid;
l = mid +1;
} else {
r = mid -1;
}
}
cout << ans;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
Compilation message
mecho.cpp:1: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
1 | #pragma optimize("unroll-loops,O3")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5464 KB |
Output is correct |
2 |
Correct |
1 ms |
5468 KB |
Output is correct |
3 |
Correct |
1 ms |
5468 KB |
Output is correct |
4 |
Correct |
1 ms |
5468 KB |
Output is correct |
5 |
Correct |
1 ms |
5468 KB |
Output is correct |
6 |
Correct |
1 ms |
5464 KB |
Output is correct |
7 |
Correct |
84 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
5464 KB |
Output is correct |
9 |
Correct |
2 ms |
5468 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
1 ms |
5468 KB |
Output is correct |
12 |
Incorrect |
2 ms |
5468 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
5468 KB |
Output isn't correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
1 ms |
5544 KB |
Output is correct |
16 |
Correct |
1 ms |
5468 KB |
Output is correct |
17 |
Correct |
1 ms |
5468 KB |
Output is correct |
18 |
Correct |
2 ms |
5468 KB |
Output is correct |
19 |
Correct |
2 ms |
5468 KB |
Output is correct |
20 |
Correct |
1 ms |
5468 KB |
Output is correct |
21 |
Correct |
1 ms |
5468 KB |
Output is correct |
22 |
Correct |
2 ms |
5468 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5468 KB |
Output is correct |
25 |
Correct |
2 ms |
5468 KB |
Output is correct |
26 |
Correct |
2 ms |
5580 KB |
Output is correct |
27 |
Correct |
2 ms |
5468 KB |
Output is correct |
28 |
Correct |
2 ms |
5468 KB |
Output is correct |
29 |
Correct |
2 ms |
5468 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
2 ms |
5468 KB |
Output is correct |
32 |
Correct |
2 ms |
5468 KB |
Output is correct |
33 |
Correct |
5 ms |
5724 KB |
Output is correct |
34 |
Correct |
5 ms |
5632 KB |
Output is correct |
35 |
Correct |
21 ms |
5640 KB |
Output is correct |
36 |
Correct |
6 ms |
5724 KB |
Output is correct |
37 |
Correct |
7 ms |
5724 KB |
Output is correct |
38 |
Correct |
29 ms |
5868 KB |
Output is correct |
39 |
Correct |
6 ms |
5724 KB |
Output is correct |
40 |
Correct |
10 ms |
5724 KB |
Output is correct |
41 |
Correct |
36 ms |
5912 KB |
Output is correct |
42 |
Correct |
8 ms |
5752 KB |
Output is correct |
43 |
Correct |
9 ms |
5724 KB |
Output is correct |
44 |
Correct |
45 ms |
5772 KB |
Output is correct |
45 |
Correct |
10 ms |
5720 KB |
Output is correct |
46 |
Correct |
10 ms |
5724 KB |
Output is correct |
47 |
Correct |
49 ms |
5976 KB |
Output is correct |
48 |
Correct |
11 ms |
5976 KB |
Output is correct |
49 |
Correct |
12 ms |
5980 KB |
Output is correct |
50 |
Correct |
59 ms |
5856 KB |
Output is correct |
51 |
Correct |
12 ms |
5980 KB |
Output is correct |
52 |
Correct |
14 ms |
5980 KB |
Output is correct |
53 |
Correct |
78 ms |
5872 KB |
Output is correct |
54 |
Correct |
14 ms |
5980 KB |
Output is correct |
55 |
Correct |
16 ms |
5948 KB |
Output is correct |
56 |
Correct |
90 ms |
6092 KB |
Output is correct |
57 |
Correct |
16 ms |
5976 KB |
Output is correct |
58 |
Correct |
17 ms |
5976 KB |
Output is correct |
59 |
Correct |
106 ms |
6140 KB |
Output is correct |
60 |
Correct |
18 ms |
5980 KB |
Output is correct |
61 |
Correct |
19 ms |
5980 KB |
Output is correct |
62 |
Correct |
111 ms |
5972 KB |
Output is correct |
63 |
Correct |
79 ms |
6148 KB |
Output is correct |
64 |
Correct |
149 ms |
6148 KB |
Output is correct |
65 |
Correct |
125 ms |
6240 KB |
Output is correct |
66 |
Correct |
93 ms |
6152 KB |
Output is correct |
67 |
Correct |
88 ms |
5976 KB |
Output is correct |
68 |
Correct |
40 ms |
6224 KB |
Output is correct |
69 |
Correct |
41 ms |
6224 KB |
Output is correct |
70 |
Correct |
32 ms |
6256 KB |
Output is correct |
71 |
Correct |
36 ms |
6228 KB |
Output is correct |
72 |
Incorrect |
30 ms |
6068 KB |
Output isn't correct |
73 |
Incorrect |
87 ms |
7604 KB |
Output isn't correct |
74 |
Correct |
65 ms |
7740 KB |
Output is correct |
75 |
Correct |
78 ms |
7828 KB |
Output is correct |
76 |
Correct |
66 ms |
7736 KB |
Output is correct |
77 |
Correct |
69 ms |
7640 KB |
Output is correct |
78 |
Correct |
84 ms |
7504 KB |
Output is correct |
79 |
Correct |
56 ms |
7456 KB |
Output is correct |
80 |
Correct |
67 ms |
7508 KB |
Output is correct |
81 |
Correct |
79 ms |
7616 KB |
Output is correct |
82 |
Correct |
67 ms |
7508 KB |
Output is correct |
83 |
Correct |
78 ms |
7224 KB |
Output is correct |
84 |
Correct |
82 ms |
7372 KB |
Output is correct |
85 |
Correct |
80 ms |
7364 KB |
Output is correct |
86 |
Correct |
78 ms |
7204 KB |
Output is correct |
87 |
Correct |
78 ms |
7316 KB |
Output is correct |
88 |
Correct |
79 ms |
6972 KB |
Output is correct |
89 |
Correct |
83 ms |
6992 KB |
Output is correct |
90 |
Correct |
87 ms |
6996 KB |
Output is correct |
91 |
Correct |
86 ms |
6916 KB |
Output is correct |
92 |
Correct |
90 ms |
6996 KB |
Output is correct |