답안 #648700

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
648700 2022-10-07T13:53:19 Z borisAngelov Mecho (IOI09_mecho) C++17
100 / 100
534 ms 8280 KB
#include <iostream>
#include <utility>
#include <vector>
#include <queue>
#include <tuple>

using namespace std;

const int MAXN = 805;

int n, s;
char table[MAXN][MAXN];

vector<pair<int, int>> hives;
int home_x, home_y;
int mecho_x, mecho_y;

bool visited_b[MAXN][MAXN];
bool visited_m[MAXN][MAXN];
int levels_b[MAXN][MAXN];
int levels_m[MAXN][MAXN];

pair<int, int> dirs[4] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};

bool isValid(int x, int y) {
    return x >= 1 && x <= n && y >= 1 && y <= n && (table[x][y] == 'G' || table[x][y] == 'M');
}

bool canReachIt(int mecho_time, int bees_time) {
    return mecho_time / s < bees_time;
}

bool check(int eating_time) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            visited_b[i][j] = visited_m[i][j] = false;
            levels_b[i][j] = levels_m[i][j] = 0;
        }
    }

    queue<pair<int, int>> q;

    for (auto hive : hives) {
        q.push(hive);
        visited_b[hive.first][hive.second] = true;
    }

    while (!q.empty()) {
        int x, y; tie(x, y) = q.front();
        q.pop();

        for (int d = 0; d < 4; d++) {
            int x2 = x + dirs[d].first;
            int y2 = y + dirs[d].second;

            if (!isValid(x2, y2)) continue;
            if (visited_b[x2][y2] == true) continue;

            q.push({x2, y2});
            visited_b[x2][y2] = true;
            levels_b[x2][y2] = 1 + levels_b[x][y];
        }
    }

    q.push({mecho_x, mecho_y});
    visited_m[mecho_x][mecho_y] = true;

    if (levels_b[mecho_x][mecho_y] <= eating_time) {
        return false;
    }

    while (!q.empty()) {
        int x, y; tie(x, y) = q.front();
        q.pop();

        for (int d = 0; d < 4; d++) {
            int x2 = x + dirs[d].first;
            int y2 = y + dirs[d].second;

            if (!isValid(x2, y2)) continue;
            if (visited_m[x2][y2] == true) continue;
            if (!canReachIt(levels_m[x][y] + 1, levels_b[x2][y2] - eating_time)) continue;

            q.push({x2, y2});
            visited_m[x2][y2] = true;
            levels_m[x2][y2] = 1 + levels_m[x][y];
        }
    }

    bool isReached = false;

    for (int d = 0; d < 4; d++) {
        int x = home_x + dirs[d].first;
        int y = home_y + dirs[d].second;

        if (isValid(x, y) && visited_m[x][y] && canReachIt(levels_m[x][y], levels_b[x][y] - eating_time)) {
            isReached = true;
            break;
        }
    }

    return isReached;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> s;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> table[i][j];

            if (table[i][j] == 'H') hives.push_back({i, j});

            if (table[i][j] == 'M') {
                mecho_x = i;
                mecho_y = j;
            }

            if (table[i][j] == 'D') {
                home_x = i;
                home_y = j;
            }
        }
    }

    int l = 0;
    int r = n * n;

    while (l <= r) {
        int mid = (l + r) / 2;

        if (check(mid)) l = mid + 1;
        else r = mid - 1;
    }

    cout << r << endl;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 356 ms 8012 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 880 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 848 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 880 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 2 ms 848 KB Output is correct
29 Correct 2 ms 852 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 2 ms 980 KB Output is correct
32 Correct 2 ms 980 KB Output is correct
33 Correct 27 ms 3412 KB Output is correct
34 Correct 25 ms 3384 KB Output is correct
35 Correct 48 ms 3404 KB Output is correct
36 Correct 34 ms 3924 KB Output is correct
37 Correct 32 ms 3924 KB Output is correct
38 Correct 63 ms 3924 KB Output is correct
39 Correct 42 ms 4432 KB Output is correct
40 Correct 48 ms 4436 KB Output is correct
41 Correct 91 ms 4432 KB Output is correct
42 Correct 56 ms 4820 KB Output is correct
43 Correct 48 ms 4904 KB Output is correct
44 Correct 107 ms 4916 KB Output is correct
45 Correct 69 ms 5324 KB Output is correct
46 Correct 66 ms 5320 KB Output is correct
47 Correct 138 ms 5400 KB Output is correct
48 Correct 81 ms 5844 KB Output is correct
49 Correct 80 ms 5828 KB Output is correct
50 Correct 184 ms 5896 KB Output is correct
51 Correct 122 ms 6424 KB Output is correct
52 Correct 89 ms 6372 KB Output is correct
53 Correct 198 ms 6372 KB Output is correct
54 Correct 140 ms 6876 KB Output is correct
55 Correct 108 ms 6872 KB Output is correct
56 Correct 210 ms 6888 KB Output is correct
57 Correct 140 ms 7380 KB Output is correct
58 Correct 132 ms 7376 KB Output is correct
59 Correct 267 ms 7388 KB Output is correct
60 Correct 177 ms 7888 KB Output is correct
61 Correct 150 ms 7832 KB Output is correct
62 Correct 342 ms 7900 KB Output is correct
63 Correct 426 ms 7780 KB Output is correct
64 Correct 534 ms 8012 KB Output is correct
65 Correct 441 ms 7900 KB Output is correct
66 Correct 415 ms 7900 KB Output is correct
67 Correct 413 ms 7892 KB Output is correct
68 Correct 399 ms 7916 KB Output is correct
69 Correct 400 ms 8012 KB Output is correct
70 Correct 387 ms 7916 KB Output is correct
71 Correct 410 ms 7924 KB Output is correct
72 Correct 389 ms 7916 KB Output is correct
73 Correct 328 ms 8268 KB Output is correct
74 Correct 340 ms 8196 KB Output is correct
75 Correct 397 ms 8136 KB Output is correct
76 Correct 369 ms 8192 KB Output is correct
77 Correct 384 ms 8192 KB Output is correct
78 Correct 393 ms 8280 KB Output is correct
79 Correct 369 ms 8152 KB Output is correct
80 Correct 354 ms 8156 KB Output is correct
81 Correct 377 ms 8152 KB Output is correct
82 Correct 382 ms 8152 KB Output is correct
83 Correct 411 ms 8260 KB Output is correct
84 Correct 385 ms 8120 KB Output is correct
85 Correct 385 ms 8148 KB Output is correct
86 Correct 395 ms 8124 KB Output is correct
87 Correct 411 ms 8140 KB Output is correct
88 Correct 363 ms 8064 KB Output is correct
89 Correct 392 ms 8096 KB Output is correct
90 Correct 352 ms 8060 KB Output is correct
91 Correct 385 ms 8060 KB Output is correct
92 Correct 400 ms 8056 KB Output is correct