답안 #810475

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
810475 2023-08-06T10:04:10 Z asdasdqwer Mecho (IOI09_mecho) C++17
100 / 100
530 ms 17004 KB
#include <bits/stdc++.h>
using namespace std;

#define int int64_t
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define tii array<int, 3>
#define all(x) x.begin(), x.end()
#define pb push_back
#define ppi pair<pii, int>

vector<pii> dir = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

pii operator+(const pii &x, const pii &y) {
    return mp(x.f+y.f, x.s+y.s);
}

bool check(const pii &x, int n) {
    return x.f >= 0 && x.f < n && x.s >= 0 && x.s < n;
}

// void printArr(vector<vector<int>> &a) {
//     int n = a.size();
//     for (int i=0;i<n;i++) {
//         fmt::print("{}\n", a[i]);
//     }
// }

bool test(int wait, vector<vector<int>> &grid, int st, pii &pos, pii &des) {
    // simulate "wait"
    int n=grid.size();
    vector<vector<int>> status(n, vector<int>(n, 0));
    queue<pair<pii, int>> q;
    vector<pii> hv;
    for (int i=0;i<n;i++) {
        for (int j=0;j<n;j++) {
            if (grid[i][j]==0) {
                hv.pb(mp(i, j));
            }
        }
    }

    for (auto &x:hv) {
        q.push(mp(x, 0));
        status[x.f][x.s]=1;
    }

    while (!q.empty() && q.front().s != wait) {
        ppi t = q.front();q.pop();
        if (t.s != wait) {
            for (int i=0;i<4;i++) {
                pii add = t.f + dir[i];
                if (check(add, n) && status[add.f][add.s] != 1 && grid[add.f][add.s]==1) {
                    status[add.f][add.s]=1;
                    q.push(mp(add, t.s+1));
                }
            }
        }
    }

    queue<ppi> pq;
    pq.push(mp(pos, 0));
    int endB = wait;
    int endP = 0;
    while (true) {
        endB++;endP+=st;
        if (pq.empty()) return false;
        while (!pq.empty() && pq.front().s != endP) {
            ppi t=pq.front();pq.pop();
            if (t.f==des) {
                return true;
            }

            if (status[t.f.f][t.f.s]==1) {
                continue;
            }

            for (int i=0;i<4;i++) {
                pii add=t.f + dir[i];
                if (check(add, n) && (status[add.f][add.s]==0) && grid[add.f][add.s]==1) {
                    status[add.f][add.s]=2;
                    pq.push(mp(add, t.s+1));
                }

                else if (check(add, n) && add==des) {
                    return true;
                } 
            }
        }

        while (!q.empty() && q.front().s != endB) {
            ppi t = q.front();q.pop();
            if (t.s != endB) {
                for (int i=0;i<4;i++) {
                    pii add = t.f + dir[i];
                    if (check(add, n) && status[add.f][add.s] != 1 && grid[add.f][add.s]==1) {
                        status[add.f][add.s]=1;
                        q.push(mp(add, t.s+1));
                    }
                }
            }
        }
    }
}

int binSearch(vector<vector<int>> a, int st, pii &pos, pii &des) {
    int n = a.size();
    int l = 0, r = 2*(n*n);

    int ans = -1;
    while (l <= r) {
        int m = l + (r-l)/2;
        bool t = test(m, a, st, pos, des);
        // cout << m << " " << t << "\n";
        if (t) {
            l = m+1;
            ans = m;
        }

        else {
            r = m-1;
        }
    }

    return ans;
}

signed main() {
    // freopen("in.txt", "r", stdin);
    int n, st;cin>>n>>st;
    vector<vector<int>> a(n, vector<int>(n, 0));
    pii pos, des;
    for (int i=0;i<n;i++) {
        string s;cin>>s;
        for (int j=0;j<n;j++) {
            if (s[j]=='H')a[i][j]=0;
            if (s[j]=='G')a[i][j]=1;
            if (s[j]=='M') {
                a[i][j]=1;
                pos=mp(i, j);
            }

            if (s[j]=='D') {
                a[i][j]=-1;
                des=mp(i, j);
            }

            if (s[j]=='T')a[i][j]=-1;
        }
    }
    // cout << test(0, a, st, pos, des) << "\n";
    cout << binSearch(a, st, pos, des) << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 400 ms 16336 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 304 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 26 ms 3344 KB Output is correct
34 Correct 27 ms 3336 KB Output is correct
35 Correct 36 ms 3284 KB Output is correct
36 Correct 34 ms 4240 KB Output is correct
37 Correct 36 ms 4296 KB Output is correct
38 Correct 47 ms 4180 KB Output is correct
39 Correct 52 ms 5480 KB Output is correct
40 Correct 48 ms 5312 KB Output is correct
41 Correct 64 ms 5304 KB Output is correct
42 Correct 74 ms 6480 KB Output is correct
43 Correct 57 ms 6492 KB Output is correct
44 Correct 88 ms 6488 KB Output is correct
45 Correct 92 ms 7876 KB Output is correct
46 Correct 84 ms 7776 KB Output is correct
47 Correct 91 ms 7764 KB Output is correct
48 Correct 85 ms 9192 KB Output is correct
49 Correct 99 ms 9172 KB Output is correct
50 Correct 132 ms 9240 KB Output is correct
51 Correct 112 ms 10736 KB Output is correct
52 Correct 116 ms 10716 KB Output is correct
53 Correct 138 ms 10716 KB Output is correct
54 Correct 126 ms 12396 KB Output is correct
55 Correct 149 ms 12368 KB Output is correct
56 Correct 184 ms 12372 KB Output is correct
57 Correct 138 ms 14180 KB Output is correct
58 Correct 137 ms 14168 KB Output is correct
59 Correct 207 ms 14164 KB Output is correct
60 Correct 174 ms 16092 KB Output is correct
61 Correct 157 ms 16180 KB Output is correct
62 Correct 416 ms 16064 KB Output is correct
63 Correct 355 ms 16132 KB Output is correct
64 Correct 355 ms 16060 KB Output is correct
65 Correct 311 ms 16056 KB Output is correct
66 Correct 344 ms 16060 KB Output is correct
67 Correct 334 ms 16052 KB Output is correct
68 Correct 398 ms 16136 KB Output is correct
69 Correct 341 ms 16136 KB Output is correct
70 Correct 364 ms 16140 KB Output is correct
71 Correct 405 ms 16212 KB Output is correct
72 Correct 448 ms 16084 KB Output is correct
73 Correct 298 ms 16916 KB Output is correct
74 Correct 378 ms 16940 KB Output is correct
75 Correct 530 ms 17004 KB Output is correct
76 Correct 457 ms 16992 KB Output is correct
77 Correct 382 ms 16928 KB Output is correct
78 Correct 361 ms 16900 KB Output is correct
79 Correct 339 ms 16812 KB Output is correct
80 Correct 363 ms 16824 KB Output is correct
81 Correct 327 ms 16824 KB Output is correct
82 Correct 324 ms 16824 KB Output is correct
83 Correct 336 ms 16812 KB Output is correct
84 Correct 354 ms 16688 KB Output is correct
85 Correct 368 ms 16712 KB Output is correct
86 Correct 371 ms 16688 KB Output is correct
87 Correct 337 ms 16764 KB Output is correct
88 Correct 330 ms 16536 KB Output is correct
89 Correct 389 ms 16540 KB Output is correct
90 Correct 326 ms 16516 KB Output is correct
91 Correct 351 ms 16540 KB Output is correct
92 Correct 388 ms 16540 KB Output is correct