#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;
int N; ll 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;
queue<pair<ll, pair<int, int>>> q;
q.push({val * S, honey});
if (val * S >= beetime[honey.first][honey.second]) return false;
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 = 1e16;
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";
// 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:69:5: note: in expansion of macro 'dbg'
69 | dbg(beetime);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is 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 |
Correct |
100 ms |
6348 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
356 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
5 ms |
1460 KB |
Output is correct |
34 |
Correct |
6 ms |
1356 KB |
Output is correct |
35 |
Correct |
23 ms |
1356 KB |
Output is correct |
36 |
Correct |
6 ms |
1740 KB |
Output is correct |
37 |
Correct |
6 ms |
1740 KB |
Output is correct |
38 |
Correct |
30 ms |
1740 KB |
Output is correct |
39 |
Correct |
9 ms |
2124 KB |
Output is correct |
40 |
Correct |
7 ms |
2180 KB |
Output is correct |
41 |
Correct |
42 ms |
2176 KB |
Output is correct |
42 |
Correct |
11 ms |
2508 KB |
Output is correct |
43 |
Correct |
12 ms |
2612 KB |
Output is correct |
44 |
Correct |
49 ms |
2604 KB |
Output is correct |
45 |
Correct |
12 ms |
3080 KB |
Output is correct |
46 |
Correct |
12 ms |
3020 KB |
Output is correct |
47 |
Correct |
58 ms |
3072 KB |
Output is correct |
48 |
Correct |
14 ms |
3608 KB |
Output is correct |
49 |
Correct |
13 ms |
3608 KB |
Output is correct |
50 |
Correct |
76 ms |
3604 KB |
Output is correct |
51 |
Correct |
13 ms |
4172 KB |
Output is correct |
52 |
Correct |
13 ms |
4172 KB |
Output is correct |
53 |
Correct |
85 ms |
4172 KB |
Output is correct |
54 |
Correct |
21 ms |
4776 KB |
Output is correct |
55 |
Correct |
21 ms |
4684 KB |
Output is correct |
56 |
Correct |
124 ms |
4788 KB |
Output is correct |
57 |
Correct |
25 ms |
5324 KB |
Output is correct |
58 |
Correct |
28 ms |
5420 KB |
Output is correct |
59 |
Correct |
127 ms |
5452 KB |
Output is correct |
60 |
Correct |
22 ms |
6092 KB |
Output is correct |
61 |
Correct |
23 ms |
6132 KB |
Output is correct |
62 |
Correct |
136 ms |
6132 KB |
Output is correct |
63 |
Correct |
131 ms |
6120 KB |
Output is correct |
64 |
Correct |
201 ms |
6132 KB |
Output is correct |
65 |
Correct |
171 ms |
6092 KB |
Output is correct |
66 |
Correct |
147 ms |
6116 KB |
Output is correct |
67 |
Correct |
143 ms |
6248 KB |
Output is correct |
68 |
Correct |
57 ms |
6092 KB |
Output is correct |
69 |
Correct |
48 ms |
6172 KB |
Output is correct |
70 |
Correct |
47 ms |
6092 KB |
Output is correct |
71 |
Correct |
45 ms |
6176 KB |
Output is correct |
72 |
Correct |
36 ms |
6136 KB |
Output is correct |
73 |
Correct |
44 ms |
6656 KB |
Output is correct |
74 |
Correct |
71 ms |
6724 KB |
Output is correct |
75 |
Correct |
77 ms |
6604 KB |
Output is correct |
76 |
Correct |
70 ms |
6676 KB |
Output is correct |
77 |
Correct |
69 ms |
6604 KB |
Output is correct |
78 |
Correct |
85 ms |
6608 KB |
Output is correct |
79 |
Correct |
65 ms |
6612 KB |
Output is correct |
80 |
Correct |
79 ms |
6616 KB |
Output is correct |
81 |
Correct |
76 ms |
6604 KB |
Output is correct |
82 |
Correct |
70 ms |
6604 KB |
Output is correct |
83 |
Correct |
86 ms |
6532 KB |
Output is correct |
84 |
Correct |
78 ms |
6532 KB |
Output is correct |
85 |
Correct |
81 ms |
6552 KB |
Output is correct |
86 |
Correct |
87 ms |
6664 KB |
Output is correct |
87 |
Correct |
85 ms |
6512 KB |
Output is correct |
88 |
Correct |
86 ms |
6392 KB |
Output is correct |
89 |
Correct |
86 ms |
6440 KB |
Output is correct |
90 |
Correct |
90 ms |
6440 KB |
Output is correct |
91 |
Correct |
88 ms |
6388 KB |
Output is correct |
92 |
Correct |
104 ms |
6440 KB |
Output is correct |