Submission #783872

# Submission time Handle Problem Language Result Execution time Memory
783872 2023-07-15T11:37:53 Z VMaksimoski008 Mecho (IOI09_mecho) C++14
97 / 100
154 ms 7272 KB
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <stack>
#include <cstring>
#include <chrono>
#include <random>
#include <numeric>
#include <bitset>
#include <array>
#include <functional>

#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << '\n'
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define each(x, v) for(auto &x : v)
#define mp make_pair

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int mod = 1e9 + 7;
const int LOG = 20;

void setIO() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

int n, s;
int dr[4] = {0, -1, 0, 1};
int dc[4] = {-1, 0, 1, 0};
char mat[800][800];
vector<pii> hives;
pii mecho, home;

int dist[800][800][2];
bool vis[800][800];

bool ok(int r, int c) {
    return (r >= 0 && r < n && c >= 0 && c < n && !vis[r][c] && mat[r][c] != 'T');
}

void hive_fill() {
    queue<pair<int, int> > q;
    for(pii &x : hives) {
        q.push(x);
        vis[x.first][x.second] = true;
    }

    while(!q.empty()) {
        int r = q.front().first;
        int c = q.front().second;
        q.pop();

        for(int i=0; i<4; i++) {
            int newR = r + dr[i];
            int newC = c + dc[i];
            if(!ok(newR, newC) || mat[newR][newC] == 'D') continue;
            vis[newR][newC] = true;
            dist[newR][newC][1] = dist[r][c][1] + 1;
            if(mat[newR][newC] == 'G')
                q.push({ newR, newC });
        }
    }
}

int calcMinutes(int steps) {
    return steps / s;
}

bool check(int mid) {
    queue<pii> q;
    memset(vis, 0, sizeof(vis));

    q.push(mecho);
    vis[mecho.first][mecho.second] = true;
    while(!q.empty()) {
        int r = q.front().first;
        int c = q.front().second;
        q.pop();

        if(r == home.first && c == home.second)
            return true;

        for(int i=0; i<4; i++) {
            int newR = r + dr[i];
            int newC = c + dc[i];
            if(!ok(newR, newC)) continue;
            if(mid >= dist[newR][newC][1] && mat[newR][newC] != 'D') 
                continue;
            if(mid + calcMinutes(dist[r][c][0] + 1) >= dist[newR][newC][1] && mat[newR][newC] != 'D')
                continue;
            q.push({ newR, newC });
            vis[newR][newC] = true;
            dist[newR][newC][0] = dist[r][c][0] + 1;
        }
    }

    return false;
}

