Submission #669098

# Submission time Handle Problem Language Result Execution time Memory
669098 2022-12-05T16:24:07 Z finn__ Mecho (IOI09_mecho) C++17
100 / 100
353 ms 2560 KB
#include <bits/stdc++.h>
using namespace std;

struct point
{
    size_t i, j;
};

point M, D;
vector<point> H;

inline bool is_free(char x)
{
    return x == 'G' || x == 'M';
}

void spread_bees(vector<string> &forest, queue<point> &q)
{
    queue<point> y;

    while (!q.empty())
    {
        auto const [i, j] = q.front();
        q.pop();

        if (i && is_free(forest[i - 1][j]))
        {
            forest[i - 1][j] = 'B';
            y.push({i - 1, j});
        }

        if (i < forest.size() - 1 && is_free(forest[i + 1][j]))
        {
            forest[i + 1][j] = 'B';
            y.push({i + 1, j});
        }

        if (j && is_free(forest[i][j - 1]))
        {
            forest[i][j - 1] = 'B';
            y.push({i, j - 1});
        }

        if (j < forest.size() - 1 && is_free(forest[i][j + 1]))
        {
            forest[i][j + 1] = 'B';
            y.push({i, j + 1});
        }
    }

    swap(q, y);
}

bool walk_step(
    vector<string> const &forest, vector<vector<bool>> &vis, queue<point> &p)
{
    queue<point> y;

    while (!p.empty())
    {
        auto const [i, j] = p.front();
        p.pop();

        if (is_free(forest[i][j]))
        {
            if (i && !vis[i - 1][j])
            {
                if (is_free(forest[i - 1][j]))
                {
                    vis[i - 1][j] = 1;
                    y.push({i - 1, j});
                }
                else if (forest[i - 1][j] == 'D')
                    return 1;
            }

            if (i < forest.size() - 1 && !vis[i + 1][j])
            {
                if (is_free(forest[i + 1][j]))
                {
                    vis[i + 1][j] = 1;
                    y.push({i + 1, j});
                }
                else if (forest[i + 1][j] == 'D')
                    return 1;
            }

            if (j && !vis[i][j - 1])
            {
                if (is_free(forest[i][j - 1]))
                {
                    vis[i][j - 1] = 1;
                    y.push({i, j - 1});
                }
                else if (forest[i][j - 1] == 'D')
                    return 1;
            }

            if (j < forest.size() - 1 && !vis[i][j + 1])
            {
                if (is_free(forest[i][j + 1]))
                {
                    vis[i][j + 1] = 1;
                    y.push({i, j + 1});
                }
                else if (forest[i][j + 1] == 'D')
                    return 1;
            }
        }
    }

    swap(p, y);
    return 0;
}

bool can_go_home(size_t n, size_t s, vector<string> forest, unsigned t)
{
    queue<point> p, q;
    p.push(M);
    for (point const &x : H)
        q.push(x);

    vector<vector<bool>> vis(n, vector<bool>(n, 0));
    vis[M.i][M.j] = 1;

    for (unsigned i = 0; i < t; i++)
        spread_bees(forest, q);

    while (!p.empty())
    {
        for (size_t i = 0; i < s; i++)
        {
            if (walk_step(forest, vis, p))
                return 1;
        }
        spread_bees(forest, q);
    }

    return 0;
}

