Submission #386569

# Submission time Handle Problem Language Result Execution time Memory
386569 2021-04-06T21:58:54 Z SansPapyrus683 Mecho (IOI09_mecho) C++17
100 / 100
632 ms 7008 KB
#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) {
            if (bee_dists[start.first][start.second] <= honey_time) {
                return false;
            }
            std::queue<pair<int, int>> frontier;
            vector<vector<int>> min_moves(row_num, vector<int>(col_num, INT_MAX));
            frontier.push(start);  // dw, we checked if the start was valid literally above
            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;
}
# Verdict Execution time Memory 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 384 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 469 ms 6904 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 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 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is 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 364 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 1 ms 364 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 2 ms 364 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 364 KB Output is correct
31 Correct 2 ms 436 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
33 Correct 18 ms 1632 KB Output is correct
34 Correct 19 ms 1612 KB Output is correct
35 Correct 96 ms 1616 KB Output is correct
36 Correct 23 ms 1996 KB Output is correct
37 Correct 25 ms 1996 KB Output is correct
38 Correct 126 ms 2000 KB Output is correct
39 Correct 29 ms 2436 KB Output is correct
40 Correct 31 ms 2560 KB Output is correct
41 Correct 172 ms 2528 KB Output is correct
42 Correct 37 ms 2932 KB Output is correct
43 Correct 40 ms 2956 KB Output is correct
44 Correct 212 ms 2900 KB Output is correct
45 Correct 43 ms 3452 KB Output is correct
46 Correct 44 ms 3404 KB Output is correct
47 Correct 247 ms 3564 KB Output is correct
48 Correct 54 ms 3964 KB Output is correct
49 Correct 53 ms 3964 KB Output is correct
50 Correct 298 ms 3980 KB Output is correct
51 Correct 60 ms 4712 KB Output is correct
52 Correct 62 ms 4632 KB Output is correct
53 Correct 364 ms 4700 KB Output is correct
54 Correct 72 ms 5324 KB Output is correct
55 Correct 72 ms 5292 KB Output is correct
56 Correct 476 ms 5420 KB Output is correct
57 Correct 83 ms 6012 KB Output is correct
58 Correct 88 ms 6012 KB Output is correct
59 Correct 527 ms 6080 KB Output is correct
60 Correct 95 ms 6944 KB Output is correct
61 Correct 93 ms 6816 KB Output is correct
62 Correct 579 ms 6884 KB Output is correct
63 Correct 354 ms 6920 KB Output is correct
64 Correct 632 ms 6884 KB Output is correct
65 Correct 490 ms 6916 KB Output is correct
66 Correct 403 ms 6868 KB Output is correct
67 Correct 387 ms 6772 KB Output is correct
68 Correct 190 ms 6796 KB Output is correct
69 Correct 170 ms 6860 KB Output is correct
70 Correct 134 ms 6780 KB Output is correct
71 Correct 146 ms 6796 KB Output is correct
72 Correct 111 ms 6764 KB Output is correct
73 Correct 148 ms 6788 KB Output is correct
74 Correct 276 ms 6788 KB Output is correct
75 Correct 327 ms 7008 KB Output is correct
76 Correct 285 ms 6788 KB Output is correct
77 Correct 288 ms 6892 KB Output is correct
78 Correct 355 ms 6892 KB Output is correct
79 Correct 226 ms 6904 KB Output is correct
80 Correct 279 ms 6792 KB Output is correct
81 Correct 318 ms 6904 KB Output is correct
82 Correct 281 ms 6784 KB Output is correct
83 Correct 377 ms 6864 KB Output is correct
84 Correct 336 ms 6912 KB Output is correct
85 Correct 371 ms 6812 KB Output is correct
86 Correct 363 ms 6940 KB Output is correct
87 Correct 360 ms 6780 KB Output is correct
88 Correct 389 ms 6928 KB Output is correct
89 Correct 390 ms 6784 KB Output is correct
90 Correct 411 ms 6892 KB Output is correct
91 Correct 396 ms 6840 KB Output is correct
92 Correct 422 ms 6904 KB Output is correct