Submission #482514

# Submission time Handle Problem Language Result Execution time Memory
482514 2021-10-25T08:45:07 Z danielsuh Mecho (IOI09_mecho) C++17
100 / 100
230 ms 11644 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

const int mxN = 805;
int N, S; 
char grid[mxN][mxN];
int dist[mxN][mxN];
int mecho_dist[mxN][mxN];
pair<int, int> start, finish;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

bool check(int seconds) {
    for(auto &x : mecho_dist) for(auto &y : x) y = INT_MAX;
    queue<pair<pair<int, int>, int>> q;
    int bees = seconds;
    if(bees < dist[start.first][start.second]) {
        q.push({make_pair(start.first, start.second), 0});
        mecho_dist[start.first][start.second] = 0;
    }
    
    unordered_map<int, bool> considered;
    while(!q.empty()) {
        int x = q.front().first.first, y = q.front().first.second;
        int cur = q.front().second; 
        if(x == finish.first && y == finish.second) return true;
        if(cur % S == 0 && cur != 0 && !considered[cur]) {
            bees++;
            considered[cur] = true;
            continue;
        }
        q.pop();
        if(bees >= dist[x][y]) continue;
        for(int i = 0; i < 4; i++) {
            if(x + dx[i] >= 0 && x + dx[i] < N && y + dy[i] >= 0 && y + dy[i] < N && bees < dist[x + dx[i]][y + dy[i]] 
                    && mecho_dist[x][y] + 1 < mecho_dist[x + dx[i]][y + dy[i]] && (grid[x + dx[i]][y + dy[i]] == 'G' 
                    || grid[x + dx[i]][y + dy[i]] == 'D')) {
                
                mecho_dist[x + dx[i]][y + dy[i]] = mecho_dist[x][y] + 1;
                q.push({make_pair(x + dx[i], y + dy[i]), cur + 1});
            }
        }
    }
    // for(int i = 0; i < N; i++) {
    //     for(int j = 0; j < N; j++) {
    //         if(mecho_dist[i][j] == INT_MAX) {
    //             cout << -1 << " ";
    //         }else {
    //             cout << mecho_dist[i][j] << " ";
    //         }
    //     }
    //     cout << "\n";
    // }
    return false;
}

int bin_search() {
    int lo = 0, hi = 2 * N * N;
    while(lo < hi) {
        int mid = (lo + hi + 1) / 2;
        if(check(mid)) {
            lo = mid;
        }else {
            hi = mid - 1;
        }
    }
    return lo;
}

void preprocess() {
    for(auto &x : dist) for(auto &y : x) y = INT_MAX;
    queue<pair<int, int>> q;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            if(grid[i][j] == 'H') {
                q.push({i, j});
                dist[i][j] = 0;
            }
        }
    }
    while(!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        for(int i = 0; i < 4; i++) {
            if(x + dx[i] >= 0 && x + dx[i] < N && y + dy[i] >= 0 && y + dy[i] < N && dist[x][y] + 1 < dist[x + dx[i]][y + dy[i]]
                    && grid[x + dx[i]][y + dy[i]] == 'G') {
                q.push({x + dx[i], y + dy[i]});
                dist[x + dx[i]][y + dy[i]] = dist[x][y] + 1;
            }
        }
    }
    dist[finish.first][finish.second] = INT_MAX;
}

void solve() {
    cin >> N >> S;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            cin >> grid[i][j];
            if(grid[i][j] == 'M') {
                start.first = i, start.second = j;
                grid[i][j] = 'G';
            }else if(grid[i][j] == 'D') {
                finish.first = i, finish.second = j;
            }
        }
    }
    preprocess();
    int ans;
    ans = bin_search();
    if(!check(0)) {
        ans = -1;
    }
    cout << ans << "\n";
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int T = 1;
    // cin >> T;
    for(int i = 1; i <= T; ++i) {
        solve();
    }
}


