답안 #670054

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670054 2022-12-08T01:11:22 Z magmamite135 Mecho (IOI09_mecho) C++17
100 / 100
172 ms 3868 KB
// 9:50 - 11:06
#include <iostream>
#include <queue>
using namespace std;
using pii = pair<int, int>;

int n, s, grid[805][805];
bool vis[805][805];
pii honey, home;
int x[] = {0, -1, 0, 1};
int y[] = {-1, 0, 1, 0};
bool inRange(pii pos, int dir) { return 0 <= pos.first + x[dir] && pos.first + x[dir] < n &&
                                        0 <= pos.second + y[dir] && pos.second + y[dir] < n; }

bool canReachHome(int wait)
{
    if (wait >= grid[honey.first][honey.second])
        return false;
    queue<pair<pii, pii>> BFS;
    BFS.push({{1, s}, honey});
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            vis[i][j] = false;
    while (!BFS.empty())
    {
        int time = BFS.front().first.first;
        int stepsLeft = BFS.front().first.second;
        pii pos = BFS.front().second;
        BFS.pop();
        if ((--stepsLeft) == 0)
            time++, stepsLeft = s;
        for (int i = 0; i < 4; i++)
            if (inRange(pos, i))
            {
                if (vis[pos.first + x[i]][pos.second + y[i]] == false &&
                    grid[pos.first + x[i]][pos.second + y[i]] != -1 &&
                    grid[pos.first + x[i]][pos.second + y[i]] - wait >= time)
                {
                    vis[pos.first + x[i]][pos.second + y[i]] = true;
                    BFS.push({{time, stepsLeft}, {pos.first + x[i], pos.second + y[i]}});
                }
                else if (grid[pos.first + x[i]][pos.second + y[i]] == -2)
                    return true;
            }
    }
    return false;
}

int main(void)
{
    cin >> n >> s;
    queue<pair<pii, int>> hive;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            char ch;
            cin >> ch;
            if (ch == 'T')
                grid[i][j] = -1;
            else if (ch == 'G')
                grid[i][j] = -3;
            else if (ch == 'M')
                grid[i][j] = -3, honey = {i, j};
            else if (ch == 'D')
                grid[i][j] = -2, home = {i, j};
            else // == 'H'
            {
                grid[i][j] = 0;
                hive.push({{i, j}, 0});
            }
        }
    while (!hive.empty())
    {
        pii loc = hive.front().first;
        int infectTime = hive.front().second;
        hive.pop();
        for (int i = 0; i < 4; i++)
            if (inRange(loc, i) && grid[loc.first + x[i]][loc.second + y[i]] == -3)
            {
                grid[loc.first + x[i]][loc.second + y[i]] = infectTime + 1;
                hive.push({{loc.first + x[i], loc.second + y[i]}, infectTime + 1});
            }
    }
    int lo = 0, hi = 1000000;
    while (hi - lo > 1)
        if (int mid = (lo + hi) / 2; canReachHome(mid))
            lo = mid;
        else
            hi = mid;
    if (lo == 0 && !canReachHome(0))
        cout << -1 << endl;
    else
        cout << lo << endl;
}
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 101 ms 3488 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 8 ms 1692 KB Output is correct
34 Correct 8 ms 1620 KB Output is correct
35 Correct 22 ms 1620 KB Output is correct
36 Correct 10 ms 1876 KB Output is correct
37 Correct 10 ms 1876 KB Output is correct
38 Correct 28 ms 1876 KB Output is correct
39 Correct 13 ms 2012 KB Output is correct
40 Correct 13 ms 1984 KB Output is correct
41 Correct 35 ms 2004 KB Output is correct
42 Correct 17 ms 2260 KB Output is correct
43 Correct 17 ms 2236 KB Output is correct
44 Correct 46 ms 2216 KB Output is correct
45 Correct 20 ms 2388 KB Output is correct
46 Correct 19 ms 2388 KB Output is correct
47 Correct 62 ms 2588 KB Output is correct
48 Correct 24 ms 2560 KB Output is correct
49 Correct 22 ms 2636 KB Output is correct
50 Correct 71 ms 2712 KB Output is correct
51 Correct 26 ms 2840 KB Output is correct
52 Correct 26 ms 2848 KB Output is correct
53 Correct 84 ms 2868 KB Output is correct
54 Correct 34 ms 3020 KB Output is correct
55 Correct 31 ms 3012 KB Output is correct
56 Correct 96 ms 2940 KB Output is correct
57 Correct 35 ms 3216 KB Output is correct
58 Correct 35 ms 3236 KB Output is correct
59 Correct 111 ms 3268 KB Output is correct
60 Correct 41 ms 3492 KB Output is correct
61 Correct 40 ms 3380 KB Output is correct
62 Correct 124 ms 3456 KB Output is correct
63 Correct 122 ms 3340 KB Output is correct
64 Correct 162 ms 3436 KB Output is correct
65 Correct 172 ms 3444 KB Output is correct
66 Correct 152 ms 3404 KB Output is correct
67 Correct 131 ms 3432 KB Output is correct
68 Correct 66 ms 3368 KB Output is correct
69 Correct 61 ms 3368 KB Output is correct
70 Correct 59 ms 3364 KB Output is correct
71 Correct 54 ms 3392 KB Output is correct
72 Correct 52 ms 3368 KB Output is correct
73 Correct 53 ms 3788 KB Output is correct
74 Correct 78 ms 3868 KB Output is correct
75 Correct 82 ms 3860 KB Output is correct
76 Correct 76 ms 3736 KB Output is correct
77 Correct 79 ms 3848 KB Output is correct
78 Correct 87 ms 3788 KB Output is correct
79 Correct 70 ms 3752 KB Output is correct
80 Correct 74 ms 3816 KB Output is correct
81 Correct 84 ms 3812 KB Output is correct
82 Correct 76 ms 3696 KB Output is correct
83 Correct 93 ms 3652 KB Output is correct
84 Correct 80 ms 3780 KB Output is correct
85 Correct 86 ms 3824 KB Output is correct
86 Correct 90 ms 3752 KB Output is correct
87 Correct 88 ms 3752 KB Output is correct
88 Correct 91 ms 3572 KB Output is correct
89 Correct 91 ms 3556 KB Output is correct
90 Correct 94 ms 3564 KB Output is correct
91 Correct 94 ms 3676 KB Output is correct
92 Correct 92 ms 3680 KB Output is correct