#include <bits/stdc++.h>
#ifdef VSLOCAL
#include "debug/d.h"
#endif
#ifdef LOCAL
// Don't use this for USACO
#include "../../debug/d.h"
#endif
using namespace std;
using ll = long long;
using ld = long double;
#if defined(LOCAL) || defined(VSLOCAL)
#define dbg(...) line_info(__LINE__, #__VA_ARGS__), dbg1(__VA_ARGS__)
void nline() { cerr << '\n'; }
#else
#define dbg(...) 0
void nline() {}
#endif
int main() {
ios::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("log.txt", "w", stderr);
#endif
const array<int, 4> dx {-1, 0, 1, 0}, dy {0, 1, 0, -1};
int N, S;
cin >> N >> S;
vector<vector<char>> grid (N, vector<char> (N));
queue<pair<ll, pair<int, int>>> q;
vector<vector<ll>> beetime (N, vector<ll> (N, (ll)4e18));
vector<vector<bool>> visited (N, vector<bool> (N));
pair<int, int> honey, home;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> grid[i][j];
if (grid[i][j] == 'H') {
q.push({0, {i, j}});
visited[i][j] = true;
beetime[i][j] = 0;
}
else if (grid[i][j] == 'M') honey = {i, j};
else if (grid[i][j] == 'D') home = {i, j};
}
}
while (!q.empty()) {
auto [dist, loc] = q.front();
q.pop();
for (int d = 0; d < 4; ++d) {
pair<int, int> nloc = {loc.first + dx[d], loc.second + dy[d]};
if (nloc.first < 0 || nloc.first >= N || nloc.second < 0 || nloc.second >= N || visited[nloc.first][nloc.second]) continue;
if (grid[nloc.first][nloc.second] == 'G' || grid[nloc.first][nloc.second] == 'M') {
visited[nloc.first][nloc.second] = true;
beetime[nloc.first][nloc.second] = dist + S;
q.push({dist + S, nloc});
}
}
}
dbg(beetime);
auto good = [&](ll val) {
visited.assign(N, vector<bool> (N));
visited[honey.first][honey.second] = true;
q.push({val * S, honey});
while (!q.empty()) {
auto [dist, loc] = q.front();
q.pop();
for (int d = 0; d < 4; ++d) {
pair<int, int> nloc = {loc.first + dx[d], loc.second + dy[d]};
if (nloc.first < 0 || nloc.first >= N || nloc.second < 0 || nloc.second >= N
|| visited[nloc.first][nloc.second] || dist + 1 >= beetime[nloc.first][nloc.second]) continue;
if (grid[nloc.first][nloc.second] == 'D') {
return true;
}
else if (grid[nloc.first][nloc.second] == 'G') {
visited[nloc.first][nloc.second] = true;
q.push({dist + 1, nloc});
}
}
}
return false;
};
ll low = 0, high = 1e7;
while (low < high) {
ll mid = (low + high + 1) / 2;
if (good(mid)) low = mid;
else high = mid - 1;
}
if (good(low)) cout << low << '\n';
else cout << "-1\n";
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:19:18: warning: statement has no effect [-Wunused-value]
19 | #define dbg(...) 0
| ^
mecho.cpp:68:5: note: in expansion of macro 'dbg'
68 | dbg(beetime);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Incorrect |
62 ms |
6260 KB |
Output isn't correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
304 KB |
Output is correct |
10 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
33 |
Incorrect |
4 ms |
1356 KB |
Output isn't correct |
34 |
Incorrect |
4 ms |
1356 KB |
Output isn't correct |
35 |
Incorrect |
18 ms |
1356 KB |
Output isn't correct |
36 |
Incorrect |
5 ms |
1796 KB |
Output isn't correct |
37 |
Incorrect |
5 ms |
1740 KB |
Output isn't correct |
38 |
Incorrect |
22 ms |
1740 KB |
Output isn't correct |
39 |
Incorrect |
6 ms |
2124 KB |
Output isn't correct |
40 |
Incorrect |
6 ms |
2124 KB |
Output isn't correct |
41 |
Incorrect |
29 ms |
2184 KB |
Output isn't correct |
42 |
Incorrect |
10 ms |
2508 KB |
Output isn't correct |
43 |
Incorrect |
9 ms |
2508 KB |
Output isn't correct |
44 |
Incorrect |
39 ms |
2568 KB |
Output isn't correct |
45 |
Incorrect |
12 ms |
3084 KB |
Output isn't correct |
46 |
Incorrect |
10 ms |
3020 KB |
Output isn't correct |
47 |
Incorrect |
44 ms |
3020 KB |
Output isn't correct |
48 |
Incorrect |
13 ms |
3600 KB |
Output isn't correct |
49 |
Incorrect |
11 ms |
3600 KB |
Output isn't correct |
50 |
Incorrect |
51 ms |
3608 KB |
Output isn't correct |
51 |
Incorrect |
15 ms |
4172 KB |
Output isn't correct |
52 |
Incorrect |
12 ms |
4176 KB |
Output isn't correct |
53 |
Incorrect |
86 ms |
4180 KB |
Output isn't correct |
54 |
Incorrect |
17 ms |
4684 KB |
Output isn't correct |
55 |
Incorrect |
17 ms |
4780 KB |
Output isn't correct |
56 |
Incorrect |
83 ms |
4780 KB |
Output isn't correct |
57 |
Incorrect |
18 ms |
5324 KB |
Output isn't correct |
58 |
Incorrect |
18 ms |
5440 KB |
Output isn't correct |
59 |
Incorrect |
112 ms |
5444 KB |
Output isn't correct |
60 |
Incorrect |
20 ms |
6132 KB |
Output isn't correct |
61 |
Incorrect |
20 ms |
6128 KB |
Output isn't correct |
62 |
Incorrect |
113 ms |
6136 KB |
Output isn't correct |
63 |
Correct |
119 ms |
6132 KB |
Output is correct |
64 |
Correct |
221 ms |
6132 KB |
Output is correct |
65 |
Correct |
190 ms |
6120 KB |
Output is correct |
66 |
Correct |
140 ms |
6120 KB |
Output is correct |
67 |
Correct |
131 ms |
6120 KB |
Output is correct |
68 |
Correct |
55 ms |
6176 KB |
Output is correct |
69 |
Correct |
55 ms |
6180 KB |
Output is correct |
70 |
Correct |
53 ms |
6092 KB |
Output is correct |
71 |
Correct |
45 ms |
6136 KB |
Output is correct |
72 |
Incorrect |
34 ms |
6132 KB |
Output isn't correct |
73 |
Incorrect |
36 ms |
6664 KB |
Output isn't correct |
74 |
Incorrect |
35 ms |
6660 KB |
Output isn't correct |
75 |
Incorrect |
42 ms |
6684 KB |
Output isn't correct |
76 |
Incorrect |
44 ms |
6680 KB |
Output isn't correct |
77 |
Incorrect |
52 ms |
6612 KB |
Output isn't correct |
78 |
Correct |
86 ms |
6624 KB |
Output is correct |
79 |
Incorrect |
34 ms |
6604 KB |
Output isn't correct |
80 |
Incorrect |
37 ms |
6604 KB |
Output isn't correct |
81 |
Incorrect |
47 ms |
6528 KB |
Output isn't correct |
82 |
Incorrect |
38 ms |
6548 KB |
Output isn't correct |
83 |
Correct |
86 ms |
6476 KB |
Output is correct |
84 |
Incorrect |
57 ms |
6524 KB |
Output isn't correct |
85 |
Incorrect |
44 ms |
6476 KB |
Output isn't correct |
86 |
Incorrect |
65 ms |
6536 KB |
Output isn't correct |
87 |
Incorrect |
47 ms |
6536 KB |
Output isn't correct |
88 |
Correct |
94 ms |
6348 KB |
Output is correct |
89 |
Incorrect |
52 ms |
6396 KB |
Output isn't correct |
90 |
Incorrect |
61 ms |
6388 KB |
Output isn't correct |
91 |
Incorrect |
58 ms |
6348 KB |
Output isn't correct |
92 |
Incorrect |
62 ms |
6440 KB |
Output isn't correct |