답안 #710505

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710505 2023-03-15T09:46:31 Z hafo Mecho (IOI09_mecho) C++14
100 / 100
211 ms 6348 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 8e2 + 7;
const ll oo = 1e9 + 69;

int n, s, xd, yd, f[3][maxn][maxn];
char a[maxn][maxn];
pa d[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void bfs(int t, int time) {
    memset(f[t], -1, sizeof f[t]);
    queue<pa> q;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) {
            if(a[i][j] == 'M' && t != 0 && f[0][i][j] > time) {
                f[t][i][j] = 0;
                q.push({i, j});
            }
            if(a[i][j] == 'H' && t == 0) {
                f[t][i][j] = 0;
                q.push({i, j});
            }
        }

    while(!q.empty()) {
        int u = q.front().fi, v = q.front().se;
        q.pop();

        for(int k = 0; k < 4; k++) {
            int x = u + d[k].fi;
            int y = v + d[k].se;
            if(x < 0 || x == n || y < 0 || y == n || a[x][y] == 'T' || f[t][x][y] != -1 || a[x][y] == 'H' || (!t && a[x][y] == 'D')) continue;
            if(t && f[0][x][y] != -1 && f[0][x][y] - (f[1][u][v] + 1) / s <= time) continue;
            f[t][x][y] = f[t][u][v] + 1;
            q.push({x, y});
        }
    }
}

bool check(int time) {
    bfs(1, time);
    for(int k = 0; k < 4; k++) {
        int x = xd + d[k].fi;
        int y = yd + d[k].se;
        if(x < 0 || x == n || y < 0 || y == n || f[1][x][y] == -1) continue;
        if(f[0][x][y] == -1) return 1;
        if(f[0][x][y] - f[1][x][y] / s > time) return 1;
    }
    return 0;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n>>s;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++){
            cin>>a[i][j];
            if(a[i][j] == 'D') {
                xd = i;
                yd = j;
            }
        }

    bfs(0, 0);
    int l = 0, r = n * n, mid, res = -1;
    while(l <= r) {
        mid = l + r >> 1;
        if(check(mid)) {
            l = mid + 1;
            res = mid;
        } else r = mid -1 ;
    }
    cout<<res;
    return 0;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:87:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   87 |         mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 3 ms 5332 KB Output is correct
3 Correct 3 ms 5332 KB Output is correct
4 Correct 3 ms 5332 KB Output is correct
5 Correct 3 ms 5332 KB Output is correct
6 Correct 3 ms 5332 KB Output is correct
7 Correct 96 ms 6176 KB Output is correct
8 Correct 3 ms 5332 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 3 ms 5332 KB Output is correct
12 Correct 3 ms 5460 KB Output is correct
13 Correct 3 ms 5476 KB Output is correct
14 Correct 5 ms 5460 KB Output is correct
15 Correct 3 ms 5332 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 3 ms 5332 KB Output is correct
18 Correct 3 ms 5332 KB Output is correct
19 Correct 4 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
21 Correct 3 ms 5460 KB Output is correct
22 Correct 3 ms 5460 KB Output is correct
23 Correct 4 ms 5460 KB Output is correct
24 Correct 3 ms 5460 KB Output is correct
25 Correct 4 ms 5460 KB Output is correct
26 Correct 3 ms 5460 KB Output is correct
27 Correct 4 ms 5460 KB Output is correct
28 Correct 4 ms 5460 KB Output is correct
29 Correct 3 ms 5460 KB Output is correct
30 Correct 4 ms 5460 KB Output is correct
31 Correct 3 ms 5460 KB Output is correct
32 Correct 4 ms 5460 KB Output is correct
33 Correct 9 ms 5704 KB Output is correct
34 Correct 9 ms 5588 KB Output is correct
35 Correct 25 ms 5588 KB Output is correct
36 Correct 10 ms 5716 KB Output is correct
37 Correct 12 ms 5752 KB Output is correct
38 Correct 32 ms 5716 KB Output is correct
39 Correct 13 ms 5716 KB Output is correct
40 Correct 14 ms 5664 KB Output is correct
41 Correct 49 ms 5668 KB Output is correct
42 Correct 16 ms 5716 KB Output is correct
43 Correct 17 ms 5820 KB Output is correct
44 Correct 52 ms 5748 KB Output is correct
45 Correct 17 ms 5864 KB Output is correct
46 Correct 18 ms 5864 KB Output is correct
47 Correct 60 ms 5852 KB Output is correct
48 Correct 19 ms 5904 KB Output is correct
49 Correct 22 ms 5792 KB Output is correct
50 Correct 72 ms 5844 KB Output is correct
51 Correct 23 ms 5832 KB Output is correct
52 Correct 26 ms 5948 KB Output is correct
53 Correct 92 ms 5932 KB Output is correct
54 Correct 26 ms 5984 KB Output is correct
55 Correct 26 ms 5980 KB Output is correct
56 Correct 105 ms 5980 KB Output is correct
57 Correct 29 ms 5972 KB Output is correct
58 Correct 38 ms 5972 KB Output is correct
59 Correct 122 ms 5956 KB Output is correct
60 Correct 33 ms 6036 KB Output is correct
61 Correct 33 ms 5972 KB Output is correct
62 Correct 139 ms 6048 KB Output is correct
63 Correct 113 ms 6044 KB Output is correct
64 Correct 211 ms 5964 KB Output is correct
65 Correct 150 ms 5944 KB Output is correct
66 Correct 132 ms 6044 KB Output is correct
67 Correct 149 ms 6044 KB Output is correct
68 Correct 61 ms 6072 KB Output is correct
69 Correct 58 ms 5968 KB Output is correct
70 Correct 53 ms 5964 KB Output is correct
71 Correct 60 ms 5956 KB Output is correct
72 Correct 41 ms 5968 KB Output is correct
73 Correct 44 ms 6348 KB Output is correct
74 Correct 68 ms 6288 KB Output is correct
75 Correct 79 ms 6316 KB Output is correct
76 Correct 73 ms 6220 KB Output is correct
77 Correct 78 ms 6320 KB Output is correct
78 Correct 94 ms 6300 KB Output is correct
79 Correct 60 ms 6300 KB Output is correct
80 Correct 85 ms 6224 KB Output is correct
81 Correct 92 ms 6176 KB Output is correct
82 Correct 80 ms 6296 KB Output is correct
83 Correct 90 ms 6280 KB Output is correct
84 Correct 72 ms 6256 KB Output is correct
85 Correct 92 ms 6256 KB Output is correct
86 Correct 101 ms 6256 KB Output is correct
87 Correct 79 ms 6164 KB Output is correct
88 Correct 85 ms 6228 KB Output is correct
89 Correct 84 ms 6088 KB Output is correct
90 Correct 85 ms 6196 KB Output is correct
91 Correct 89 ms 6204 KB Output is correct
92 Correct 96 ms 6200 KB Output is correct