Submission #789665

# Submission time Handle Problem Language Result Execution time Memory
789665 2023-07-21T17:04:31 Z phongcd Mecho (IOI09_mecho) C++14
100 / 100
145 ms 9416 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long  double
#define ull unsigned long long
#define ii pair <int, int>
#define ill pair <ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define file "test"
using namespace std;
const ll N = 8e2 + 2;
const ll MOD = 1e9;
const ll INF = 1e18;
const ll base = 311;
const int BLOCK_SIZE = 2000;
 
int n, s;
ii start, target, d[N][N];
bool vis[N][N];
char a[N][N];
int mtime[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
 
void addstep(ii &a) {
    a.se += 1;
    if (a.se == s) a.fi ++, a.se = 0;
}

bool cmp2(ii a, int b) {
    if (a.fi != b) return a.fi < b;
    return a.se < s;
}
 
bool check(int x) {
    if (x >= mtime[start.fi][start.se]) return 0;

    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++) d[i][j] = {2e9, 0};

    queue <ii> h;
    h.push(start);
    d[start.fi][start.se] = {x + 1, 0};
    vis[start.fi][start.se] = 1;
 
    while (!h.empty()) {
        ii e = h.front(); h.pop();
        int x = e.fi, y = e.se;
        ii res = d[x][y];
        addstep(res);
 
        for (int j = 0; j <= 3; j ++) {
            int u = x + dx[j];
            int v = y + dy[j];
            if (a[u][v] == 'G' && cmp2(res, mtime[u][v]) && !vis[u][v]) {
                h.push({u, v});
                d[u][v] = res;
                vis[u][v] = 1;
            }
        }
    }
    return d[target.fi][target.se].fi != 2e9;
}
 
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> s;
    queue <ii> bee;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++) {
            cin >> a[i][j]; 
            mtime[i][j] = 2e9;
 
            if (a[i][j] == 'H') 
                bee.push({i, j}), mtime[i][j] = 0;
 
            if (a[i][j] == 'M')
                a[i][j] = 'G', start = {i, j};
 
            if (a[i][j] == 'D') 
                target = {i, j};
        }
 
    while (!bee.empty()) {
        ii e = bee.front(); bee.pop();
        int x = e.fi, y = e.se;
        int res = mtime[x][y] + 1;
 
        for (int j = 0; j <= 3; j ++) {
            int u = x + dx[j];
            int v = y + dy[j];
            if (a[u][v] == 'G' && mtime[u][v] > res) {
                bee.push({u, v});
                mtime[u][v] = res;
            }
        }
    }
    a[target.fi][target.se] = 'G';

    int l = 0, r = 1e9;
    while (l < r) {
        int mid = l + (r - l + 1) / 2;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    cout << (check(l) ? l : -1) << '\n';
}
/*
     /\_/\ zzZ
    (= -_-)
    / >u  >u
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 67 ms 9228 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 1364 KB Output is correct
13 Correct 1 ms 1364 KB Output is correct
14 Correct 1 ms 1364 KB Output is correct
15 Correct 1 ms 1152 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 1108 KB Output is correct
21 Correct 1 ms 1236 KB Output is correct
22 Correct 1 ms 1236 KB Output is correct
23 Correct 1 ms 1364 KB Output is correct
24 Correct 1 ms 1364 KB Output is correct
25 Correct 1 ms 1364 KB Output is correct
26 Correct 1 ms 1364 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 1 ms 1492 KB Output is correct
29 Correct 1 ms 1492 KB Output is correct
30 Correct 1 ms 1492 KB Output is correct
31 Correct 1 ms 1492 KB Output is correct
32 Correct 1 ms 1492 KB Output is correct
33 Correct 5 ms 4540 KB Output is correct
34 Correct 5 ms 4544 KB Output is correct
35 Correct 18 ms 4472 KB Output is correct
36 Correct 6 ms 4948 KB Output is correct
37 Correct 5 ms 4948 KB Output is correct
38 Correct 23 ms 5032 KB Output is correct
39 Correct 8 ms 5560 KB Output is correct
40 Correct 6 ms 5460 KB Output is correct
41 Correct 29 ms 5472 KB Output is correct
42 Correct 8 ms 6020 KB Output is correct
43 Correct 7 ms 6032 KB Output is correct
44 Correct 38 ms 5972 KB Output is correct
45 Correct 12 ms 6484 KB Output is correct
46 Correct 10 ms 6484 KB Output is correct
47 Correct 44 ms 6592 KB Output is correct
48 Correct 13 ms 7096 KB Output is correct
49 Correct 13 ms 6996 KB Output is correct
50 Correct 54 ms 7044 KB Output is correct
51 Correct 14 ms 7496 KB Output is correct
52 Correct 15 ms 7500 KB Output is correct
53 Correct 76 ms 7596 KB Output is correct
54 Correct 18 ms 8020 KB Output is correct
55 Correct 21 ms 8120 KB Output is correct
56 Correct 72 ms 8104 KB Output is correct
57 Correct 17 ms 8556 KB Output is correct
58 Correct 16 ms 8532 KB Output is correct
59 Correct 82 ms 8532 KB Output is correct
60 Correct 19 ms 9044 KB Output is correct
61 Correct 22 ms 9140 KB Output is correct
62 Correct 94 ms 9120 KB Output is correct
63 Correct 94 ms 9112 KB Output is correct
64 Correct 143 ms 9044 KB Output is correct
65 Correct 145 ms 9120 KB Output is correct
66 Correct 105 ms 9056 KB Output is correct
67 Correct 97 ms 9084 KB Output is correct
68 Correct 39 ms 9056 KB Output is correct
69 Correct 44 ms 9144 KB Output is correct
70 Correct 38 ms 9136 KB Output is correct
71 Correct 31 ms 9140 KB Output is correct
72 Correct 25 ms 9044 KB Output is correct
73 Correct 29 ms 9416 KB Output is correct
74 Correct 49 ms 9396 KB Output is correct
75 Correct 56 ms 9396 KB Output is correct
76 Correct 48 ms 9400 KB Output is correct
77 Correct 52 ms 9396 KB Output is correct
78 Correct 57 ms 9356 KB Output is correct
79 Correct 46 ms 9376 KB Output is correct
80 Correct 49 ms 9300 KB Output is correct
81 Correct 51 ms 9368 KB Output is correct
82 Correct 47 ms 9300 KB Output is correct
83 Correct 57 ms 9236 KB Output is correct
84 Correct 55 ms 9332 KB Output is correct
85 Correct 57 ms 9296 KB Output is correct
86 Correct 61 ms 9320 KB Output is correct
87 Correct 59 ms 9328 KB Output is correct
88 Correct 59 ms 9276 KB Output is correct
89 Correct 77 ms 9276 KB Output is correct
90 Correct 67 ms 9280 KB Output is correct
91 Correct 63 ms 9280 KB Output is correct
92 Correct 67 ms 9164 KB Output is correct