Submission #489346

# Submission time Handle Problem Language Result Execution time Memory
489346 2021-11-22T16:56:35 Z Alexandruabcde Mecho (IOI09_mecho) C++14
100 / 100
550 ms 4056 KB
#include <bits/stdc++.h>

using namespace std;

constexpr int NMAX = 805;
typedef pair <int, int> PII;

int N, S;
char init[NMAX][NMAX];
int viz[NMAX][NMAX];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void Read () {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> S;

    for (int i = 1; i <= N; ++ i )
        for (int j = 1; j <= N; ++ j )
            cin >> init[i][j];
}

bool Interior (int x, int y) {
    return 0 < x && x < N+1 && 0 < y && y < N+1;
}

void Expand (queue <PII> &Bees) {
    queue <PII> next;

    while (!Bees.empty()) {
        PII P = Bees.front();
        Bees.pop();

        for (int i = 0; i < 4; ++ i ) {
            PII urm = {P.first + dx[i], P.second + dy[i]};

            if (Interior(urm.first, urm.second) && (init[urm.first][urm.second] == 'G' || init[urm.first][urm.second] == 'M') && viz[urm.first][urm.second] != -1) {
                next.push(urm);
                viz[urm.first][urm.second] = -1;
            }
        }
    }

    Bees = next;
}

bool Check (int wait) {
    queue <PII> Ends, CanExp;
    PII finish;

    for (int i = 1; i <= N; ++ i ) {
        for (int j = 1; j <= N; ++ j ) {
            if (init[i][j] == 'T') viz[i][j] = -1;

            if (init[i][j] == 'G') viz[i][j] = 0;

            if (init[i][j] == 'M') {
                Ends.push({i, j});
                viz[i][j] = 1;
            }

            if (init[i][j] == 'D') {
                finish = {i, j};
                viz[i][j] = 0;
            }

            if (init[i][j] == 'H') {
                CanExp.push({i, j});
                viz[i][j] = -1;
            }
        }
    }

    for (int i = 1; i <= wait; ++ i )
        Expand(CanExp);

    bool finished = 0;
    while (!finished && !Ends.empty()) {
        for (int pas = 1; pas <= S; ++ pas ) {
            queue <PII> nextEnds;
            while (!Ends.empty()) {
                PII P = Ends.front();
                Ends.pop();

                if (viz[P.first][P.second] != 1) continue;

                for (int i = 0; i < 4; ++ i ) {
                    PII urm = {P.first + dx[i], P.second + dy[i]};

                    if (Interior(urm.first, urm.second) && viz[urm.first][urm.second] == 0) {
                        nextEnds.push(urm);
                        viz[urm.first][urm.second] = 1;
                    }
                }
            }

            Ends = nextEnds;
        }

        if (viz[finish.first][finish.second]) finished = 1;
        Expand(CanExp);
    }

    return finished;
}

int aux[NMAX][NMAX];
int FindRight () {
    queue <PII> Q;
    for (int i = 1; i <= N; ++ i )
        for (int j = 1; j <= N; ++ j ) {
            aux[i][j] = 0;

            if (init[i][j] == 'H') {
                Q.push({i, j});
                aux[i][j] = 1;
            }
        }

    while (!Q.empty()) {
        PII x = Q.front();
        Q.pop();

        for (int i = 0; i < 4; ++ i ) {
            PII urm = {x.first + dx[i], x.second + dy[i]};

            if (Interior(urm.first, urm.second) && aux[urm.first][urm.second] == 0){
                aux[urm.first][urm.second] = aux[x.first][x.second]+1;
                Q.push(urm);
            }
        }
    }

    for (int i = 1; i <= N; ++ i )
        for (int j = 1; j <= N; ++ j )
            if (init[i][j] == 'M') return (aux[i][j] == 0 ? N*N : aux[i][j]);
}

