답안 #654280

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
654280 2022-10-30T20:44:04 Z four_specks Mecho (IOI09_mecho) C++17
100 / 100
598 ms 2044 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);
            }
        };

        // auto able = [&]() -> bool
        // {
        //     vector<array<int, 2>> roll;

        //     queue q1 = q;
        //     for (int i = 0; i < s && !q1.empty(); i++)
        //     {
        //         queue<array<int, 2>> q2;
        //         while (!q1.empty())
        //         {
        //             auto [x, y] = q1.front();
        //             q1.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, q2.push({x1, y1}), roll.push_back({x, y});
        //             }
        //         }

        //         q1 = move(q2);
        //     }

        //     bool ok = seen[home[0]][home[1]];

        //     for (auto [x, y] : roll)
        //         seen[x][y] = 0;

        //     return ok;
        // };

        while (!q.empty())
        {
            // if (able())
            //     return 1;

            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 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 549 ms 1868 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 320 KB Output is correct
17 Correct 1 ms 324 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 316 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 2 ms 324 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 2 ms 316 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 2 ms 320 KB Output is correct
29 Correct 3 ms 320 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 6 ms 340 KB Output is correct
32 Correct 3 ms 324 KB Output is correct
33 Correct 96 ms 588 KB Output is correct
34 Correct 64 ms 668 KB Output is correct
35 Correct 58 ms 656 KB Output is correct
36 Correct 122 ms 760 KB Output is correct
37 Correct 89 ms 776 KB Output is correct
38 Correct 72 ms 724 KB Output is correct
39 Correct 155 ms 852 KB Output is correct
40 Correct 112 ms 972 KB Output is correct
41 Correct 97 ms 900 KB Output is correct
42 Correct 206 ms 1088 KB Output is correct
43 Correct 137 ms 980 KB Output is correct
44 Correct 125 ms 1008 KB Output is correct
45 Correct 265 ms 1076 KB Output is correct
46 Correct 161 ms 980 KB Output is correct
47 Correct 141 ms 1084 KB Output is correct
48 Correct 291 ms 1200 KB Output is correct
49 Correct 204 ms 1196 KB Output is correct
50 Correct 167 ms 1132 KB Output is correct
51 Correct 367 ms 1284 KB Output is correct
52 Correct 233 ms 1228 KB Output is correct
53 Correct 196 ms 1352 KB Output is correct
54 Correct 423 ms 1276 KB Output is correct
55 Correct 280 ms 1376 KB Output is correct
56 Correct 241 ms 1392 KB Output is correct
57 Correct 485 ms 1400 KB Output is correct
58 Correct 319 ms 1516 KB Output is correct
59 Correct 270 ms 1540 KB Output is correct
60 Correct 584 ms 1604 KB Output is correct
61 Correct 361 ms 1604 KB Output is correct
62 Correct 326 ms 1616 KB Output is correct
63 Correct 359 ms 1612 KB Output is correct
64 Correct 355 ms 1620 KB Output is correct
65 Correct 331 ms 1740 KB Output is correct
66 Correct 353 ms 1620 KB Output is correct
67 Correct 352 ms 1644 KB Output is correct
68 Correct 330 ms 1624 KB Output is correct
69 Correct 306 ms 1740 KB Output is correct
70 Correct 304 ms 1548 KB Output is correct
71 Correct 343 ms 1536 KB Output is correct
72 Correct 363 ms 1740 KB Output is correct
73 Correct 468 ms 1848 KB Output is correct
74 Correct 563 ms 1828 KB Output is correct
75 Correct 598 ms 1872 KB Output is correct
76 Correct 583 ms 1868 KB Output is correct
77 Correct 569 ms 1836 KB Output is correct
78 Correct 551 ms 1884 KB Output is correct
79 Correct 562 ms 1868 KB Output is correct
80 Correct 581 ms 1748 KB Output is correct
81 Correct 565 ms 1796 KB Output is correct
82 Correct 570 ms 2044 KB Output is correct
83 Correct 549 ms 1864 KB Output is correct
84 Correct 557 ms 1868 KB Output is correct
85 Correct 579 ms 1740 KB Output is correct
86 Correct 557 ms 1736 KB Output is correct
87 Correct 546 ms 1756 KB Output is correct
88 Correct 563 ms 1808 KB Output is correct
89 Correct 533 ms 1808 KB Output is correct
90 Correct 556 ms 1728 KB Output is correct
91 Correct 550 ms 1724 KB Output is correct
92 Correct 576 ms 1844 KB Output is correct