답안 #907247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
907247 2024-01-15T10:13:02 Z Ghetto Mecho (IOI09_mecho) C++17
84 / 100
265 ms 8308 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 800 + 5, INF = 1e9, MAX_T = MAX_N * MAX_N;

int n, s;
char arr[MAX_N][MAX_N];

bool bee_seen[MAX_N][MAX_N];
int bee_dist[MAX_N][MAX_N]; // INF if unreachable
queue<pii> bee_order;
void bee_bfs() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            if (arr[i][j] == 'H') {
                bee_seen[i][j] = true;
                bee_order.push({i, j});
            } else
                bee_dist[i][j] = INF;
    }
        
    while (bee_order.size()) {
        int i = bee_order.front().first, j = bee_order.front().second;
        bee_order.pop();

        vector<pii> delta = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for (pii in : delta) {
            int new_i = i + in.first, new_j = j + in.second;
            if (new_i < 1 || new_i > n || new_j < 1 || new_j > n) continue;
            if (arr[new_i][new_j] != 'G') continue;
            if (bee_seen[new_i][new_j]) continue;

            bee_seen[new_i][new_j] = true;
            bee_dist[new_i][new_j] = bee_dist[i][j] + 1;
            bee_order.push({new_i, new_j});
        }
    }
}

bool check_seen[MAX_N][MAX_N];
int check_dist[MAX_N][MAX_N]; // INF if unreachable
queue<pii> check_order;
bool check(int t) {
    pii finish = {0, 0};
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (arr[i][j] == 'M') {
                check_seen[i][j] = true;
                check_dist[i][j] = 0;
                check_order.push({i, j});
            } else {
                check_seen[i][j] = false;
                check_dist[i][j] = INF;
            }
            if (arr[i][j] == 'D')
                finish = {i, j};
        }
    }
    
    while (check_order.size()) {
        int i = check_order.front().first, j = check_order.front().second;
        check_order.pop();

        // cout << t << ": " << i << " " << j << endl;

        vector<pii> delta = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for (pii in : delta) {
            int new_i = i + in.first, new_j = j + in.second;
            if (new_i < 1 || new_i > n || new_j < 1 || new_j > n) continue;
            if (arr[new_i][new_j] == 'T') continue;
            if (check_seen[new_i][new_j]) continue;

            int new_dist = check_dist[i][j] + 1;
            if (bee_dist[new_i][new_j] < t + (new_dist / s)) continue;
            check_dist[new_i][new_j] = new_dist;

            check_seen[new_i][new_j] = true;
            check_order.push({new_i, new_j}); 
        }
    }

    return check_seen[finish.first][finish.second];
}
int bin_search() {
    int lo = 1, hi = MAX_T;
    while (lo != hi) {
        int mid = ceil((lo + hi) / (double) 2);
        if (check(mid)) lo = mid;
        else hi = mid - 1;
    }
    if (check(lo)) return lo;
    else return -1;
}

int main() {
    // freopen("mecho.in", "r", stdin);

    cin >> n >> s;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> arr[i][j];
    
    bee_bfs();

    int ans = bin_search();
    if (ans != -1) ans--;
    cout << ans << '\n';

    // for (int i = 1; i <= n; i++) {
    //     for (int j = 1; j <= n; j++)
    //         cout << bee_dist[i][j] << " ";
    //     cout << endl;
    // }
        
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4556 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 161 ms 8132 KB Output is correct
8 Correct 2 ms 4440 KB Output is correct
9 Correct 1 ms 4696 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Incorrect 2 ms 6744 KB Output isn't correct
13 Incorrect 2 ms 4700 KB Output isn't correct
14 Correct 2 ms 6808 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 1 ms 4552 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 2 ms 4696 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 2 ms 4700 KB Output is correct
25 Correct 2 ms 6744 KB Output is correct
26 Correct 2 ms 6744 KB Output is correct
27 Correct 2 ms 6748 KB Output is correct
28 Correct 2 ms 6748 KB Output is correct
29 Correct 2 ms 6748 KB Output is correct
30 Correct 2 ms 6748 KB Output is correct
31 Correct 2 ms 6748 KB Output is correct
32 Correct 2 ms 6864 KB Output is correct
33 Correct 12 ms 7000 KB Output is correct
34 Correct 12 ms 7000 KB Output is correct
35 Correct 41 ms 7004 KB Output is correct
36 Correct 17 ms 7000 KB Output is correct
37 Correct 17 ms 7260 KB Output is correct
38 Correct 52 ms 7004 KB Output is correct
39 Correct 20 ms 7260 KB Output is correct
40 Correct 20 ms 7332 KB Output is correct
41 Correct 65 ms 7288 KB Output is correct
42 Correct 22 ms 7256 KB Output is correct
43 Correct 22 ms 7260 KB Output is correct
44 Correct 82 ms 7660 KB Output is correct
45 Correct 27 ms 7260 KB Output is correct
46 Correct 29 ms 7508 KB Output is correct
47 Correct 102 ms 7496 KB Output is correct
48 Correct 30 ms 7516 KB Output is correct
49 Correct 30 ms 7432 KB Output is correct
50 Correct 115 ms 7516 KB Output is correct
51 Correct 38 ms 7504 KB Output is correct
52 Correct 40 ms 7500 KB Output is correct
53 Correct 143 ms 7524 KB Output is correct
54 Correct 44 ms 7796 KB Output is correct
55 Correct 41 ms 7628 KB Output is correct
56 Correct 167 ms 7800 KB Output is correct
57 Correct 48 ms 7904 KB Output is correct
58 Correct 50 ms 7908 KB Output is correct
59 Correct 203 ms 7728 KB Output is correct
60 Correct 55 ms 8020 KB Output is correct
61 Correct 61 ms 8016 KB Output is correct
62 Correct 232 ms 8276 KB Output is correct
63 Correct 155 ms 8020 KB Output is correct
64 Correct 265 ms 8020 KB Output is correct
65 Correct 226 ms 8016 KB Output is correct
66 Correct 173 ms 8024 KB Output is correct
67 Correct 188 ms 8092 KB Output is correct
68 Correct 92 ms 7872 KB Output is correct
69 Correct 80 ms 8052 KB Output is correct
70 Correct 81 ms 7872 KB Output is correct
71 Correct 81 ms 8068 KB Output is correct
72 Incorrect 67 ms 8020 KB Output isn't correct
73 Incorrect 130 ms 8304 KB Output isn't correct
74 Correct 112 ms 8308 KB Output is correct
75 Correct 128 ms 8304 KB Output is correct
76 Correct 125 ms 8272 KB Output is correct
77 Correct 131 ms 8304 KB Output is correct
78 Correct 151 ms 8280 KB Output is correct
79 Correct 104 ms 8276 KB Output is correct
80 Correct 119 ms 8180 KB Output is correct
81 Correct 145 ms 8272 KB Output is correct
82 Correct 128 ms 8272 KB Output is correct
83 Correct 151 ms 8184 KB Output is correct
84 Correct 150 ms 8016 KB Output is correct
85 Correct 131 ms 8068 KB Output is correct
86 Correct 148 ms 8232 KB Output is correct
87 Correct 147 ms 8020 KB Output is correct
88 Correct 149 ms 8020 KB Output is correct
89 Correct 160 ms 8004 KB Output is correct
90 Correct 185 ms 8180 KB Output is correct
91 Correct 148 ms 8076 KB Output is correct
92 Correct 169 ms 8180 KB Output is correct