답안 #513891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
513891 2022-01-17T22:41:36 Z nhuang685 Mecho (IOI09_mecho) C++17
84 / 100
192 ms 6684 KB
#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;
    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 = 1e7;
    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";
}

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:68:5: note: in expansion of macro 'dbg'
   68 |     dbg(beetime);
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 97 ms 6348 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Incorrect 1 ms 332 KB Output isn't correct
13 Incorrect 1 ms 332 KB Output isn't correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 5 ms 1356 KB Output is correct
34 Correct 4 ms 1356 KB Output is correct
35 Correct 24 ms 1456 KB Output is correct
36 Correct 6 ms 1792 KB Output is correct
37 Correct 5 ms 1740 KB Output is correct
38 Correct 30 ms 1740 KB Output is correct
39 Correct 6 ms 2124 KB Output is correct
40 Correct 6 ms 2184 KB Output is correct
41 Correct 51 ms 2172 KB Output is correct
42 Correct 11 ms 2608 KB Output is correct
43 Correct 9 ms 2508 KB Output is correct
44 Correct 54 ms 2600 KB Output is correct
45 Correct 10 ms 3020 KB Output is correct
46 Correct 11 ms 3020 KB Output is correct
47 Correct 67 ms 3020 KB Output is correct
48 Correct 12 ms 3532 KB Output is correct
49 Correct 13 ms 3604 KB Output is correct
50 Correct 73 ms 3596 KB Output is correct
51 Correct 14 ms 4172 KB Output is correct
52 Correct 13 ms 4172 KB Output is correct
53 Correct 100 ms 4176 KB Output is correct
54 Correct 17 ms 4780 KB Output is correct
55 Correct 17 ms 4684 KB Output is correct
56 Correct 119 ms 4776 KB Output is correct
57 Correct 19 ms 5440 KB Output is correct
58 Correct 20 ms 5412 KB Output is correct
59 Correct 131 ms 5432 KB Output is correct
60 Correct 20 ms 6092 KB Output is correct
61 Correct 26 ms 6212 KB Output is correct
62 Correct 142 ms 6132 KB Output is correct
63 Correct 120 ms 6136 KB Output is correct
64 Correct 172 ms 6092 KB Output is correct
65 Correct 192 ms 6124 KB Output is correct
66 Correct 140 ms 6128 KB Output is correct
67 Correct 139 ms 6116 KB Output is correct
68 Correct 59 ms 6092 KB Output is correct
69 Correct 54 ms 6128 KB Output is correct
70 Correct 52 ms 6176 KB Output is correct
71 Correct 45 ms 6172 KB Output is correct
72 Incorrect 35 ms 6172 KB Output isn't correct
73 Incorrect 54 ms 6604 KB Output isn't correct
74 Correct 63 ms 6604 KB Output is correct
75 Correct 86 ms 6672 KB Output is correct
76 Correct 71 ms 6684 KB Output is correct
77 Correct 66 ms 6668 KB Output is correct
78 Correct 82 ms 6604 KB Output is correct
79 Correct 59 ms 6596 KB Output is correct
80 Correct 68 ms 6616 KB Output is correct
81 Correct 80 ms 6604 KB Output is correct
82 Correct 68 ms 6532 KB Output is correct
83 Correct 90 ms 6552 KB Output is correct
84 Correct 78 ms 6476 KB Output is correct
85 Correct 101 ms 6620 KB Output is correct
86 Correct 82 ms 6476 KB Output is correct
87 Correct 84 ms 6540 KB Output is correct
88 Correct 88 ms 6440 KB Output is correct
89 Correct 89 ms 6348 KB Output is correct
90 Correct 94 ms 6348 KB Output is correct
91 Correct 101 ms 6548 KB Output is correct
92 Correct 89 ms 6436 KB Output is correct