답안 #484690

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
484690 2021-11-05T04:29:11 Z Dirii Mecho (IOI09_mecho) C++14
100 / 100
185 ms 16120 KB
// #pragma GCC target("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define cll const ll
#define lp(a, b, c) for(ll a = b; a <= c; ++a)
#define lpd(a, b, c) for(ll a = b; a >= c; --a)
#define vec(a) vector<a>
#define pp(a, b) pair<a, b>
#define EACHCASE lpd(cs, read(), 1)
#define Fname "f"
using namespace std;

template <typename T> inline void Read(T &x){
    x = 0; char c;
    ll dau = 1;
    while(!isdigit(c = getchar())) if(c == '-') dau = -1;
    do{
        x = x * 10 + c - '0';
    } while (isdigit(c = getchar()));
    x *= dau;
}

ll read(){
    ll tmp;
    cin >> tmp;
    return tmp;
}

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

void OF(){
    if(fopen(Fname".inp", "r")){
        freopen(Fname".inp", "r", stdin);
        freopen(Fname".out", "w", stdout);
    } else if(fopen(Fname".in", "r")){
        freopen(Fname".in", "r", stdin);
        freopen(Fname".out", "w", stdout);
    }
}

cll mxn = 805, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
ll n, s, a[mxn][mxn], d[mxn][mxn], rest[mxn][mxn];
pp(ll, ll) st, fi;

// void bfs(ll i, ll j, ll step, const ll &ti){
//     // if(step == s) return;
//     // lp(t, 0, 3){
//     //     ll ii = i + dx[t], jj = j + dy[t];
//     //     if(a[ii][jj] == -1 || d[ii][jj]) continue;
//     //     d[ii][jj] = ti;
//     //     if(a[ii][jj] > ti) q.push({ii, jj});
//     //     dfs(ii, jj, step + 1, ti);
//     // }
//     queue<pp(ll, ll)> qq[2];
//     qq[0].push({i, j});
//     while(qq[0].size()){
//         if(++step > s) break;
//         while(qq[0].size()){
//             ll u = qq[0].front().first, v = qq[0].front().second;
//             // if(i == 4 && j == 5) cerr << u << ' ' << v << '\n';
//             qq[0].pop();
//             lp(t, 0, 3){
//                 ll nu = u + dx[t], nv = v + dy[t];
//                 if(a[nu][nv] == -1 || (d[nu][nv] != d[u][v] && d[nu][nv] != 0)) continue;
//                 d[nu][nv] = d[u][v];
//                 if(a[nu][nv] > d[nu][nv]) q.push({nu, nv});
//                 qq[1].push({nu, nv});
//             }
//         }
//         swap(qq[0], qq[1]);
//     }
// }

bool check(ll timestart){
    lp(i, 1, n) lp(j, 1, n) d[i][j] = rest[i][j] = 0;
    d[st.first][st.second] = timestart, rest[st.first][st.second] = s;
    queue<pp(ll, ll)> q;
    q.push(st);
    while(q.size()){
        ll u = q.front().first, v = q.front().second;
        q.pop();
        lp(i, 0, 3){
            ll nu = u + dx[i], nv = v + dy[i];
            if(a[nu][nv] == -1) continue;
            if(d[nu][nv] == d[u][v]){
                if(rest[nu][nv] < rest[u][v] - 1){
                    rest[nu][nv] = rest[u][v] - 1;
                    q.push({nu, nv});
                } 
            } else if(!d[nu][nv]){
                if(rest[u][v] == 1){
                    if(a[nu][nv] > d[u][v] + 1){
                        d[nu][nv] = d[u][v] + 1, rest[nu][nv] = s;
                        q.push({nu, nv});
                    }
                } else{
                    if(a[nu][nv] > d[u][v]){
                        d[nu][nv] = d[u][v], rest[nu][nv] = rest[u][v] - 1;
                        q.push({nu, nv});
                    }
                }
            }
        }
    }

    // while(q.size()){
    //     ll u = q.front().first, v = q.front().second;
    //     q.pop();
    //     lp(i, 0, 3){
    //         ll nu = u + dx[i], nv = v + dy[i];
    //         if(a[nu][nv] == -1 || (d[nu][nv] != 0 && d[nu][nv] != d[u][v] + 1)) continue;
    //         d[nu][nv] = d[u][v] + 1;
    //         // if(u == 4 && v == 4) cerr << nu << ' ' << nv << ' ' << d[nu][nv] << '\n';
    //         if(a[nu][nv] > d[nu][nv]) q.push({nu, nv});
    //         bfs(nu, nv, 1, d[nu][nv]);
    //     }   
    // }
    // lp(i, 1, n){
    //     lp(j, 1, n) cerr << d[i][j] << ' ';
    //     cerr << '\n';
    // }
    if(!d[fi.first][fi.second]) 
        return 0;
    return 1;
}

int main(){
    giuncute();
    #ifndef ONLINE_JUDGE
    OF();
    #endif
    memset(a, -1, sizeof a);
    memset(d, -1, sizeof d);
    cin >> n >> s;
    queue<pp(ll, ll)> q;
    lp(i, 1, n) lp(j, 1, n){
        char c;
        cin >> c;
        a[i][j] = 0;
        if(c == 'M') st = {i, j};
        else if(c == 'D') fi = {i, j};
        else if(c == 'T') a[i][j] = -1;
        else if(c == 'H') q.push({i, j}), a[i][j] = 1;
    }
    while(q.size()){
        ll u = q.front().first, v = q.front().second;
        q.pop();
        lp(i, 0, 3){
            ll nu = u + dx[i], nv = v + dy[i];
            if(a[nu][nv] || (nu == fi.first && nv == fi.second)) continue;
            a[nu][nv] = a[u][v] + 1;
            q.push({nu, nv});
        }
    }
    a[fi.first][fi.second] = 1e16;
    // lp(i, 1, n){
    //     lp(j, 1, n) cerr << a[i][j] << ' ';
    //     cerr << '\n';
    // }
    // cerr << check(2);
    ll l = 1, r = a[st.first][st.second] - 1, ans = -1; 
    while(l <= r){
        ll mid = (l + r) >> 1;
        if(check(mid)) ans = mid, l = mid + 1;
        else r = mid - 1;
    }
    if(ans != -1) ans -= 1;
    cout << ans;
}

