Submission #406413

# Submission time Handle Problem Language Result Execution time Memory
406413 2021-05-17T14:44:19 Z Falcon Mecho (IOI09_mecho) C++17
84 / 100
242 ms 6960 KB
#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)

#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }

#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }

#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


using ll = long long;

template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, s; cin >> n >> s;
    vector<string> grid(n); rep(i, n) cin >> grid[i];

    vector<vector<int>> bee_dist = [&]{
        vector<vector<int>> dist(n, vector<int>(n, -1));

        queue<pair<int, int>> q;
        rep(i, n) rep(j, n) if(grid[i][j] == 'H')
            q.push({i, j}), dist[i][j] = 0;

        auto add = [&](int x, int y, int d) {
            if(x < 0 || y < 0 || x >= n || y >= n || grid[x][y] != 'G')
                return;
            if(~dist[x][y])
                return;

            q.push({x, y});
            dist[x][y] = d + s; 
        };

        while(!q.empty()) {
            auto [i, j] = q.front(); q.pop();
            add(i + 1, j, dist[i][j]);
            add(i, j + 1, dist[i][j]);
            add(i - 1, j, dist[i][j]);
            add(i, j - 1, dist[i][j]);
        }

        return dist;
    }();

    auto check = [&](int t) {
        t = t * s;
        queue<pair<int, int>> q;
        vector<vector<int>> dist(n, vector<int>(n, -1));
        rep(i, n) rep(j, n) if(grid[i][j] == 'M')
            q.push({i, j}), dist[i][j] = t;

        auto add = [&](int x, int y, int d) {
            if(x < 0 || y < 0 || x >= n || y >= n)
                return;

            if(grid[x][y] != 'G' && grid[x][y] != 'D')
                return;

            if(~dist[x][y])
                return;
            
            if(~bee_dist[x][y] && d + 1 >= bee_dist[x][y])
                return;

            q.push({x, y});
            dist[x][y] = d + 1; 
        };

        while(!q.empty()) {
            auto [i, j] = q.front(); q.pop();
            if(grid[i][j] == 'D')
                return true;

            add(i + 1, j, dist[i][j]);
            add(i - 1, j, dist[i][j]);
            add(i, j + 1, dist[i][j]);
            add(i, j - 1, dist[i][j]);
        }

        return false;
     };

    debug(bee_dist);

    if(!check(0))
        return cout << -1 << '\n', 0;

    int t{};
    for(int k{1 << __lg(n * n)}; k > 0; k >>= 1)
        if(t + k <= n * n && check(t + k))
            t += k;

    cout << t << '\n';
    
    
    #ifdef DEBUG
    dbg::dout << "\nExecution time: "
              << clock() * 1000 / CLOCKS_PER_SEC 
              << "ms" << endl;
    #endif
    return 0;
}


Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:33:29: warning: statement has no effect [-Wunused-value]
   33 | #define debug(x...)         42
      |                             ^~
mecho.cpp:120:5: note: in expansion of macro 'debug'
  120 |     debug(bee_dist);
      |     ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 136 ms 6804 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 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 2 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 312 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 308 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 316 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 316 KB Output is correct
28 Correct 1 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 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 10 ms 1584 KB Output is correct
34 Correct 10 ms 1556 KB Output is correct
35 Correct 29 ms 1628 KB Output is correct
36 Correct 11 ms 1956 KB Output is correct
37 Correct 10 ms 1936 KB Output is correct
38 Correct 38 ms 1940 KB Output is correct
39 Correct 13 ms 2408 KB Output is correct
40 Correct 13 ms 2348 KB Output is correct
41 Correct 51 ms 2412 KB Output is correct
42 Correct 15 ms 2828 KB Output is correct
43 Correct 16 ms 2904 KB Output is correct
44 Correct 61 ms 2836 KB Output is correct
45 Correct 19 ms 3404 KB Output is correct
46 Correct 20 ms 3400 KB Output is correct
47 Correct 76 ms 3388 KB Output is correct
48 Correct 22 ms 4004 KB Output is correct
49 Correct 22 ms 3932 KB Output is correct
50 Correct 95 ms 3988 KB Output is correct
51 Correct 27 ms 4588 KB Output is correct
52 Correct 27 ms 4540 KB Output is correct
53 Correct 114 ms 4576 KB Output is correct
54 Correct 42 ms 5240 KB Output is correct
55 Correct 31 ms 5264 KB Output is correct
56 Correct 131 ms 5316 KB Output is correct
57 Correct 38 ms 5984 KB Output is correct
58 Correct 39 ms 5980 KB Output is correct
59 Correct 152 ms 6008 KB Output is correct
60 Correct 43 ms 6828 KB Output is correct
61 Correct 44 ms 6872 KB Output is correct
62 Correct 180 ms 6752 KB Output is correct
63 Correct 162 ms 6748 KB Output is correct
64 Correct 242 ms 6824 KB Output is correct
65 Correct 238 ms 6784 KB Output is correct
66 Correct 189 ms 6784 KB Output is correct
67 Correct 34 ms 6712 KB Output is correct
68 Correct 82 ms 6732 KB Output is correct
69 Correct 83 ms 6860 KB Output is correct
70 Correct 72 ms 6960 KB Output is correct
71 Correct 70 ms 6736 KB Output is correct
72 Incorrect 36 ms 6708 KB Output isn't correct
73 Incorrect 104 ms 6756 KB Output isn't correct
74 Correct 115 ms 6728 KB Output is correct
75 Correct 112 ms 6728 KB Output is correct
76 Correct 108 ms 6888 KB Output is correct
77 Correct 101 ms 6748 KB Output is correct
78 Correct 31 ms 6592 KB Output is correct
79 Correct 96 ms 6736 KB Output is correct
80 Correct 101 ms 6732 KB Output is correct
81 Correct 128 ms 6744 KB Output is correct
82 Correct 102 ms 6748 KB Output is correct
83 Correct 124 ms 6812 KB Output is correct
84 Correct 122 ms 6740 KB Output is correct
85 Correct 110 ms 6748 KB Output is correct
86 Correct 120 ms 6752 KB Output is correct
87 Correct 140 ms 6828 KB Output is correct
88 Correct 137 ms 6752 KB Output is correct
89 Correct 141 ms 6740 KB Output is correct
90 Correct 164 ms 6808 KB Output is correct
91 Correct 130 ms 6740 KB Output is correct
92 Correct 128 ms 6756 KB Output is correct