#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using std::cout;
using std::endl;
using std::vector;
using std::pair;
vector<pair<int, int>> neighbors(const pair<int, int>& at, int row_num, int col_num) {
vector<pair<int, int>> neighbors;
if (at.first > 0) {
neighbors.push_back({at.first - 1, at.second});
}
if (at.second > 0) {
neighbors.push_back({at.first, at.second - 1});
}
if (at.first < row_num - 1) {
neighbors.push_back({at.first + 1, at.second});
}
if (at.second < col_num - 1) {
neighbors.push_back({at.first, at.second + 1});
}
return neighbors;
}
class Field {
private:
const vector<vector<char>> field;
const int row_num;
const int col_num;
pair<int, int> start;
vector<vector<int>> bee_dists;
public:
Field(const vector<vector<char>>& field)
: field(field), row_num(field.size()), col_num(field[0].size()),
bee_dists(row_num, vector<int>(col_num, INT_MAX)) {
std::queue<pair<int, int>> frontier;
for (int r = 0; r < row_num; r++) {
for (int c = 0; c < col_num; c++) {
if (field[r][c] == 'H') {
frontier.push({r, c});
bee_dists[r][c] = 0;
} else if (field[r][c] == 'M') {
start = {r, c};
}
}
}
while (!frontier.empty()) {
pair<int, int> c = frontier.front(); // c for curr and to reduce my line length
frontier.pop();
int n_cost = bee_dists[c.first][c.second] + 1;
for (const pair<int, int>& n : neighbors(c, row_num, col_num)) {
if (n_cost < bee_dists[n.first][n.second]
&& field[n.first][n.second] != 'T'
&& field[n.first][n.second] != 'D') {
bee_dists[n.first][n.second] = n_cost;
frontier.push(n);
}
}
}
}
bool can_go_home(int honey_time, int speed) {
std::queue<pair<int, int>> frontier;
vector<vector<int>> min_moves(row_num, vector<int>(col_num, INT_MAX));
frontier.push(start);
min_moves[start.first][start.second] = honey_time * speed;
while (!frontier.empty()) {
pair<int, int> curr = frontier.front();
frontier.pop();
if (field[curr.first][curr.second] == 'D') {
return true;
}
int n_cost = min_moves[curr.first][curr.second] + 1;
for (const pair<int, int>& n : neighbors(curr, row_num, col_num)) {
if (field[n.first][n.second] != 'T'
&& (n_cost / speed) < bee_dists[n.first][n.second]
&& n_cost < min_moves[n.first][n.second]) {
min_moves[n.first][n.second] = n_cost;
frontier.push(n);
}
}
}
return false;
}
};
// https://oj.uz/problem/view/IOI09_mecho (sample input ommitted)
int main() {
int side_len;
int speed;
std::cin >> side_len >> speed;
vector<vector<char>> grid(side_len, vector<char>(side_len));
for (int r = 0; r < side_len; r++) {
for (int c = 0; c < side_len; c++) {
std::cin >> grid[r][c];
}
}
Field field(grid);
int lo = 0;
int hi = side_len * side_len;
int valid = -1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (field.can_go_home(mid, speed)) {
valid = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << valid << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
442 ms |
7500 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
2 ms |
364 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
364 KB |
Output is correct |
29 |
Correct |
2 ms |
364 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
364 KB |
Output is correct |
32 |
Correct |
2 ms |
364 KB |
Output is correct |
33 |
Correct |
22 ms |
1612 KB |
Output is correct |
34 |
Correct |
20 ms |
1724 KB |
Output is correct |
35 |
Correct |
96 ms |
1796 KB |
Output is correct |
36 |
Correct |
24 ms |
1996 KB |
Output is correct |
37 |
Correct |
29 ms |
2156 KB |
Output is correct |
38 |
Correct |
146 ms |
2476 KB |
Output is correct |
39 |
Correct |
31 ms |
2412 KB |
Output is correct |
40 |
Correct |
32 ms |
2612 KB |
Output is correct |
41 |
Correct |
171 ms |
2728 KB |
Output is correct |
42 |
Correct |
41 ms |
2940 KB |
Output is correct |
43 |
Correct |
42 ms |
3200 KB |
Output is correct |
44 |
Correct |
215 ms |
3164 KB |
Output is correct |
45 |
Correct |
47 ms |
3532 KB |
Output is correct |
46 |
Correct |
50 ms |
3740 KB |
Output is correct |
47 |
Correct |
243 ms |
3724 KB |
Output is correct |
48 |
Correct |
52 ms |
3996 KB |
Output is correct |
49 |
Correct |
65 ms |
4372 KB |
Output is correct |
50 |
Correct |
305 ms |
4324 KB |
Output is correct |
51 |
Correct |
64 ms |
4664 KB |
Output is correct |
52 |
Correct |
70 ms |
5072 KB |
Output is correct |
53 |
Correct |
376 ms |
5240 KB |
Output is correct |
54 |
Correct |
74 ms |
5332 KB |
Output is correct |
55 |
Correct |
77 ms |
5820 KB |
Output is correct |
56 |
Correct |
444 ms |
6004 KB |
Output is correct |
57 |
Correct |
84 ms |
6044 KB |
Output is correct |
58 |
Correct |
91 ms |
6808 KB |
Output is correct |
59 |
Correct |
535 ms |
6628 KB |
Output is correct |
60 |
Correct |
97 ms |
6912 KB |
Output is correct |
61 |
Correct |
101 ms |
7524 KB |
Output is correct |
62 |
Correct |
592 ms |
7592 KB |
Output is correct |
63 |
Correct |
358 ms |
7472 KB |
Output is correct |
64 |
Correct |
685 ms |
7572 KB |
Output is correct |
65 |
Correct |
517 ms |
7620 KB |
Output is correct |
66 |
Correct |
414 ms |
7444 KB |
Output is correct |
67 |
Correct |
390 ms |
7444 KB |
Output is correct |
68 |
Correct |
181 ms |
7596 KB |
Output is correct |
69 |
Correct |
178 ms |
7508 KB |
Output is correct |
70 |
Correct |
142 ms |
7488 KB |
Output is correct |
71 |
Correct |
164 ms |
7700 KB |
Output is correct |
72 |
Incorrect |
129 ms |
7532 KB |
Output isn't correct |
73 |
Incorrect |
202 ms |
7568 KB |
Output isn't correct |
74 |
Correct |
279 ms |
7756 KB |
Output is correct |
75 |
Correct |
340 ms |
7456 KB |
Output is correct |
76 |
Correct |
302 ms |
7568 KB |
Output is correct |
77 |
Correct |
318 ms |
7480 KB |
Output is correct |
78 |
Correct |
366 ms |
7448 KB |
Output is correct |
79 |
Correct |
267 ms |
7488 KB |
Output is correct |
80 |
Correct |
293 ms |
7480 KB |
Output is correct |
81 |
Correct |
327 ms |
7480 KB |
Output is correct |
82 |
Correct |
305 ms |
7484 KB |
Output is correct |
83 |
Correct |
392 ms |
7444 KB |
Output is correct |
84 |
Correct |
360 ms |
7468 KB |
Output is correct |
85 |
Correct |
378 ms |
7476 KB |
Output is correct |
86 |
Correct |
377 ms |
7468 KB |
Output is correct |
87 |
Correct |
378 ms |
7464 KB |
Output is correct |
88 |
Correct |
392 ms |
7600 KB |
Output is correct |
89 |
Correct |
413 ms |
7468 KB |
Output is correct |
90 |
Correct |
423 ms |
7456 KB |
Output is correct |
91 |
Correct |
409 ms |
7584 KB |
Output is correct |
92 |
Correct |
444 ms |
7496 KB |
Output is correct |