Submission #482513

# Submission time Handle Problem Language Result Execution time Memory
482513 2021-10-25T08:40:07 Z danielsuh Mecho (IOI09_mecho) C++17
54 / 100
256 ms 12260 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();
        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 Incorrect 6 ms 10444 KB Output isn't correct
2 Incorrect 6 ms 10444 KB Output isn't correct
3 Incorrect 6 ms 10444 KB Output isn't correct
4 Incorrect 6 ms 10444 KB Output isn't correct
5 Correct 6 ms 10444 KB Output is correct
6 Correct 7 ms 10480 KB Output is correct
7 Correct 114 ms 11860 KB Output is correct
8 Incorrect 8 ms 10444 KB Output isn't correct
9 Correct 7 ms 10444 KB Output is correct
10 Correct 6 ms 10444 KB Output is correct
11 Correct 8 ms 10444 KB Output is correct
12 Correct 8 ms 10520 KB Output is correct
13 Correct 8 ms 10444 KB Output is correct
14 Correct 8 ms 10424 KB Output is correct
15 Incorrect 8 ms 10444 KB Output isn't correct
16 Correct 7 ms 10460 KB Output is correct
17 Incorrect 7 ms 10444 KB Output isn't correct
18 Correct 7 ms 10444 KB Output is correct
19 Incorrect 8 ms 10484 KB Output isn't correct
20 Correct 7 ms 10444 KB Output is correct
21 Incorrect 7 ms 10444 KB Output isn't correct
22 Correct 9 ms 10444 KB Output is correct
23 Incorrect 7 ms 10444 KB Output isn't correct
24 Correct 9 ms 10444 KB Output is correct
25 Incorrect 10 ms 10444 KB Output isn't correct
26 Correct 9 ms 10444 KB Output is correct
27 Incorrect 9 ms 10444 KB Output isn't correct
28 Correct 9 ms 10412 KB Output is correct
29 Incorrect 10 ms 10572 KB Output isn't correct
30 Correct 10 ms 10460 KB Output is correct
31 Incorrect 9 ms 10444 KB Output isn't correct
32 Correct 8 ms 10444 KB Output is correct
33 Incorrect 12 ms 10700 KB Output isn't correct
34 Correct 12 ms 10748 KB Output is correct
35 Correct 30 ms 10752 KB Output is correct
36 Incorrect 12 ms 10780 KB Output isn't correct
37 Correct 13 ms 10676 KB Output is correct
38 Correct 38 ms 10792 KB Output is correct
39 Incorrect 14 ms 10828 KB Output isn't correct
40 Correct 14 ms 10828 KB Output is correct
41 Correct 54 ms 10836 KB Output is correct
42 Incorrect 15 ms 10828 KB Output isn't correct
43 Correct 16 ms 10864 KB Output is correct
44 Correct 64 ms 10868 KB Output is correct
45 Incorrect 17 ms 10828 KB Output isn't correct
46 Correct 17 ms 10828 KB Output is correct
47 Correct 72 ms 10912 KB Output is correct
48 Incorrect 18 ms 10972 KB Output isn't correct
49 Correct 19 ms 10940 KB Output is correct
50 Correct 87 ms 10888 KB Output is correct
51 Incorrect 21 ms 10956 KB Output isn't correct
52 Correct 20 ms 10956 KB Output is correct
53 Correct 108 ms 11076 KB Output is correct
54 Incorrect 20 ms 10956 KB Output isn't correct
55 Correct 20 ms 10956 KB Output is correct
56 Correct 124 ms 10912 KB Output is correct
57 Incorrect 22 ms 10984 KB Output isn't correct
58 Correct 23 ms 10984 KB Output is correct
59 Correct 148 ms 10984 KB Output is correct
60 Incorrect 23 ms 11084 KB Output isn't correct
61 Correct 23 ms 11024 KB Output is correct
62 Correct 184 ms 11204 KB Output is correct
63 Correct 124 ms 11076 KB Output is correct
64 Correct 256 ms 11076 KB Output is correct
65 Correct 170 ms 11136 KB Output is correct
66 Incorrect 145 ms 11016 KB Output isn't correct
67 Correct 137 ms 11108 KB Output is correct
68 Correct 60 ms 11084 KB Output is correct
69 Correct 57 ms 11144 KB Output is correct
70 Correct 54 ms 11072 KB Output is correct
71 Correct 52 ms 11060 KB Output is correct
72 Correct 39 ms 11100 KB Output is correct
73 Correct 45 ms 12204 KB Output is correct
74 Correct 92 ms 12168 KB Output is correct
75 Correct 84 ms 12260 KB Output is correct
76 Correct 72 ms 12228 KB Output is correct
77 Correct 76 ms 12212 KB Output is correct
78 Correct 86 ms 12200 KB Output is correct
79 Correct 65 ms 12088 KB Output is correct
80 Correct 68 ms 12200 KB Output is correct
81 Correct 76 ms 12148 KB Output is correct
82 Correct 68 ms 12092 KB Output is correct
83 Correct 138 ms 12056 KB Output is correct
84 Correct 114 ms 12052 KB Output is correct
85 Correct 92 ms 12100 KB Output is correct
86 Correct 102 ms 12100 KB Output is correct
87 Correct 96 ms 12096 KB Output is correct
88 Correct 132 ms 11948 KB Output is correct
89 Correct 139 ms 12068 KB Output is correct
90 Correct 118 ms 11972 KB Output is correct
91 Correct 113 ms 11972 KB Output is correct
92 Correct 118 ms 12020 KB Output is correct