# Verdict Execution time Memory Grader output
1 Correct 7 ms 10444 KB Output is correct
2 Correct 7 ms 10444 KB Output is correct
3 Correct 7 ms 10444 KB Output is correct
4 Correct 6 ms 10444 KB Output is correct
5 Correct 7 ms 10444 KB Output is correct
6 Correct 9 ms 10460 KB Output is correct
7 Correct 121 ms 11204 KB Output is correct
8 Correct 9 ms 10444 KB Output is correct
9 Correct 7 ms 10484 KB Output is correct
10 Correct 9 ms 10444 KB Output is correct
11 Correct 7 ms 10444 KB Output is correct
12 Correct 8 ms 10516 KB Output is correct
13 Correct 9 ms 10444 KB Output is correct
14 Correct 9 ms 10444 KB Output is correct
15 Correct 8 ms 10436 KB Output is correct
16 Correct 9 ms 10444 KB Output is correct
17 Correct 7 ms 10444 KB Output is correct
18 Correct 7 ms 10488 KB Output is correct
19 Correct 8 ms 10444 KB Output is correct
20 Correct 8 ms 10444 KB Output is correct
21 Correct 7 ms 10444 KB Output is correct
22 Correct 11 ms 10444 KB Output is correct
23 Correct 9 ms 10480 KB Output is correct
24 Correct 9 ms 10444 KB Output is correct
25 Correct 10 ms 10460 KB Output is correct
26 Correct 12 ms 10520 KB Output is correct
27 Correct 8 ms 10444 KB Output is correct
28 Correct 8 ms 10444 KB Output is correct
29 Correct 12 ms 10420 KB Output is correct
30 Correct 9 ms 10444 KB Output is correct
31 Correct 8 ms 10444 KB Output is correct
32 Correct 9 ms 10444 KB Output is correct
33 Correct 12 ms 10828 KB Output is correct
34 Correct 12 ms 10700 KB Output is correct
35 Correct 40 ms 10740 KB Output is correct
36 Correct 15 ms 10796 KB Output is correct
37 Correct 15 ms 10788 KB Output is correct
38 Correct 51 ms 10800 KB Output is correct
39 Correct 16 ms 10896 KB Output is correct
40 Correct 15 ms 10896 KB Output is correct
41 Correct 54 ms 10828 KB Output is correct
42 Correct 15 ms 10888 KB Output is correct
43 Correct 16 ms 10828 KB Output is correct
44 Correct 65 ms 10952 KB Output is correct
45 Correct 17 ms 10956 KB Output is correct
46 Correct 18 ms 10976 KB Output is correct
47 Correct 90 ms 10956 KB Output is correct
48 Correct 18 ms 11084 KB Output is correct
49 Correct 23 ms 11144 KB Output is correct
50 Correct 97 ms 11008 KB Output is correct
51 Correct 26 ms 10956 KB Output is correct
52 Correct 20 ms 10980 KB Output is correct
53 Correct 120 ms 11056 KB Output is correct
54 Correct 21 ms 11084 KB Output is correct
55 Correct 23 ms 11096 KB Output is correct
56 Correct 142 ms 11092 KB Output is correct
57 Correct 24 ms 11212 KB Output is correct
58 Correct 25 ms 11040 KB Output is correct
59 Correct 167 ms 11136 KB Output is correct
60 Correct 24 ms 11092 KB Output is correct
61 Correct 26 ms 11096 KB Output is correct
62 Correct 196 ms 11108 KB Output is correct
63 Correct 144 ms 11332 KB Output is correct
64 Correct 230 ms 11052 KB Output is correct
65 Correct 181 ms 11096 KB Output is correct
66 Correct 156 ms 11076 KB Output is correct
67 Correct 153 ms 11328 KB Output is correct
68 Correct 63 ms 11148 KB Output is correct
69 Correct 58 ms 11144 KB Output is correct
70 Correct 56 ms 11152 KB Output is correct
71 Correct 52 ms 11192 KB Output is correct
72 Correct 39 ms 11196 KB Output is correct
73 Correct 46 ms 11568 KB Output is correct
74 Correct 87 ms 11644 KB Output is correct
75 Correct 124 ms 11572 KB Output is correct
76 Correct 81 ms 11592 KB Output is correct
77 Correct 90 ms 11588 KB Output is correct
78 Correct 98 ms 11576 KB Output is correct
79 Correct 77 ms 11520 KB Output is correct
80 Correct 74 ms 11580 KB Output is correct
81 Correct 82 ms 11588 KB Output is correct
82 Correct 74 ms 11476 KB Output is correct
83 Correct 108 ms 11396 KB Output is correct
84 Correct 117 ms 11468 KB Output is correct
85 Correct 115 ms 11560 KB Output is correct
86 Correct 112 ms 11444 KB Output is correct
87 Correct 108 ms 11468 KB Output is correct
88 Correct 109 ms 11404 KB Output is correct
89 Correct 133 ms 11340 KB Output is correct
90 Correct 134 ms 11328 KB Output is correct
91 Correct 119 ms 11340 KB Output is correct
92 Correct 112 ms 11396 KB Output is correct