#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;
while (!q.empty()) q.pop();
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 = 1e15;
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 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
7 |
Incorrect |
80 ms |
6336 KB |
Output isn't correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
11 |
Incorrect |
0 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 |
2 ms |
332 KB |
Output is correct |
15 |
Incorrect |
1 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 |
0 ms |
204 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
204 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 |
356 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 |
5 ms |
1356 KB |
Output isn't correct |
34 |
Incorrect |
7 ms |
1460 KB |
Output isn't correct |
35 |
Incorrect |
20 ms |
1356 KB |
Output isn't correct |
36 |
Incorrect |
6 ms |
1740 KB |
Output isn't correct |
37 |
Incorrect |
5 ms |
1740 KB |
Output isn't correct |
38 |
Incorrect |
28 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 |
2124 KB |
Output isn't correct |
42 |
Incorrect |
12 ms |
2508 KB |
Output isn't correct |
43 |
Incorrect |
13 ms |
2612 KB |
Output isn't correct |
44 |
Incorrect |
43 ms |
2600 KB |
Output isn't correct |
45 |
Incorrect |
12 ms |
3020 KB |
Output isn't correct |
46 |
Incorrect |
13 ms |
3020 KB |
Output isn't correct |
47 |
Incorrect |
52 ms |
3096 KB |
Output isn't correct |
48 |
Incorrect |
13 ms |
3604 KB |
Output isn't correct |
49 |
Incorrect |
12 ms |
3604 KB |
Output isn't correct |
50 |
Incorrect |
61 ms |
3604 KB |
Output isn't correct |
51 |
Incorrect |
15 ms |
4180 KB |
Output isn't correct |
52 |
Incorrect |
16 ms |
4172 KB |
Output isn't correct |
53 |
Incorrect |
79 ms |
4172 KB |
Output isn't correct |
54 |
Incorrect |
20 ms |
4684 KB |
Output isn't correct |
55 |
Incorrect |
21 ms |
4684 KB |
Output isn't correct |
56 |
Incorrect |
90 ms |
4772 KB |
Output isn't correct |
57 |
Incorrect |
21 ms |
5324 KB |
Output isn't correct |
58 |
Incorrect |
27 ms |
5444 KB |
Output isn't correct |
59 |
Incorrect |
100 ms |
5452 KB |
Output isn't correct |
60 |
Incorrect |
24 ms |
6128 KB |
Output isn't correct |
61 |
Incorrect |
22 ms |
6092 KB |
Output isn't correct |
62 |
Incorrect |
109 ms |
6092 KB |
Output isn't correct |
63 |
Incorrect |
101 ms |
6136 KB |
Output isn't correct |
64 |
Incorrect |
159 ms |
6092 KB |
Output isn't correct |
65 |
Incorrect |
157 ms |
6124 KB |
Output isn't correct |
66 |
Incorrect |
138 ms |
6136 KB |
Output isn't correct |
67 |
Correct |
103 ms |
6092 KB |
Output is correct |
68 |
Incorrect |
47 ms |
6092 KB |
Output isn't correct |
69 |
Incorrect |
50 ms |
6196 KB |
Output isn't correct |
70 |
Incorrect |
47 ms |
6160 KB |
Output isn't correct |
71 |
Incorrect |
40 ms |
6168 KB |
Output isn't correct |
72 |
Incorrect |
35 ms |
6092 KB |
Output isn't correct |
73 |
Incorrect |
44 ms |
6684 KB |
Output isn't correct |
74 |
Incorrect |
74 ms |
6740 KB |
Output isn't correct |
75 |
Incorrect |
81 ms |
6664 KB |
Output isn't correct |
76 |
Incorrect |
73 ms |
6668 KB |
Output isn't correct |
77 |
Incorrect |
62 ms |
6664 KB |
Output isn't correct |
78 |
Correct |
68 ms |
6520 KB |
Output is correct |
79 |
Incorrect |
57 ms |
6540 KB |
Output isn't correct |
80 |
Incorrect |
62 ms |
6604 KB |
Output isn't correct |
81 |
Incorrect |
70 ms |
6648 KB |
Output isn't correct |
82 |
Incorrect |
59 ms |
6620 KB |
Output isn't correct |
83 |
Incorrect |
72 ms |
6540 KB |
Output isn't correct |
84 |
Incorrect |
64 ms |
6536 KB |
Output isn't correct |
85 |
Incorrect |
66 ms |
6444 KB |
Output isn't correct |
86 |
Incorrect |
77 ms |
6540 KB |
Output isn't correct |
87 |
Incorrect |
86 ms |
6540 KB |
Output isn't correct |
88 |
Incorrect |
71 ms |
6440 KB |
Output isn't correct |
89 |
Incorrect |
75 ms |
6436 KB |
Output isn't correct |
90 |
Incorrect |
86 ms |
6348 KB |
Output isn't correct |
91 |
Incorrect |
78 ms |
6436 KB |
Output isn't correct |
92 |
Incorrect |
79 ms |
6348 KB |
Output isn't correct |