Submission #783838

# Submission time Handle Problem Language Result Execution time Memory
783838 2023-07-15T11:21:09 Z VMaksimoski008 Mecho (IOI09_mecho) C++14
69 / 100
176 ms 7508 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;
            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=1, r=n*n;
    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 5900 KB Output is correct
2 Correct 3 ms 5968 KB Output is correct
3 Correct 2 ms 5844 KB Output is correct
4 Correct 2 ms 5956 KB Output is correct
5 Incorrect 2 ms 5972 KB Output isn't correct
6 Correct 3 ms 5972 KB Output is correct
7 Correct 80 ms 7320 KB Output is correct
8 Correct 3 ms 5972 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 Incorrect 2 ms 5972 KB Output isn't correct
13 Incorrect 3 ms 5972 KB Output isn't 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 3 ms 5972 KB Output is correct
18 Correct 2 ms 5964 KB Output is correct
19 Correct 2 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 3 ms 5972 KB Output is correct
23 Correct 2 ms 5972 KB Output is correct
24 Correct 4 ms 5968 KB Output is correct
25 Correct 3 ms 5972 KB Output is correct
26 Correct 2 ms 5972 KB Output is correct
27 Correct 2 ms 5972 KB Output is correct
28 Correct 2 ms 5968 KB Output is correct
29 Correct 2 ms 5972 KB Output is correct
30 Correct 2 ms 5972 KB Output is correct
31 Correct 2 ms 5972 KB Output is correct
32 Correct 3 ms 5972 KB Output is correct
33 Correct 5 ms 6356 KB Output is correct
34 Correct 5 ms 6228 KB Output is correct
35 Correct 21 ms 6356 KB Output is correct
36 Correct 6 ms 6356 KB Output is correct
37 Correct 6 ms 6356 KB Output is correct
38 Correct 27 ms 6360 KB Output is correct
39 Correct 7 ms 6492 KB Output is correct
40 Correct 6 ms 6492 KB Output is correct
41 Correct 36 ms 6484 KB Output is correct
42 Correct 7 ms 6484 KB Output is correct
43 Correct 7 ms 6604 KB Output is correct
44 Correct 44 ms 6488 KB Output is correct
45 Correct 8 ms 6612 KB Output is correct
46 Correct 9 ms 6776 KB Output is correct
47 Correct 50 ms 6612 KB Output is correct
48 Correct 9 ms 6740 KB Output is correct
49 Correct 10 ms 6740 KB Output is correct
50 Correct 61 ms 6740 KB Output is correct
51 Correct 11 ms 6892 KB Output is correct
52 Correct 14 ms 6876 KB Output is correct
53 Correct 74 ms 6880 KB Output is correct
54 Correct 12 ms 7000 KB Output is correct
55 Correct 12 ms 6892 KB Output is correct
56 Correct 107 ms 6988 KB Output is correct
57 Correct 18 ms 7100 KB Output is correct
58 Correct 13 ms 7096 KB Output is correct
59 Correct 100 ms 6996 KB Output is correct
60 Correct 14 ms 7204 KB Output is correct
61 Correct 14 ms 7220 KB Output is correct
62 Correct 134 ms 7204 KB Output is correct
63 Incorrect 95 ms 7208 KB Output isn't correct
64 Correct 176 ms 7224 KB Output is correct
65 Correct 136 ms 7220 KB Output is correct
66 Correct 103 ms 7200 KB Output is correct
67 Correct 104 ms 7108 KB Output is correct
68 Incorrect 44 ms 7232 KB Output isn't correct
69 Correct 38 ms 7124 KB Output is correct
70 Correct 42 ms 7236 KB Output is correct
71 Correct 41 ms 7252 KB Output is correct
72 Incorrect 32 ms 7252 KB Output isn't correct
73 Incorrect 28 ms 7508 KB Output isn't correct
74 Correct 71 ms 7508 KB Output is correct
75 Correct 72 ms 7508 KB Output is correct
76 Correct 59 ms 7504 KB Output is correct
77 Correct 71 ms 7504 KB Output is correct
78 Correct 66 ms 7416 KB Output is correct
79 Correct 43 ms 7464 KB Output is correct
80 Correct 54 ms 7464 KB Output is correct
81 Correct 78 ms 7444 KB Output is correct
82 Correct 58 ms 7400 KB Output is correct
83 Incorrect 74 ms 7440 KB Output isn't correct
84 Correct 70 ms 7428 KB Output is correct
85 Correct 91 ms 7368 KB Output is correct
86 Correct 97 ms 7428 KB Output is correct
87 Correct 75 ms 7424 KB Output is correct
88 Incorrect 70 ms 7368 KB Output isn't correct
89 Correct 89 ms 7388 KB Output is correct
90 Correct 89 ms 7380 KB Output is correct
91 Correct 76 ms 7348 KB Output is correct
92 Correct 71 ms 7372 KB Output is correct