void Solve () {
    int st = 0, dr = N*N;
    int ans = -1;

    while (st <= dr) {
        int mij = (st + dr) / 2;

        if (Check(mij)) {
            ans = mij;
            st = mij + 1;
        }
        else dr = mij - 1;
    }

    cout << ans << '\n';
}

int main () {
    Read();
    Solve();

    return 0;
}

Compilation message

mecho.cpp: In function 'int FindRight()':
mecho.cpp:112:17: warning: control reaches end of non-void function [-Wreturn-type]
  112 |     queue <PII> Q;
      |                 ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 339 ms 3676 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 3 ms 460 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 3 ms 588 KB Output is correct
30 Correct 3 ms 588 KB Output is correct
31 Correct 4 ms 628 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 84 ms 1612 KB Output is correct
34 Correct 60 ms 1684 KB Output is correct
35 Correct 44 ms 1696 KB Output is correct
36 Correct 132 ms 1900 KB Output is correct
37 Correct 75 ms 1868 KB Output is correct
38 Correct 68 ms 1892 KB Output is correct
39 Correct 169 ms 2100 KB Output is correct
40 Correct 97 ms 1996 KB Output is correct
41 Correct 74 ms 1996 KB Output is correct
42 Correct 179 ms 2292 KB Output is correct
43 Correct 133 ms 2280 KB Output is correct
44 Correct 89 ms 2284 KB Output is correct
45 Correct 229 ms 2500 KB Output is correct
46 Correct 168 ms 2484 KB Output is correct
47 Correct 116 ms 2488 KB Output is correct
48 Correct 268 ms 2676 KB Output is correct
49 Correct 183 ms 2672 KB Output is correct
50 Correct 128 ms 2688 KB Output is correct
51 Correct 328 ms 2884 KB Output is correct
52 Correct 247 ms 2868 KB Output is correct
53 Correct 152 ms 2892 KB Output is correct
54 Correct 388 ms 3068 KB Output is correct
55 Correct 248 ms 3068 KB Output is correct
56 Correct 190 ms 3080 KB Output is correct
57 Correct 440 ms 3264 KB Output is correct
58 Correct 325 ms 3276 KB Output is correct
59 Correct 200 ms 3276 KB Output is correct
60 Correct 550 ms 3588 KB Output is correct
61 Correct 313 ms 3404 KB Output is correct
62 Correct 234 ms 3396 KB Output is correct
63 Correct 362 ms 3476 KB Output is correct
64 Correct 387 ms 3464 KB Output is correct
65 Correct 345 ms 3460 KB Output is correct
66 Correct 351 ms 3460 KB Output is correct
67 Correct 367 ms 3524 KB Output is correct
68 Correct 388 ms 3516 KB Output is correct
69 Correct 357 ms 3532 KB Output is correct
70 Correct 371 ms 3532 KB Output is correct
71 Correct 439 ms 3532 KB Output is correct
72 Correct 429 ms 3532 KB Output is correct
73 Correct 364 ms 4024 KB Output is correct
74 Correct 346 ms 4012 KB Output is correct
75 Correct 362 ms 4052 KB Output is correct
76 Correct 344 ms 4016 KB Output is correct
77 Correct 348 ms 4020 KB Output is correct
78 Correct 359 ms 4000 KB Output is correct
79 Correct 348 ms 3956 KB Output is correct
80 Correct 400 ms 4056 KB Output is correct
81 Correct 352 ms 3940 KB Output is correct
82 Correct 421 ms 3988 KB Output is correct
83 Correct 374 ms 3880 KB Output is correct
84 Correct 395 ms 3948 KB Output is correct
85 Correct 377 ms 3868 KB Output is correct
86 Correct 370 ms 3944 KB Output is correct
87 Correct 349 ms 3868 KB Output is correct
88 Correct 345 ms 3872 KB Output is correct
89 Correct 349 ms 3788 KB Output is correct
90 Correct 348 ms 3788 KB Output is correct
91 Correct 356 ms 3828 KB Output is correct
92 Correct 403 ms 3788 KB Output is correct