답안 #789664

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789664 2023-07-21T17:03:44 Z phongcd Mecho (IOI09_mecho) C++14
100 / 100
147 ms 10020 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 cmp(ii a, ii b) {
    if (a.fi != b.fi) return a.fi < b.fi;
    return a.se < b.se; 
}

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
*/
# 결과 실행 시간 메모리 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 70 ms 9860 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 1484 KB Output is correct
15 Correct 1 ms 1108 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 1388 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 6 ms 4436 KB Output is correct
34 Correct 5 ms 4436 KB Output is correct
35 Correct 18 ms 4664 KB Output is correct
36 Correct 6 ms 4948 KB Output is correct
37 Correct 6 ms 4948 KB Output is correct
38 Correct 26 ms 5216 KB Output is correct
39 Correct 8 ms 5460 KB Output is correct
40 Correct 7 ms 5564 KB Output is correct
41 Correct 29 ms 5716 KB Output is correct
42 Correct 8 ms 5972 KB Output is correct
43 Correct 8 ms 5972 KB Output is correct
44 Correct 37 ms 6328 KB Output is correct
45 Correct 10 ms 6484 KB Output is correct
46 Correct 10 ms 6480 KB Output is correct
47 Correct 44 ms 6868 KB Output is correct
48 Correct 13 ms 6996 KB Output is correct
49 Correct 12 ms 7000 KB Output is correct
50 Correct 53 ms 7380 KB Output is correct
51 Correct 15 ms 7604 KB Output is correct
52 Correct 15 ms 7604 KB Output is correct
53 Correct 60 ms 8020 KB Output is correct
54 Correct 18 ms 8112 KB Output is correct
55 Correct 19 ms 8020 KB Output is correct
56 Correct 89 ms 8584 KB Output is correct
57 Correct 19 ms 8632 KB Output is correct
58 Correct 17 ms 8532 KB Output is correct
59 Correct 98 ms 9044 KB Output is correct
60 Correct 21 ms 9044 KB Output is correct
61 Correct 25 ms 9044 KB Output is correct
62 Correct 97 ms 9748 KB Output is correct
63 Correct 86 ms 9116 KB Output is correct
64 Correct 147 ms 9112 KB Output is correct
65 Correct 142 ms 9120 KB Output is correct
66 Correct 102 ms 9044 KB Output is correct
67 Correct 93 ms 9108 KB Output is correct
68 Correct 39 ms 9148 KB Output is correct
69 Correct 41 ms 9144 KB Output is correct
70 Correct 47 ms 9248 KB Output is correct
71 Correct 30 ms 9144 KB Output is correct
72 Correct 25 ms 9144 KB Output is correct
73 Correct 29 ms 9944 KB Output is correct
74 Correct 47 ms 9944 KB Output is correct
75 Correct 52 ms 10020 KB Output is correct
76 Correct 47 ms 9968 KB Output is correct
77 Correct 50 ms 10020 KB Output is correct
78 Correct 57 ms 9988 KB Output is correct
79 Correct 45 ms 9932 KB Output is correct
80 Correct 50 ms 10000 KB Output is correct
81 Correct 54 ms 9936 KB Output is correct
82 Correct 46 ms 10000 KB Output is correct
83 Correct 57 ms 9940 KB Output is correct
84 Correct 58 ms 9936 KB Output is correct
85 Correct 63 ms 9960 KB Output is correct
86 Correct 62 ms 9956 KB Output is correct
87 Correct 58 ms 9956 KB Output is correct
88 Correct 59 ms 9900 KB Output is correct
89 Correct 63 ms 9904 KB Output is correct
90 Correct 65 ms 9908 KB Output is correct
91 Correct 73 ms 9804 KB Output is correct
92 Correct 67 ms 9900 KB Output is correct