답안 #482932

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
482932 2021-10-26T20:41:56 Z PacifiedPenguin Mecho (IOI09_mecho) C++17
100 / 100
381 ms 6852 KB
#include <bits/stdc++.h>
using P = std::pair<int, int>;
const int INF = 1e9;

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

int32_t main() {
    std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
    int N, S; std::cin >> N >> S;
    std::vector<std::string> a(N);
    P start, end;

    std::vector<std::vector<int>> dpB(N, std::vector<int>(N, INF));
    std::queue<P> qb;
    for(int i = 0; i < N; i++){
        std::cin >> a[i];
        for(int j = 0; j < N; j++){
            if(a[i][j] == 'M'){
                start = P(i, j);
                a[i][j] = 'G';
            }else if(a[i][j] == 'D'){
                end = P(i, j);
            }else if(a[i][j] == 'H'){
                qb.emplace(i, j);
                dpB[i][j] = 0;
            }
        }
    }

    while(!qb.empty()){
        P tp = qb.front(); qb.pop();
        for(int i = 0; i < 4; i++){
            int nx = tp.first + dx[i];
            int ny = tp.second + dy[i];
            if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
            if(a[nx][ny] == 'G'){
                if(dpB[nx][ny] > dpB[tp.first][tp.second] + S){
                    dpB[nx][ny] = dpB[tp.first][tp.second] + S;
                    qb.emplace(nx, ny);
                }
            }
        }
    }

    std::vector<std::vector<int>> dp = dpB;
    auto check = [&](int delay) -> bool{
        int tickStart = delay * S;
        if(tickStart >= dpB[start.first][start.second]) return false;
        // can only walk through regions wich tick value less
        for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) dp[i][j] = INF;
        std::queue<P> q;
        q.push(start); dp[start.first][start.second] = tickStart;
        while(!q.empty()){
            P tp = q.front(); q.pop();
            if(tp == end) return true;
            for(int i = 0; i < 4; i++){
                int nx = tp.first + dx[i];
                int ny = tp.second + dy[i];
                if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
                if(dpB[nx][ny] <= dp[tp.first][tp.second] + 1) continue;
                if(a[nx][ny] == 'G' || a[nx][ny] == 'D'){
                    if(dp[nx][ny] > dp[tp.first][tp.second] + 1){
                        dp[nx][ny] = dp[tp.first][tp.second] + 1;
                        q.emplace(nx, ny);
                    }
                }
            }
        }
        return false;
    };

    if(!check(0)){
        std::cout << "-1\n";
        return 0;
    }

    int l = -1, h = 1;
    while(check(h)) h *= 2;
    while(l < h){
        int mid = l + (h - l + 1) / 2;
        if(check(mid)){
            l = mid;
        }else{
            h = mid - 1;
        }
    }
    std::cout << l << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 135 ms 6716 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 312 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 5 ms 1484 KB Output is correct
34 Correct 5 ms 1484 KB Output is correct
35 Correct 20 ms 1556 KB Output is correct
36 Correct 9 ms 1868 KB Output is correct
37 Correct 7 ms 1868 KB Output is correct
38 Correct 34 ms 1868 KB Output is correct
39 Correct 8 ms 2348 KB Output is correct
40 Correct 9 ms 2348 KB Output is correct
41 Correct 33 ms 2320 KB Output is correct
42 Correct 10 ms 2836 KB Output is correct
43 Correct 11 ms 2764 KB Output is correct
44 Correct 42 ms 2764 KB Output is correct
45 Correct 12 ms 3276 KB Output is correct
46 Correct 12 ms 3276 KB Output is correct
47 Correct 48 ms 3328 KB Output is correct
48 Correct 14 ms 3916 KB Output is correct
49 Correct 15 ms 3936 KB Output is correct
50 Correct 57 ms 3860 KB Output is correct
51 Correct 16 ms 4428 KB Output is correct
52 Correct 22 ms 4416 KB Output is correct
53 Correct 67 ms 4428 KB Output is correct
54 Correct 20 ms 5196 KB Output is correct
55 Correct 20 ms 5196 KB Output is correct
56 Correct 77 ms 5192 KB Output is correct
57 Correct 21 ms 5876 KB Output is correct
58 Correct 23 ms 5836 KB Output is correct
59 Correct 88 ms 5916 KB Output is correct
60 Correct 25 ms 6604 KB Output is correct
61 Correct 28 ms 6728 KB Output is correct
62 Correct 104 ms 6696 KB Output is correct
63 Correct 72 ms 6592 KB Output is correct
64 Correct 381 ms 6600 KB Output is correct
65 Correct 355 ms 6692 KB Output is correct
66 Correct 266 ms 6596 KB Output is correct
67 Correct 31 ms 6604 KB Output is correct
68 Correct 41 ms 6600 KB Output is correct
69 Correct 76 ms 6752 KB Output is correct
70 Correct 55 ms 6604 KB Output is correct
71 Correct 50 ms 6596 KB Output is correct
72 Correct 38 ms 6604 KB Output is correct
73 Correct 47 ms 6732 KB Output is correct
74 Correct 159 ms 6632 KB Output is correct
75 Correct 125 ms 6740 KB Output is correct
76 Correct 116 ms 6852 KB Output is correct
77 Correct 137 ms 6732 KB Output is correct
78 Correct 29 ms 6728 KB Output is correct
79 Correct 157 ms 6724 KB Output is correct
80 Correct 122 ms 6728 KB Output is correct
81 Correct 126 ms 6848 KB Output is correct
82 Correct 132 ms 6724 KB Output is correct
83 Correct 53 ms 6732 KB Output is correct
84 Correct 165 ms 6672 KB Output is correct
85 Correct 128 ms 6692 KB Output is correct
86 Correct 124 ms 6732 KB Output is correct
87 Correct 148 ms 6724 KB Output is correct
88 Correct 57 ms 6636 KB Output is correct
89 Correct 186 ms 6604 KB Output is correct
90 Correct 127 ms 6720 KB Output is correct
91 Correct 154 ms 6724 KB Output is correct
92 Correct 106 ms 6716 KB Output is correct