Submission #953342

# Submission time Handle Problem Language Result Execution time Memory
953342 2024-03-25T20:32:35 Z snowman Mecho (IOI09_mecho) C++17
100 / 100
272 ms 6460 KB
#include <bits/stdc++.h>
using namespace std;
//Soltan Cristian
#define ll long long
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define pb push_back
#define st first
#define nd second
#define sz(x) (ll)x.size()
#define rall(x) x.rbegin(), x.rend()

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

const int mxa = 2 * 1e5 + 2;
// string __fname = "maxflow";  
// ifstream in(__fname + ".in"); 
// ofstream out (__fname + ".out"); 
// #define cin in 
// #define cout out
int n, s;
vector<vector<char>> mapa;

bool validB(int x, int y){
    if(x >= 0 && x < n && y >= 0 && y < n && mapa[x][y] != 'T' && mapa[x][y] != 'D'){
        return true;
    }
    return false;
}

bool valid(int x, int y){
    if(x >= 0 && x < n && y >= 0 && y < n && mapa[x][y] != 'T'){
        return true;
    }
    return false;
}


void solve(){
    cin >> n >> s;
    mapa = vector<vector<char>>(n, vector<char>(n));
    vector<pair<int,int>> stup;
    pair<int, int> start, finish;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            char x;
            cin >> x;
            if(x == 'H') stup.pb({i, j});
            if(x == 'M') start = {i, j};
            if(x == 'D') finish = {i, j};
            mapa[i][j] = x;
        }
    }
    vector<vector<int>> bees(n, vector<int>(n, INT_MAX));
    queue<pair<int, int>> qB;
    for(int i = 0; i < sz(stup); i++){
        bees[stup[i].st][stup[i].nd] = 0;
        qB.push({stup[i].st, stup[i].nd});
    }
    // cout << "a\n";
    while(!qB.empty()){
        pair<int, int> u = qB.front();
        qB.pop();
        for(int i = 0; i < 4; i++){
            if(!validB(u.st + dx[i], u.nd + dy[i])) continue;
            if(bees[u.st][u.nd] + s < bees[u.st + dx[i]][u.nd + dy[i]]){
                qB.push({u.st + dx[i], u.nd + dy[i]});
                bees[u.st + dx[i]][u.nd + dy[i]] = bees[u.st][u.nd] + s;
            }
        }
    }
    if(bees[start.st][start.nd] == INT_MAX){
        cout << -1 << '\n';
        return;
    }
    // for(auto i : bees){
    //     for(auto j : i){
    //         if(j == INT_MAX){
    //             cout << -1 << ' ';
    //         }
    //         else cout << j << ' ';
    //     }
    //     cout << '\n';
    // }
    int l = 0, r = 1e9;
    // cout << r<< '\n';
    int ans = -1;
    while(l <= r){
        int mid = (l + r) / 2;
        vector<vector<int>> h(n, vector<int>(n, INT_MAX));
        h[start.st][start.nd] = mid ;
        if(bees[start.st][start.nd] <= mid){
            r = mid - 1;
            continue;
        }
        queue<pair<int,int>> q;
        q.push({start.st, start.nd});
        while(!q.empty()){
            pair<int, int> u = q.front();
            q.pop();
            for(int i = 0; i < 4; i++){
                if(!valid(u.st + dx[i], u.nd + dy[i])) continue;
                if(h[u.st][u.nd] + 1 < bees[u.st + dx[i]][u.nd + dy[i]] && 
                            h[u.st][u.nd] + 1 < h[u.st + dx[i]][u.nd + dy[i]]){
                    q.push({u.st + dx[i], u.nd + dy[i]});
                    h[u.st + dx[i]][u.nd + dy[i]] = h[u.st][u.nd] + 1;
                }
            }
        }
        if(h[finish.st][finish.nd] < INT_MAX){
            // cout << h[finish.st][finish.nd] << '\n';
            // cout << mid << ' ' << ans << '\n';
            ans = max(ans, mid);
            l = mid + 1;
        }
        else r = mid - 1;
    }
    // cout << ans << '\n';
    if(ans == -1){
        cout << -1<< '\n';
    }
    else
     cout << ans / s << '\n';

    // cout << -1 /3 << '\n';
}
 
 
int main()
{   
    // ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed  << (setprecision(10)); 
    // int t;
    // cin >> t;
    // while(t--)
        solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 142 ms 6236 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 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 0 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 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 504 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 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 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 10 ms 1544 KB Output is correct
34 Correct 10 ms 1532 KB Output is correct
35 Correct 37 ms 1544 KB Output is correct
36 Correct 13 ms 1760 KB Output is correct
37 Correct 13 ms 1880 KB Output is correct
38 Correct 56 ms 1948 KB Output is correct
39 Correct 17 ms 2260 KB Output is correct
40 Correct 17 ms 2256 KB Output is correct
41 Correct 63 ms 2328 KB Output is correct
42 Correct 19 ms 2692 KB Output is correct
43 Correct 21 ms 2684 KB Output is correct
44 Correct 82 ms 2692 KB Output is correct
45 Correct 24 ms 3124 KB Output is correct
46 Correct 24 ms 3240 KB Output is correct
47 Correct 98 ms 3144 KB Output is correct
48 Correct 29 ms 3768 KB Output is correct
49 Correct 30 ms 3656 KB Output is correct
50 Correct 117 ms 3668 KB Output is correct
51 Correct 45 ms 4280 KB Output is correct
52 Correct 35 ms 4276 KB Output is correct
53 Correct 137 ms 4204 KB Output is correct
54 Correct 39 ms 4904 KB Output is correct
55 Correct 38 ms 4884 KB Output is correct
56 Correct 179 ms 4868 KB Output is correct
57 Correct 43 ms 5536 KB Output is correct
58 Correct 45 ms 5532 KB Output is correct
59 Correct 197 ms 5512 KB Output is correct
60 Correct 55 ms 6244 KB Output is correct
61 Correct 48 ms 6216 KB Output is correct
62 Correct 221 ms 6172 KB Output is correct
63 Correct 126 ms 6256 KB Output is correct
64 Correct 272 ms 6272 KB Output is correct
65 Correct 180 ms 6260 KB Output is correct
66 Correct 146 ms 6164 KB Output is correct
67 Correct 130 ms 6200 KB Output is correct
68 Correct 90 ms 6264 KB Output is correct
69 Correct 82 ms 6280 KB Output is correct
70 Correct 84 ms 6460 KB Output is correct
71 Correct 70 ms 6208 KB Output is correct
72 Correct 62 ms 6232 KB Output is correct
73 Correct 80 ms 6288 KB Output is correct
74 Correct 118 ms 6240 KB Output is correct
75 Correct 107 ms 6276 KB Output is correct
76 Correct 103 ms 6268 KB Output is correct
77 Correct 115 ms 6352 KB Output is correct
78 Correct 111 ms 6232 KB Output is correct
79 Correct 111 ms 6216 KB Output is correct
80 Correct 96 ms 6332 KB Output is correct
81 Correct 111 ms 6256 KB Output is correct
82 Correct 100 ms 6240 KB Output is correct
83 Correct 119 ms 6256 KB Output is correct
84 Correct 139 ms 6228 KB Output is correct
85 Correct 130 ms 6300 KB Output is correct
86 Correct 117 ms 6260 KB Output is correct
87 Correct 123 ms 6252 KB Output is correct
88 Correct 129 ms 6216 KB Output is correct
89 Correct 155 ms 6188 KB Output is correct
90 Correct 135 ms 6292 KB Output is correct
91 Correct 137 ms 6256 KB Output is correct
92 Correct 129 ms 6312 KB Output is correct