Compilation message

mecho.cpp: In function 'void OF()':
mecho.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen(Fname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(Fname".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10444 KB Output is correct
2 Correct 5 ms 10444 KB Output is correct
3 Correct 4 ms 10444 KB Output is correct
4 Correct 4 ms 10444 KB Output is correct
5 Correct 4 ms 10444 KB Output is correct
6 Correct 4 ms 10444 KB Output is correct
7 Correct 95 ms 15840 KB Output is correct
8 Correct 4 ms 10444 KB Output is correct
9 Correct 4 ms 10444 KB Output is correct
10 Correct 4 ms 10444 KB Output is correct
11 Correct 5 ms 10444 KB Output is correct
12 Correct 5 ms 10700 KB Output is correct
13 Correct 5 ms 10700 KB Output is correct
14 Correct 5 ms 10656 KB Output is correct
15 Correct 4 ms 10444 KB Output is correct
16 Correct 5 ms 10444 KB Output is correct
17 Correct 4 ms 10572 KB Output is correct
18 Correct 4 ms 10572 KB Output is correct
19 Correct 5 ms 10580 KB Output is correct
20 Correct 5 ms 10572 KB Output is correct
21 Correct 5 ms 10572 KB Output is correct
22 Correct 5 ms 10572 KB Output is correct
23 Correct 4 ms 10700 KB Output is correct
24 Correct 5 ms 10700 KB Output is correct
25 Correct 5 ms 10700 KB Output is correct
26 Correct 6 ms 10700 KB Output is correct
27 Correct 5 ms 10700 KB Output is correct
28 Correct 5 ms 10700 KB Output is correct
29 Correct 5 ms 10700 KB Output is correct
30 Correct 5 ms 10700 KB Output is correct
31 Correct 5 ms 10700 KB Output is correct
32 Correct 5 ms 10700 KB Output is correct
33 Correct 10 ms 12620 KB Output is correct
34 Correct 10 ms 12620 KB Output is correct
35 Correct 20 ms 12684 KB Output is correct
36 Correct 11 ms 12876 KB Output is correct
37 Correct 11 ms 12880 KB Output is correct
38 Correct 28 ms 13004 KB Output is correct
39 Correct 13 ms 13284 KB Output is correct
40 Correct 13 ms 13260 KB Output is correct
41 Correct 35 ms 13288 KB Output is correct
42 Correct 15 ms 13624 KB Output is correct
43 Correct 16 ms 13516 KB Output is correct
44 Correct 44 ms 13516 KB Output is correct
45 Correct 18 ms 13900 KB Output is correct
46 Correct 19 ms 13944 KB Output is correct
47 Correct 53 ms 13900 KB Output is correct
48 Correct 21 ms 14152 KB Output is correct
49 Correct 22 ms 14156 KB Output is correct
50 Correct 63 ms 14212 KB Output is correct
51 Correct 31 ms 14564 KB Output is correct
52 Correct 27 ms 14572 KB Output is correct
53 Correct 77 ms 14568 KB Output is correct
54 Correct 32 ms 14816 KB Output is correct
55 Correct 32 ms 14796 KB Output is correct
56 Correct 91 ms 14796 KB Output is correct
57 Correct 36 ms 15180 KB Output is correct
58 Correct 37 ms 15084 KB Output is correct
59 Correct 115 ms 15200 KB Output is correct
60 Correct 42 ms 15464 KB Output is correct
61 Correct 42 ms 15460 KB Output is correct
62 Correct 133 ms 15512 KB Output is correct
63 Correct 115 ms 15428 KB Output is correct
64 Correct 185 ms 15560 KB Output is correct
65 Correct 178 ms 15460 KB Output is correct
66 Correct 140 ms 15456 KB Output is correct
67 Correct 135 ms 15444 KB Output is correct
68 Correct 57 ms 15456 KB Output is correct
69 Correct 52 ms 15428 KB Output is correct
70 Correct 51 ms 15556 KB Output is correct
71 Correct 41 ms 15500 KB Output is correct
72 Correct 42 ms 15520 KB Output is correct
73 Correct 42 ms 16024 KB Output is correct
74 Correct 66 ms 16028 KB Output is correct
75 Correct 63 ms 16052 KB Output is correct
76 Correct 66 ms 16120 KB Output is correct
77 Correct 68 ms 16012 KB Output is correct
78 Correct 78 ms 15988 KB Output is correct
79 Correct 64 ms 15956 KB Output is correct
80 Correct 66 ms 15992 KB Output is correct
81 Correct 67 ms 15936 KB Output is correct
82 Correct 63 ms 15940 KB Output is correct
83 Correct 87 ms 15976 KB Output is correct
84 Correct 73 ms 15812 KB Output is correct
85 Correct 81 ms 15812 KB Output is correct
86 Correct 83 ms 15892 KB Output is correct
87 Correct 78 ms 15812 KB Output is correct
88 Correct 88 ms 15820 KB Output is correct
89 Correct 87 ms 15864 KB Output is correct
90 Correct 90 ms 15820 KB Output is correct
91 Correct 92 ms 15836 KB Output is correct
92 Correct 89 ms 15728 KB Output is correct