Submission #491454

# Submission time Handle Problem Language Result Execution time Memory
491454 2021-12-02T14:03:16 Z XII Mecho (IOI09_mecho) C++17
84 / 100
170 ms 10948 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "IOI09_mecho"
void Fi(){
    if(fopen(PROB".inp", "r")){
        freopen(PROB".inp", "r", stdin);
        freopen(PROB".out", "w", stdout);
    }
}

const int INF = 1e9;
const int N = 800 + 1;
int n, spd;
int a[N][N];
using pi = pair<int, int>;
pi S, E;
queue<pi> qe;

int d[N][N];
const int dx[] = {0, 0, -1, +1};
const int dy[] = {-1, +1, 0, 0};

void bfsBee(){
    while(!qe.empty()){
        auto [x, y] = qe.front(); qe.pop();
        FOR(direc, 0, 4){
            int i = x + dx[direc];
            int j = y + dy[direc];
            if(i < 1 || n < i || j < 1 || n < j) continue;
            if(a[i][j] == 1 && d[i][j] == -1){
                d[i][j] = d[x][y] + 1;
                qe.emplace(i, j);
            }
        }
    }
}

pi D[N][N];
bool bfsMecho(const int &T){
    FORU(i, 1, n) FORU(j, 1, n) D[i][j] = {INF, INF};
    qe.emplace(S); D[S.fi][S.se] = {T, 0};
    while(!qe.empty()){
        auto [x, y] = qe.front(); qe.pop();
//        cout << x << " " << y << " " << D[x][y].fi << " " << D[x][y].se << "\n";
        FOR(direc, 0, 4){
            int i = x + dx[direc];
            int j = y + dy[direc];
            if(i < 1 || n < i || j < 1 || n < j) continue;
            if(a[i][j] && D[i][j].fi == INF){
                D[i][j] = D[x][y];
                if(++D[i][j].se >= spd){
                    ++D[i][j].fi;
                    D[i][j].se = 0;
                }
                if(a[i][j] == 2){
                    while(!qe.empty()) qe.pop();
                    return true;
                }
                if(D[i][j].fi < d[i][j]){
                    qe.emplace(i, j);
                }
            }
        }
    }
    return false;
}

int main(){
    IOS;
    Fi();
    cin >> n >> spd;
    memset(d, -1, sizeof(d));
    FORU(i, 1, n){
        string s; cin >> s;
        FORU(j, 1, n){
            switch(s[j - 1]){
                case 'T': a[i][j] = 0; break;
                case 'G': a[i][j] = 1; break;
                case 'M': a[i][j] = 1; S = {i, j}; break;
                case 'D': a[i][j] = 2; E = {i, j}; break;
                case 'H': a[i][j] = 0; qe.emplace(i, j); d[i][j] = 0; break;
            }
        }
    }
    bfsBee();
    int l = 0, r = d[S.fi][S.se], ans = -1;
    while(l <= r){
        int m = (l + r) / 2;
        if(bfsMecho(m)) l = m + 1, ans = m;
        else r = m - 1;
    }
    cout << ans;
    return 0;
}

Compilation message

