답안 #654281

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
654281 2022-10-30T20:45:22 Z four_specks Mecho (IOI09_mecho) C++17
100 / 100
598 ms 1996 KB
#include <bits/stdc++.h>

using namespace std;

inline namespace
{
} // namespace

void solve()
{
    int n, s;
    cin >> n >> s;

    vector<string> grid(n);
    for (string &row : grid)
        cin >> row;

    vector grass(n, vector<bool>(n, 1));
    vector<array<int, 2>> hives;
    array<int, 2> honey, home;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            char cell = grid[i][j];
            if (cell == 'T')
                grass[i][j] = 0;
            else if (cell == 'M')
                honey = {i, j};
            else if (cell == 'D')
                home = {i, j};
            else if (cell == 'H')
                hives.push_back({i, j});
        }
    }

    auto check = [&](int mid) -> bool
    {
        vector bad(n, vector<bool>(n, 0));
        queue<array<int, 2>> bees;
        for (const array<int, 2> &coord : hives)
            bad[coord[0]][coord[1]] = 1, bees.push(coord);

        auto mv_bees = [&]
        {
            queue<array<int, 2>> bees1;
            while (!bees.empty())
            {
                auto [x, y] = bees.front();
                bees.pop();

                for (auto [dx, dy] : {array{0, -1}, array{0, 1}, array{-1, 0}, array{1, 0}})
                {
                    int x1 = x + dx, y1 = y + dy;
                    if (0 <= x1 && x1 < n && 0 <= y1 && y1 < n && grass[x1][y1] && array{x1, y1} != home && !bad[x1][y1])
                        bad[x1][y1] = 1, bees1.push({x1, y1});
                }
            }

            bees = move(bees1);
        };

        for (int i = 0; i < mid; i++)
            mv_bees();

        vector seen(n, vector<bool>(n, 0));
        queue<array<int, 2>> q;
        if (!bad[honey[0]][honey[1]])
            seen[honey[0]][honey[1]] = 1, q.push(honey);

        auto mv_q = [&]
        {
            {
                queue<array<int, 2>> q1;
                while (!q.empty())
                {
                    auto [x, y] = q.front();
                    q.pop();

                    if (!bad[x][y])
                        q1.push({x, y});
                }

                q = move(q1);
            }
            for (int i = 0; i < s && !q.empty(); i++)
            {
                queue<array<int, 2>> q1;
                while (!q.empty())
                {
                    auto [x, y] = q.front();
                    q.pop();

                    for (auto [dx, dy] : {array{0, -1}, array{0, 1}, array{-1, 0}, array{1, 0}})
                    {
                        int x1 = x + dx, y1 = y + dy;
                        if (0 <= x1 && x1 < n && 0 <= y1 && y1 < n && grass[x1][y1] && !bad[x1][y1] && !seen[x1][y1])
                            seen[x1][y1] = 1, q1.push({x1, y1});
                    }
                }

                q = move(q1);
            }
        };

        while (!q.empty())
        {
            mv_q();
            mv_bees();
        }

        return seen[home[0]][home[1]];
    };

    int lo = -1, hi = n * n;
    while (lo < hi)
    {
        int mid = (lo + hi + 1) / 2;
        if (check(mid))
            lo = mid;
        else
            hi = mid - 1;
    }

    cout << lo << '\n';
}

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

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 547 ms 1640 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 2 ms 320 KB Output is correct
24 Correct 2 ms 316 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 4 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 4 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 97 ms 596 KB Output is correct
34 Correct 64 ms 592 KB Output is correct
35 Correct 54 ms 668 KB Output is correct
36 Correct 123 ms 736 KB Output is correct
37 Correct 87 ms 736 KB Output is correct
38 Correct 73 ms 736 KB Output is correct
39 Correct 164 ms 808 KB Output is correct
40 Correct 111 ms 724 KB Output is correct
41 Correct 103 ms 820 KB Output is correct
42 Correct 208 ms 884 KB Output is correct
43 Correct 142 ms 876 KB Output is correct
44 Correct 120 ms 884 KB Output is correct
45 Correct 254 ms 992 KB Output is correct
46 Correct 169 ms 988 KB Output is correct
47 Correct 144 ms 996 KB Output is correct
48 Correct 287 ms 1168 KB Output is correct
49 Correct 205 ms 1168 KB Output is correct
50 Correct 165 ms 1172 KB Output is correct
51 Correct 353 ms 1228 KB Output is correct
52 Correct 236 ms 1204 KB Output is correct
53 Correct 202 ms 1220 KB Output is correct
54 Correct 407 ms 1372 KB Output is correct
55 Correct 285 ms 1372 KB Output is correct
56 Correct 238 ms 1372 KB Output is correct
57 Correct 469 ms 1512 KB Output is correct
58 Correct 320 ms 1624 KB Output is correct
59 Correct 278 ms 1412 KB Output is correct
60 Correct 566 ms 1504 KB Output is correct
61 Correct 366 ms 1612 KB Output is correct
62 Correct 310 ms 1612 KB Output is correct
63 Correct 342 ms 1620 KB Output is correct
64 Correct 354 ms 1568 KB Output is correct
65 Correct 331 ms 1620 KB Output is correct
66 Correct 353 ms 1612 KB Output is correct
67 Correct 341 ms 1508 KB Output is correct
68 Correct 342 ms 1628 KB Output is correct
69 Correct 298 ms 1536 KB Output is correct
70 Correct 314 ms 1748 KB Output is correct
71 Correct 341 ms 1612 KB Output is correct
72 Correct 381 ms 1624 KB Output is correct
73 Correct 469 ms 1868 KB Output is correct
74 Correct 569 ms 1960 KB Output is correct
75 Correct 598 ms 1868 KB Output is correct
76 Correct 571 ms 1748 KB Output is correct
77 Correct 579 ms 1832 KB Output is correct
78 Correct 569 ms 1960 KB Output is correct
79 Correct 563 ms 1796 KB Output is correct
80 Correct 575 ms 1748 KB Output is correct
81 Correct 569 ms 1996 KB Output is correct
82 Correct 568 ms 1748 KB Output is correct
83 Correct 551 ms 1748 KB Output is correct
84 Correct 588 ms 1864 KB Output is correct
85 Correct 581 ms 1748 KB Output is correct
86 Correct 551 ms 1752 KB Output is correct
87 Correct 551 ms 1864 KB Output is correct
88 Correct 545 ms 1868 KB Output is correct
89 Correct 547 ms 1812 KB Output is correct
90 Correct 542 ms 1720 KB Output is correct
91 Correct 538 ms 1688 KB Output is correct
92 Correct 574 ms 1692 KB Output is correct