Submission #386523

# Submission time Handle Problem Language Result Execution time Memory
386523 2021-04-06T18:06:01 Z SansPapyrus683 Mecho (IOI09_mecho) C++17
68 / 100
637 ms 9240 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) {
            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;
        }
    }
    if (valid == -1) {
      throw -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 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 427 ms 6840 KB Output is correct
8 Runtime error 5 ms 620 KB Execution killed with signal 6
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 1 ms 364 KB Output isn't correct
13 Incorrect 1 ms 364 KB Output isn't correct
14 Runtime error 4 ms 620 KB Execution killed with signal 6
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 1 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 364 KB Output is correct
32 Correct 2 ms 492 KB Output is correct
33 Correct 22 ms 1612 KB Output is correct
34 Correct 20 ms 1612 KB Output is correct
35 Correct 101 ms 1616 KB Output is correct
36 Correct 24 ms 1996 KB Output is correct
37 Correct 26 ms 1996 KB Output is correct
38 Correct 129 ms 2132 KB Output is correct
39 Correct 30 ms 2412 KB Output is correct
40 Correct 33 ms 2560 KB Output is correct
41 Correct 184 ms 2412 KB Output is correct
42 Correct 47 ms 2948 KB Output is correct
43 Correct 41 ms 2956 KB Output is correct
44 Correct 216 ms 2952 KB Output is correct
45 Correct 48 ms 3468 KB Output is correct
46 Correct 49 ms 3452 KB Output is correct
47 Correct 252 ms 3500 KB Output is correct
48 Correct 54 ms 3972 KB Output is correct
49 Correct 57 ms 3972 KB Output is correct
50 Correct 303 ms 4112 KB Output is correct
51 Correct 64 ms 4664 KB Output is correct
52 Correct 70 ms 4664 KB Output is correct
53 Correct 368 ms 4644 KB Output is correct
54 Correct 76 ms 5332 KB Output is correct
55 Correct 78 ms 5392 KB Output is correct
56 Correct 450 ms 5468 KB Output is correct
57 Correct 88 ms 6092 KB Output is correct
58 Correct 92 ms 6100 KB Output is correct
59 Correct 633 ms 5992 KB Output is correct
60 Correct 99 ms 6912 KB Output is correct
61 Correct 101 ms 6884 KB Output is correct
62 Correct 591 ms 6828 KB Output is correct
63 Correct 352 ms 6828 KB Output is correct
64 Correct 637 ms 6932 KB Output is correct
65 Correct 489 ms 6828 KB Output is correct
66 Correct 412 ms 6828 KB Output is correct
67 Runtime error 403 ms 8916 KB Execution killed with signal 6
68 Correct 181 ms 6860 KB Output is correct
69 Correct 180 ms 6940 KB Output is correct
70 Correct 156 ms 6920 KB Output is correct
71 Correct 160 ms 6892 KB Output is correct
72 Incorrect 123 ms 6892 KB Output isn't correct
73 Incorrect 198 ms 6828 KB Output isn't correct
74 Correct 283 ms 6872 KB Output is correct
75 Correct 348 ms 6852 KB Output is correct
76 Correct 302 ms 6932 KB Output is correct
77 Correct 320 ms 6960 KB Output is correct
78 Runtime error 370 ms 9240 KB Execution killed with signal 6
79 Correct 235 ms 6948 KB Output is correct
80 Correct 295 ms 7064 KB Output is correct
81 Correct 328 ms 6840 KB Output is correct
82 Correct 291 ms 6900 KB Output is correct
83 Correct 417 ms 6848 KB Output is correct
84 Correct 356 ms 6860 KB Output is correct
85 Correct 378 ms 6924 KB Output is correct
86 Correct 408 ms 6884 KB Output is correct
87 Correct 368 ms 6936 KB Output is correct
88 Correct 402 ms 6984 KB Output is correct
89 Correct 410 ms 6824 KB Output is correct
90 Correct 415 ms 6944 KB Output is correct
91 Correct 438 ms 6952 KB Output is correct
92 Correct 438 ms 6964 KB Output is correct