Submission #657888

# Submission time Handle Problem Language Result Execution time Memory
657888 2022-11-11T16:07:51 Z burak_ozzkan Mecho (IOI09_mecho) C++14
100 / 100
269 ms 9552 KB
#include <bits/stdc++.h>
#define nl '\n'
#define int long long
using namespace std;

int n, s;
array<int, 2> starting, ending;

bool bfs(int t, vector< vector<bool> >& vis, vector< vector<char> >& v, vector< vector<int> >& obstacle){
    bool ans = 0;
    queue< array<int, 3> > q;
    q.push({starting[0], starting[1], t*s});
    while(!q.empty()){
        auto top = q.front(); q.pop();
        int i = top[0];
        int j = top[1];
        int currTime = top[2];
        if(i <= -1 || i >= n || j <= -1 || j >= n) continue;
        if(vis[i][j] || v[i][j] == 'T' || obstacle[i][j] <= currTime) continue;
        vis[i][j] = 1;
        if(v[i][j] == 'D') ans = 1;

        q.push({i-1, j, currTime+1});
        q.push({i, j-1, currTime+1});
        q.push({i+1, j, currTime+1});
        q.push({i, j+1, currTime+1});
    }

    return ans;
}

void solve(){
    cin >> n >> s;

    vector< vector<char> > v(n, vector<char>(n));
    queue< array<int, 3> > q; // [0] = i, [1] = j, [2] = time

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> v[i][j];
            if(v[i][j] == 'H') q.push({i, j, 0});
            if(v[i][j] == 'M') starting = {i, j};
            if(v[i][j] == 'D') ending = {i, j};
        }
    }

    vector< vector<int> > obstacle(n, vector<int>(n, 1e9));
    vector< vector<bool> > visB(n, vector<bool>(n, 0));
    while(!q.empty()){
        auto top = q.front(); q.pop();
        int i = top[0];
        int j = top[1];
        int t = top[2];
        if(i <= -1 || i >= n || j <= -1 || j >= n) continue;
        if(visB[i][j] || v[i][j] == 'T' || v[i][j] == 'D') continue;
        visB[i][j] = 1;

        obstacle[i][j] = t*s;
        q.push({i-1, j, t+1});
        q.push({i, j-1, t+1});
        q.push({i+1, j, t+1});
        q.push({i, j+1, t+1});
    }


    int lo = 0;
    int hi = 1e11;
    int ans = INT_MAX;
    while(lo <= hi){
        int mid = lo+(hi-lo)/2;

        vector< vector<bool> > vis(n, vector<bool>(n, 0));
        if(bfs(mid, vis, v, obstacle)){
            ans = mid;
            lo = mid+1;
        } else {
            hi = mid-1;
        }
    }

    cout << (ans == INT_MAX ? -1 : ans) << nl;
}

int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1; /*cin >> t;*/ while(t--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 260 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 198 ms 7372 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 8 ms 1492 KB Output is correct
34 Correct 8 ms 1456 KB Output is correct
35 Correct 46 ms 1512 KB Output is correct
36 Correct 13 ms 1800 KB Output is correct
37 Correct 15 ms 1748 KB Output is correct
38 Correct 57 ms 1876 KB Output is correct
39 Correct 10 ms 2132 KB Output is correct
40 Correct 10 ms 2132 KB Output is correct
41 Correct 71 ms 2276 KB Output is correct
42 Correct 16 ms 2620 KB Output is correct
43 Correct 17 ms 2676 KB Output is correct
44 Correct 89 ms 2680 KB Output is correct
45 Correct 20 ms 3092 KB Output is correct
46 Correct 21 ms 3100 KB Output is correct
47 Correct 128 ms 3192 KB Output is correct
48 Correct 22 ms 3612 KB Output is correct
49 Correct 26 ms 3668 KB Output is correct
50 Correct 128 ms 3736 KB Output is correct
51 Correct 21 ms 4224 KB Output is correct
52 Correct 24 ms 4176 KB Output is correct
53 Correct 167 ms 4264 KB Output is correct
54 Correct 28 ms 4784 KB Output is correct
55 Correct 32 ms 4820 KB Output is correct
56 Correct 190 ms 4932 KB Output is correct
57 Correct 33 ms 5540 KB Output is correct
58 Correct 47 ms 5444 KB Output is correct
59 Correct 239 ms 5600 KB Output is correct
60 Correct 37 ms 6248 KB Output is correct
61 Correct 42 ms 6124 KB Output is correct
62 Correct 269 ms 6288 KB Output is correct
63 Correct 173 ms 6284 KB Output is correct
64 Correct 246 ms 6276 KB Output is correct
65 Correct 261 ms 6296 KB Output is correct
66 Correct 191 ms 6220 KB Output is correct
67 Correct 183 ms 6268 KB Output is correct
68 Correct 74 ms 6608 KB Output is correct
69 Correct 102 ms 6484 KB Output is correct
70 Correct 64 ms 6484 KB Output is correct
71 Correct 56 ms 6484 KB Output is correct
72 Correct 48 ms 6484 KB Output is correct
73 Correct 72 ms 9428 KB Output is correct
74 Correct 153 ms 9432 KB Output is correct
75 Correct 142 ms 9428 KB Output is correct
76 Correct 125 ms 9552 KB Output is correct
77 Correct 129 ms 9432 KB Output is correct
78 Correct 203 ms 9100 KB Output is correct
79 Correct 129 ms 9100 KB Output is correct
80 Correct 122 ms 9060 KB Output is correct
81 Correct 167 ms 9044 KB Output is correct
82 Correct 140 ms 9064 KB Output is correct
83 Correct 149 ms 8528 KB Output is correct
84 Correct 158 ms 8584 KB Output is correct
85 Correct 145 ms 8540 KB Output is correct
86 Correct 145 ms 8564 KB Output is correct
87 Correct 141 ms 8524 KB Output is correct
88 Correct 166 ms 8020 KB Output is correct
89 Correct 158 ms 8044 KB Output is correct
90 Correct 159 ms 8040 KB Output is correct
91 Correct 180 ms 8020 KB Output is correct
92 Correct 186 ms 8048 KB Output is correct