int main()
{
    size_t n, s;
    cin >> n >> s;

    vector<string> forest(n);
    for (size_t i = 0; i < n; i++)
    {
        cin >> forest[i];
        for (size_t j = 0; j < n; j++)
        {
            switch (forest[i][j])
            {
            case 'M':
                M = {i, j};
                break;
            case 'D':
                D = {i, j};
                break;
            case 'H':
                H.push_back({i, j});
                break;
            }
        }
    }

    unsigned a = 0, b = n * n;
    while (a < b)
    {
        unsigned t = (a + b + 1) / 2;

        if (can_go_home(n, s, forest, t))
            a = t;
        else
            b = t - 1;
    }

    if (!a && !can_go_home(n, s, forest, 0))
        cout << -1 << '\n';
    else
        cout << a << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 224 ms 2120 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 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 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 2 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 2 ms 212 KB Output is correct
28 Correct 2 ms 212 KB Output is correct
29 Correct 2 ms 212 KB Output is correct
30 Correct 2 ms 212 KB Output is correct
31 Correct 4 ms 212 KB Output is correct
32 Correct 2 ms 212 KB Output is correct
33 Correct 60 ms 596 KB Output is correct
34 Correct 37 ms 596 KB Output is correct
35 Correct 30 ms 596 KB Output is correct
36 Correct 86 ms 712 KB Output is correct
37 Correct 55 ms 596 KB Output is correct
38 Correct 42 ms 724 KB Output is correct
39 Correct 97 ms 796 KB Output is correct
40 Correct 68 ms 852 KB Output is correct
41 Correct 53 ms 792 KB Output is correct
42 Correct 129 ms 1108 KB Output is correct
43 Correct 84 ms 1108 KB Output is correct
44 Correct 65 ms 1108 KB Output is correct
45 Correct 165 ms 1220 KB Output is correct
46 Correct 108 ms 1220 KB Output is correct
47 Correct 77 ms 1216 KB Output is correct
48 Correct 178 ms 1332 KB Output is correct
49 Correct 123 ms 1336 KB Output is correct
50 Correct 92 ms 1288 KB Output is correct
51 Correct 222 ms 1468 KB Output is correct
52 Correct 144 ms 1364 KB Output is correct
53 Correct 110 ms 1472 KB Output is correct
54 Correct 263 ms 1580 KB Output is correct
55 Correct 170 ms 1576 KB Output is correct
56 Correct 127 ms 1548 KB Output is correct
57 Correct 303 ms 1712 KB Output is correct
58 Correct 196 ms 1716 KB Output is correct
59 Correct 150 ms 1620 KB Output is correct
60 Correct 353 ms 1848 KB Output is correct
61 Correct 229 ms 1852 KB Output is correct
62 Correct 169 ms 1876 KB Output is correct
63 Correct 221 ms 1996 KB Output is correct
64 Correct 257 ms 1928 KB Output is correct
65 Correct 213 ms 1876 KB Output is correct
66 Correct 225 ms 1904 KB Output is correct
67 Correct 224 ms 1876 KB Output is correct
68 Correct 207 ms 1932 KB Output is correct
69 Correct 182 ms 1936 KB Output is correct
70 Correct 207 ms 1940 KB Output is correct
71 Correct 201 ms 1876 KB Output is correct
72 Correct 214 ms 1940 KB Output is correct
73 Correct 193 ms 2548 KB Output is correct
74 Correct 225 ms 2520 KB Output is correct
75 Correct 243 ms 2524 KB Output is correct
76 Correct 244 ms 2516 KB Output is correct
77 Correct 241 ms 2560 KB Output is correct
78 Correct 250 ms 2456 KB Output is correct
79 Correct 237 ms 2388 KB Output is correct
80 Correct 241 ms 2464 KB Output is correct
81 Correct 239 ms 2444 KB Output is correct
82 Correct 235 ms 2440 KB Output is correct
83 Correct 254 ms 2388 KB Output is correct
84 Correct 245 ms 2340 KB Output is correct
85 Correct 249 ms 2336 KB Output is correct
86 Correct 252 ms 2332 KB Output is correct
87 Correct 236 ms 2464 KB Output is correct
88 Correct 238 ms 2236 KB Output is correct
89 Correct 240 ms 2132 KB Output is correct
90 Correct 246 ms 2272 KB Output is correct
91 Correct 226 ms 2284 KB Output is correct
92 Correct 229 ms 2352 KB Output is correct