Submission #745821

# Submission time Handle Problem Language Result Execution time Memory
745821 2023-05-21T08:21:11 Z fishy15 Mecho (IOI09_mecho) C++17
100 / 100
407 ms 6792 KB
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <numeric>

#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 1000000

using namespace std;

constexpr array<int, 4> dx = {0, 1, 0, -1};
constexpr array<int, 4> dy = {1, 0, -1, 0};

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n, s;
    cin >> n >> s;

    vector<string> grid(n);
    for (int i = 0; i < n; i++) {
        cin >> grid[i];
    }

    vector min_bee(n, vector<int>(n, INF));

    auto valid = [&](int x, int y, string ok) {
        if (x >= 0 && y >= 0 && x < n && y < n) {
            return count(ok.begin(), ok.end(), grid[x][y]) > 0;
        }
        return false; 
    };

    queue<array<int, 2>> q;
    array<int, 2> start, end;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (grid[i][j] == 'H') {
                min_bee[i][j] = 0;
                q.push({i, j});
            } else if (grid[i][j] == 'M') {
                start = {i, j};
            } else if (grid[i][j] == 'D') {
                end = {i, j};
            }
        }
    }

    while (!q.empty()) {
        auto [x, y] = q.front();
        q.pop();

        for (int d = 0; d < 4; d++) {
            int nx = x + dx[d];
            int ny = y + dy[d];
            if (valid(nx, ny, "GM") && min_bee[nx][ny] == INF) {
                min_bee[nx][ny] = min_bee[x][y] + 1;
                q.push({nx, ny});
            }
        }
    }

    vector min_reach(n, vector<int>(n));
    auto check = [&](int wait) {
        queue<array<int, 2>> q;
        
        if (wait >= min_bee[start[0]][start[1]]) {
            return false;
        }

        for (auto &v : min_reach) {
            fill(v.begin(), v.end(), INF);
        }

        min_reach[start[0]][start[1]] = wait * s;
        q.push(start);

        while (!q.empty()) {
            auto [x, y] = q.front();
            q.pop();

            for (int d = 0; d < 4; d++) {
                int nx = x + dx[d];
                int ny = y + dy[d];
                int nt = min_reach[x][y] + 1;
                if (valid(nx, ny, "GHD") && min_reach[nx][ny] == INF && nt / s < min_bee[nx][ny]) {
                    min_reach[nx][ny] = nt;
                    q.push({nx, ny});
                }
            }
        }

        return min_reach[end[0]][end[1]] < INF;
    };

    int ans = -1;
    int l = 0;
    int r = n * n;

    while (l <= r) {
        int m = (l + r) / 2;
        if (check(m)) {
            ans = m;
            l = m + 1;
        } else {
            r = m - 1;
        }
    }

    cout << ans << '\n';

    return 0;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:112:40: warning: 'end.std::array<int, 2>::_M_elems[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
  112 |         return min_reach[end[0]][end[1]] < INF;
      |                                        ^
mecho.cpp:112:32: warning: 'end.std::array<int, 2>::_M_elems[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
  112 |         return min_reach[end[0]][end[1]] < INF;
      |                                ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 197 ms 6716 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 244 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 324 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 328 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 8 ms 1484 KB Output is correct
34 Correct 9 ms 1492 KB Output is correct
35 Correct 48 ms 1560 KB Output is correct
36 Correct 9 ms 1876 KB Output is correct
37 Correct 11 ms 1860 KB Output is correct
38 Correct 60 ms 1932 KB Output is correct
39 Correct 11 ms 2260 KB Output is correct
40 Correct 30 ms 2260 KB Output is correct
41 Correct 84 ms 2260 KB Output is correct
42 Correct 14 ms 2772 KB Output is correct
43 Correct 18 ms 2772 KB Output is correct
44 Correct 103 ms 2828 KB Output is correct
45 Correct 17 ms 3268 KB Output is correct
46 Correct 21 ms 3332 KB Output is correct
47 Correct 121 ms 3284 KB Output is correct
48 Correct 19 ms 3924 KB Output is correct
49 Correct 24 ms 3916 KB Output is correct
50 Correct 147 ms 3916 KB Output is correct
51 Correct 23 ms 4524 KB Output is correct
52 Correct 29 ms 4404 KB Output is correct
53 Correct 187 ms 4544 KB Output is correct
54 Correct 26 ms 5196 KB Output is correct
55 Correct 32 ms 5200 KB Output is correct
56 Correct 227 ms 5216 KB Output is correct
57 Correct 37 ms 5836 KB Output is correct
58 Correct 42 ms 5880 KB Output is correct
59 Correct 284 ms 5884 KB Output is correct
60 Correct 41 ms 6604 KB Output is correct
61 Correct 47 ms 6592 KB Output is correct
62 Correct 271 ms 6708 KB Output is correct
63 Correct 216 ms 6704 KB Output is correct
64 Correct 407 ms 6704 KB Output is correct
65 Correct 357 ms 6608 KB Output is correct
66 Correct 249 ms 6732 KB Output is correct
67 Correct 229 ms 6724 KB Output is correct
68 Correct 83 ms 6620 KB Output is correct
69 Correct 85 ms 6620 KB Output is correct
70 Correct 65 ms 6632 KB Output is correct
71 Correct 69 ms 6624 KB Output is correct
72 Correct 57 ms 6620 KB Output is correct
73 Correct 65 ms 6728 KB Output is correct
74 Correct 121 ms 6720 KB Output is correct
75 Correct 158 ms 6692 KB Output is correct
76 Correct 141 ms 6792 KB Output is correct
77 Correct 218 ms 6720 KB Output is correct
78 Correct 221 ms 6728 KB Output is correct
79 Correct 119 ms 6716 KB Output is correct
80 Correct 156 ms 6732 KB Output is correct
81 Correct 215 ms 6732 KB Output is correct
82 Correct 205 ms 6716 KB Output is correct
83 Correct 190 ms 6728 KB Output is correct
84 Correct 181 ms 6604 KB Output is correct
85 Correct 167 ms 6684 KB Output is correct
86 Correct 175 ms 6736 KB Output is correct
87 Correct 194 ms 6728 KB Output is correct
88 Correct 222 ms 6724 KB Output is correct
89 Correct 246 ms 6724 KB Output is correct
90 Correct 211 ms 6724 KB Output is correct
91 Correct 193 ms 6720 KB Output is correct
92 Correct 286 ms 6724 KB Output is correct