int32_t main() {
    setIO();

    cin >> n >> s;
    memset(dist, 0, sizeof(dist));
    memset(vis, 0, sizeof(vis));
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            cin >> mat[i][j];
            if(mat[i][j] == 'H') hives.pb({i, j});
            else if(mat[i][j] == 'M') mecho = {i, j};
            else if(mat[i][j] == 'D') home = {i, j};
        }
    }

    hive_fill();

    int ans = -1;
    int l=0, r=dist[mecho.first][mecho.second][1]-1;
    while(l <= r) {
        int mid = (l + r) / 2;

        if(check(mid)) {
            ans = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }

    // for(int i=0; i<n; i++) {
    //     for(int j=0; j<n; j++) {
    //         cout << dist[i][j][1];
    //         if(j == n-1) cout << '\n';
    //         else cout << " ";
    //     }
    // }

    // cout << "-----------------\n";

    
    // for(int i=0; i<n; i++) {
    //     for(int j=0; j<n; j++) {
    //         cout << dist[i][j][0];
    //         if(j == n-1) cout << '\n';
    //         else cout << " ";
    //     }
    // }

    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5968 KB Output is correct
2 Correct 2 ms 5964 KB Output is correct
3 Correct 2 ms 5972 KB Output is correct
4 Correct 3 ms 5844 KB Output is correct
5 Correct 2 ms 5972 KB Output is correct
6 Correct 3 ms 5964 KB Output is correct
7 Correct 78 ms 6996 KB Output is correct
8 Correct 3 ms 5964 KB Output is correct
9 Correct 3 ms 5972 KB Output is correct
10 Correct 3 ms 5972 KB Output is correct
11 Correct 3 ms 5972 KB Output is correct
12 Correct 2 ms 5972 KB Output is correct
13 Correct 2 ms 5964 KB Output is correct
14 Correct 3 ms 5972 KB Output is correct
15 Correct 2 ms 5972 KB Output is correct
16 Correct 2 ms 5972 KB Output is correct
17 Correct 2 ms 5972 KB Output is correct
18 Correct 2 ms 5968 KB Output is correct
19 Correct 3 ms 5972 KB Output is correct
20 Correct 2 ms 5972 KB Output is correct
21 Correct 3 ms 5972 KB Output is correct
22 Correct 2 ms 5972 KB Output is correct
23 Correct 2 ms 5972 KB Output is correct
24 Correct 2 ms 5972 KB Output is correct
25 Correct 3 ms 5972 KB Output is correct
26 Correct 3 ms 5972 KB Output is correct
27 Correct 3 ms 5968 KB Output is correct
28 Correct 3 ms 5968 KB Output is correct
29 Correct 3 ms 5972 KB Output is correct
30 Correct 3 ms 5972 KB Output is correct
31 Correct 3 ms 6016 KB Output is correct
32 Correct 3 ms 5972 KB Output is correct
33 Correct 7 ms 6236 KB Output is correct
34 Correct 5 ms 6312 KB Output is correct
35 Correct 20 ms 6364 KB Output is correct
36 Correct 6 ms 6332 KB Output is correct
37 Correct 6 ms 6364 KB Output is correct
38 Correct 26 ms 6336 KB Output is correct
39 Correct 7 ms 6492 KB Output is correct
40 Correct 10 ms 6496 KB Output is correct
41 Correct 36 ms 6500 KB Output is correct
42 Correct 7 ms 6484 KB Output is correct
43 Correct 7 ms 6484 KB Output is correct
44 Correct 41 ms 6484 KB Output is correct
45 Correct 9 ms 6648 KB Output is correct
46 Correct 10 ms 6688 KB Output is correct
47 Correct 54 ms 6684 KB Output is correct
48 Correct 10 ms 6748 KB Output is correct
49 Correct 10 ms 6772 KB Output is correct
50 Correct 58 ms 6776 KB Output is correct
51 Correct 11 ms 6740 KB Output is correct
52 Correct 11 ms 6764 KB Output is correct
53 Correct 71 ms 6844 KB Output is correct
54 Correct 12 ms 6884 KB Output is correct
55 Correct 18 ms 6776 KB Output is correct
56 Correct 106 ms 6884 KB Output is correct
57 Correct 13 ms 6828 KB Output is correct
58 Correct 14 ms 6868 KB Output is correct
59 Correct 92 ms 6924 KB Output is correct
60 Correct 15 ms 6888 KB Output is correct
61 Correct 16 ms 6868 KB Output is correct
62 Correct 113 ms 6960 KB Output is correct
63 Correct 97 ms 6868 KB Output is correct
64 Correct 154 ms 6972 KB Output is correct
65 Correct 151 ms 6960 KB Output is correct
66 Correct 113 ms 6876 KB Output is correct
67 Correct 106 ms 6996 KB Output is correct
68 Correct 48 ms 6980 KB Output is correct
69 Correct 39 ms 6860 KB Output is correct
70 Correct 35 ms 6988 KB Output is correct
71 Correct 31 ms 6864 KB Output is correct
72 Incorrect 54 ms 6984 KB Output isn't correct
73 Correct 36 ms 7252 KB Output is correct
74 Correct 70 ms 7256 KB Output is correct
75 Correct 57 ms 7260 KB Output is correct
76 Correct 59 ms 7272 KB Output is correct
77 Correct 60 ms 7252 KB Output is correct
78 Correct 68 ms 7216 KB Output is correct
79 Correct 50 ms 7148 KB Output is correct
80 Correct 59 ms 7124 KB Output is correct
81 Correct 70 ms 7228 KB Output is correct
82 Correct 55 ms 7124 KB Output is correct
83 Correct 76 ms 7108 KB Output is correct
84 Correct 62 ms 7124 KB Output is correct
85 Correct 66 ms 7124 KB Output is correct
86 Correct 71 ms 7184 KB Output is correct
87 Correct 64 ms 7180 KB Output is correct
88 Correct 78 ms 7132 KB Output is correct
89 Correct 73 ms 7120 KB Output is correct
90 Correct 93 ms 7124 KB Output is correct
91 Correct 77 ms 7140 KB Output is correct
92 Correct 79 ms 7140 KB Output is correct