답안 #965424

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965424 2024-04-18T13:55:54 Z Alihan_8 Mecho (IOI09_mecho) C++17
59 / 100
185 ms 12016 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const int inf = 1e9;

int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k; cin >> n >> k;
    vector <string> a(n);
    for ( auto &u: a ) cin >> u;
    ar <int,2> s, e;
    vector <vector<int>> bee(n, vector <int> (n, inf));
    queue <ar<int,2>> q;
    for ( int i = 0; i < n; i++ ){
        for ( int j = 0; j < n; j++ ){
            if ( a[i][j] == 'M' ){
                s = {i, j};
            }
            if ( a[i][j] == 'D' ){
                e = {i, j};
            }
            if ( a[i][j] == 'H' ){
                q.push({i, j});
                bee[i][j] = 0;
            }
        }
    }
    auto ok = [&](int u, int v){
        return u >= 0 && v >= 0 && u < n && v < n;
    };
    while ( !q.empty() ){
        auto [u, v] = q.front();
        q.pop();
        for ( int i = 0; i < 4; i++ ){
            int x = dx[i] + u, y = dy[i] + v;
            if ( ok(x, y) && a[x][y] != 'T' && a[x][y] != 'D' ){
                if ( chmin(bee[x][y], bee[u][v] + 1) ){
                    q.push({x, y});
                }
            }
        }
    }
    auto valid = [&](int t){
        if ( bee[s[0]][s[1]] <= t ){
            return false;
        }
        vector <vector<int>> dp(n, vector <int> (n, inf));
        dp[s[0]][s[1]] = t + 1;
        queue <ar<int,3>> q;
        q.push({s[0], s[1], k});
        while ( !q.empty() ){
            auto [u, v, r] = q.front();
            q.pop();
            for ( int i = 0; i < 4; i++ ){
                int x = dx[i] + u, y = dy[i] + v;
                if ( !ok(x, y) || a[x][y] == 'T' ){
                    continue;
                }
                if ( r == 0 ){
                    if ( chmin(dp[x][y], dp[u][v] + 1) ){
                        q.push({x, y, k});
                    }
                } else if ( r == 1 ){
                    if ( bee[x][y] > dp[u][v] ){
                        if ( chmin(dp[x][y], dp[u][v]) ){
                            q.push({x, y, r - 1});
                        }
                    }
                } else{
                    if ( bee[x][y] >= dp[u][v] ){
                        if ( chmin(dp[x][y], dp[u][v]) ){
                            q.push({x, y, r - 1});
                        }
                    }
                }
            }
        }
        for ( int i = 0; i < n; i++ ){
            for ( int j = 0; j < n; j++ ){

            }
        }
        return dp[e[0]][e[1]] != inf;
    };
    int l = 0, r = inf;
    while ( l + 1 < r ){
        int md = (l + r) >> 1;
        if ( valid(md) ){
            l = md;
        } else r = md;
    }
    cout << (valid(l) ? l : -1);

    cout << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Correct 1 ms 456 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Incorrect 114 ms 11344 KB Output isn't correct
8 Incorrect 1 ms 344 KB Output isn't correct
9 Correct 0 ms 348 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Incorrect 1 ms 504 KB Output isn't correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 452 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 6 ms 2660 KB Output is correct
34 Correct 6 ms 2632 KB Output is correct
35 Correct 26 ms 2644 KB Output is correct
36 Correct 7 ms 3256 KB Output is correct
37 Correct 6 ms 3236 KB Output is correct
38 Correct 31 ms 3276 KB Output is correct
39 Correct 11 ms 4048 KB Output is correct
40 Correct 9 ms 3992 KB Output is correct
41 Correct 43 ms 4060 KB Output is correct
42 Correct 9 ms 4876 KB Output is correct
43 Correct 8 ms 4880 KB Output is correct
44 Correct 54 ms 4872 KB Output is correct
45 Correct 14 ms 5772 KB Output is correct
46 Correct 14 ms 5784 KB Output is correct
47 Correct 67 ms 5800 KB Output is correct
48 Correct 19 ms 6856 KB Output is correct
49 Correct 17 ms 6780 KB Output is correct
50 Correct 100 ms 6752 KB Output is correct
51 Correct 21 ms 7896 KB Output is correct
52 Correct 22 ms 7900 KB Output is correct
53 Correct 94 ms 7944 KB Output is correct
54 Correct 29 ms 9020 KB Output is correct
55 Correct 28 ms 9096 KB Output is correct
56 Correct 146 ms 9060 KB Output is correct
57 Correct 25 ms 10320 KB Output is correct
58 Correct 23 ms 10364 KB Output is correct
59 Correct 130 ms 10292 KB Output is correct
60 Correct 29 ms 11716 KB Output is correct
61 Correct 30 ms 11676 KB Output is correct
62 Correct 169 ms 11720 KB Output is correct
63 Incorrect 164 ms 11632 KB Output isn't correct
64 Correct 185 ms 11612 KB Output is correct
65 Incorrect 180 ms 11864 KB Output isn't correct
66 Incorrect 157 ms 11612 KB Output isn't correct
67 Incorrect 175 ms 11660 KB Output isn't correct
68 Incorrect 61 ms 12016 KB Output isn't correct
69 Correct 55 ms 11600 KB Output is correct
70 Incorrect 44 ms 11716 KB Output isn't correct
71 Incorrect 45 ms 11712 KB Output isn't correct
72 Correct 30 ms 11536 KB Output is correct
73 Correct 37 ms 11680 KB Output is correct
74 Correct 75 ms 11832 KB Output is correct
75 Incorrect 85 ms 11656 KB Output isn't correct
76 Incorrect 83 ms 11672 KB Output isn't correct
77 Incorrect 80 ms 11712 KB Output isn't correct
78 Incorrect 95 ms 11636 KB Output isn't correct
79 Correct 83 ms 11720 KB Output is correct
80 Incorrect 79 ms 11692 KB Output isn't correct
81 Incorrect 101 ms 11836 KB Output isn't correct
82 Incorrect 78 ms 11668 KB Output isn't correct
83 Incorrect 90 ms 11704 KB Output isn't correct
84 Correct 88 ms 11532 KB Output is correct
85 Incorrect 107 ms 11704 KB Output isn't correct
86 Incorrect 94 ms 11680 KB Output isn't correct
87 Correct 95 ms 11732 KB Output is correct
88 Incorrect 108 ms 11684 KB Output isn't correct
89 Correct 147 ms 11872 KB Output is correct
90 Correct 107 ms 11724 KB Output is correct
91 Correct 105 ms 11680 KB Output is correct
92 Incorrect 105 ms 11664 KB Output isn't correct