mecho.cpp: In function 'void Fi()':
mecho.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen(PROB".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(PROB".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 1 ms 2764 KB Output is correct
3 Correct 1 ms 2764 KB Output is correct
4 Correct 1 ms 2764 KB Output is correct
5 Correct 1 ms 2892 KB Output is correct
6 Correct 2 ms 2892 KB Output is correct
7 Correct 97 ms 10692 KB Output is correct
8 Correct 1 ms 2892 KB Output is correct
9 Correct 1 ms 2892 KB Output is correct
10 Correct 2 ms 2884 KB Output is correct
11 Correct 1 ms 2892 KB Output is correct
12 Incorrect 2 ms 3276 KB Output isn't correct
13 Incorrect 2 ms 3148 KB Output isn't correct
14 Correct 2 ms 3276 KB Output is correct
15 Correct 1 ms 2892 KB Output is correct
16 Correct 2 ms 2892 KB Output is correct
17 Correct 1 ms 3020 KB Output is correct
18 Correct 1 ms 3020 KB Output is correct
19 Correct 1 ms 3020 KB Output is correct
20 Correct 1 ms 3020 KB Output is correct
21 Correct 1 ms 3148 KB Output is correct
22 Correct 2 ms 3148 KB Output is correct
23 Correct 1 ms 3148 KB Output is correct
24 Correct 2 ms 3148 KB Output is correct
25 Correct 2 ms 3276 KB Output is correct
26 Correct 2 ms 3276 KB Output is correct
27 Correct 2 ms 3276 KB Output is correct
28 Correct 2 ms 3276 KB Output is correct
29 Correct 2 ms 3276 KB Output is correct
30 Correct 2 ms 3276 KB Output is correct
31 Correct 2 ms 3404 KB Output is correct
32 Correct 2 ms 3404 KB Output is correct
33 Correct 6 ms 6224 KB Output is correct
34 Correct 6 ms 6220 KB Output is correct
35 Correct 17 ms 6220 KB Output is correct
36 Correct 8 ms 6732 KB Output is correct
37 Correct 9 ms 6724 KB Output is correct
38 Correct 23 ms 6732 KB Output is correct
39 Correct 9 ms 7244 KB Output is correct
40 Correct 9 ms 7196 KB Output is correct
41 Correct 33 ms 7252 KB Output is correct
42 Correct 10 ms 7756 KB Output is correct
43 Correct 10 ms 7756 KB Output is correct
44 Correct 41 ms 7748 KB Output is correct
45 Correct 12 ms 8140 KB Output is correct
46 Correct 12 ms 8140 KB Output is correct
47 Correct 46 ms 8240 KB Output is correct
48 Correct 13 ms 8712 KB Output is correct
49 Correct 14 ms 8652 KB Output is correct
50 Correct 54 ms 8652 KB Output is correct
51 Correct 16 ms 9164 KB Output is correct
52 Correct 16 ms 9164 KB Output is correct
53 Correct 71 ms 9180 KB Output is correct
54 Correct 18 ms 9548 KB Output is correct
55 Correct 18 ms 9672 KB Output is correct
56 Correct 81 ms 9652 KB Output is correct
57 Correct 20 ms 10064 KB Output is correct
58 Correct 20 ms 10024 KB Output is correct
59 Correct 97 ms 10128 KB Output is correct
60 Correct 22 ms 10572 KB Output is correct
61 Correct 22 ms 10572 KB Output is correct
62 Correct 127 ms 10584 KB Output is correct
63 Correct 106 ms 10564 KB Output is correct
64 Correct 170 ms 10688 KB Output is correct
65 Correct 170 ms 10564 KB Output is correct
66 Correct 126 ms 10692 KB Output is correct
67 Correct 116 ms 10580 KB Output is correct
68 Correct 54 ms 10600 KB Output is correct
69 Correct 47 ms 10592 KB Output is correct
70 Correct 43 ms 10600 KB Output is correct
71 Correct 37 ms 10592 KB Output is correct
72 Incorrect 38 ms 10592 KB Output isn't correct
73 Incorrect 41 ms 10948 KB Output isn't correct
74 Correct 63 ms 10860 KB Output is correct
75 Correct 59 ms 10844 KB Output is correct
76 Correct 63 ms 10860 KB Output is correct
77 Correct 61 ms 10852 KB Output is correct
78 Correct 64 ms 10828 KB Output is correct
79 Correct 51 ms 10824 KB Output is correct
80 Correct 58 ms 10832 KB Output is correct
81 Correct 61 ms 10832 KB Output is correct
82 Correct 57 ms 10824 KB Output is correct
83 Correct 79 ms 10792 KB Output is correct
84 Correct 66 ms 10904 KB Output is correct
85 Correct 68 ms 10792 KB Output is correct
86 Correct 76 ms 10788 KB Output is correct
87 Correct 67 ms 10692 KB Output is correct
88 Correct 87 ms 10828 KB Output is correct
89 Correct 80 ms 10740 KB Output is correct
90 Correct 85 ms 10792 KB Output is correct
91 Correct 105 ms 10744 KB Output is correct
92 Correct 81 ms 10700 KB Output is correct