답안 #608804

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
608804 2022-07-27T10:17:47 Z Mazaalai Mecho (IOI09_mecho) C++17
100 / 100
318 ms 5772 KB
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define LINE "---------------\n"
#define sz(a) (int)a.size()
using namespace std;
using PII = pair <int, int>;
const int N = 802;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m, X1, Y1, X2, Y2;
vector <PII> hives;
int mat[N][N], maze[N][N];
bool check(int lim) {
    memcpy(maze, mat, sizeof(mat));
    queue <PII> bfs1, bfs2;
    for (auto& [a, b] : hives) bfs2.push({a, b});
    for (int i = 0; i < lim; i++) {
        int rep = sz(bfs2);
        for (int j = 0; j < rep; j++) {
            int x, y; tie(x, y) = bfs2.front(); bfs2.pop();
            for (int a = 0; a < 4; a++) {
                int xx = dx[a] + x;
                int yy = dy[a] + y;
                if (xx < 1 || xx > n || yy < 1 || yy > n || maze[xx][yy] != 0) continue;
                maze[xx][yy] = 1;
                bfs2.push({xx, yy});
            }
        }
    }
    if (maze[X1][Y1] == 1) return 0;
    maze[X1][Y1] = 2;
    bfs1.push({X1, Y1});
    bool changed = 1;
    // cout << "START\n";
    // cout << lim << '\n';
    // for (int i = 1; i <= n; i++)
    // for (int j = 1; j <= n; j++) cout << maze[i][j] << " \n"[j==n];
            

    while (maze[X2][Y2] == 4 && changed) {
        // move bear
        changed = 0;
        for (int i = 0; i < m; i++) {
            int rep = sz(bfs1);
            for (int j = 0; j < rep; j++) {
                int x, y; tie(x, y) = bfs1.front(); bfs1.pop();
                if (maze[x][y] == 1) continue;
                for (int a = 0; a < 4; a++) {
                    int xx = dx[a] + x;
                    int yy = dy[a] + y;
                    if (xx < 1 || xx > n || yy < 1 || yy > n || maze[xx][yy] == 1 || maze[xx][yy] == 2) continue;
                    changed = 1;
                    maze[xx][yy] = 2;
                    bfs1.push({xx, yy});
                }
            }
        }
        if (maze[X2][Y2] != 4) return 1;
        // spread bees
        int rep = sz(bfs2);
        for (int j = 0; j < rep; j++) {
            int x, y; tie(x, y) = bfs2.front(); bfs2.pop();
            for (int a = 0; a < 4; a++) {
                int xx = dx[a] + x;
                int yy = dy[a] + y;
                if (xx < 1 || xx > n || yy < 1 || yy > n || maze[xx][yy] == 1 || maze[xx][yy] == 4) continue;

                changed = 1;
                maze[xx][yy] = 1;
                bfs2.push({xx, yy});
            }
        }
        // cout << LINE;
        // for (int i = 1; i <= n; i++)
        // for (int j = 1; j <= n; j++) cout << maze[i][j] << " \n"[j==n];
                
    }
    return 0;
}
signed main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // freopen("0.in", "r", stdin);
    // freopen("0.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
        char x; cin >> x;
        if (x == 'T') { // tree
            mat[i][j] = 1;
        } else if (x == 'G') { // grass
            mat[i][j] = 0;
        } else if (x == 'M') { // mecho the bear
            tie(X1, Y1) = mp(i, j);
            mat[i][j] = 0;
        } else if (x == 'D') { // home of the mecho
            mat[i][j] = 4;
            tie(X2, Y2) = mp(i, j);
        } else if (x == 'H') { // bee hive
            mat[i][j] = 1;
            hives.pb({i, j});
        }
    }
    int l = 0, r = n*n/2, ans = -1;
    while (l <= r) {
        int m = (l+r)>>1;
        if (check(m)) {
            l = m+1;
            ans = m;
        } else {
            r = m-1;
        }
    }
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 3 ms 2772 KB Output is correct
7 Correct 268 ms 5468 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2776 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 5 ms 3028 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 4 ms 3028 KB Output is correct
15 Correct 3 ms 2904 KB Output is correct
16 Correct 2 ms 2900 KB Output is correct
17 Correct 2 ms 2900 KB Output is correct
18 Correct 3 ms 2900 KB Output is correct
19 Correct 3 ms 2900 KB Output is correct
20 Correct 3 ms 2900 KB Output is correct
21 Correct 3 ms 2900 KB Output is correct
22 Correct 3 ms 2900 KB Output is correct
23 Correct 4 ms 2900 KB Output is correct
24 Correct 3 ms 2900 KB Output is correct
25 Correct 4 ms 2940 KB Output is correct
26 Correct 3 ms 3028 KB Output is correct
27 Correct 3 ms 3028 KB Output is correct
28 Correct 3 ms 3052 KB Output is correct
29 Correct 3 ms 3028 KB Output is correct
30 Correct 3 ms 3028 KB Output is correct
31 Correct 3 ms 3028 KB Output is correct
32 Correct 3 ms 3028 KB Output is correct
33 Correct 23 ms 3940 KB Output is correct
34 Correct 18 ms 3924 KB Output is correct
35 Correct 42 ms 3924 KB Output is correct
36 Correct 32 ms 3980 KB Output is correct
37 Correct 50 ms 4100 KB Output is correct
38 Correct 54 ms 4108 KB Output is correct
39 Correct 37 ms 4180 KB Output is correct
40 Correct 32 ms 4180 KB Output is correct
41 Correct 73 ms 4260 KB Output is correct
42 Correct 44 ms 4308 KB Output is correct
43 Correct 39 ms 4396 KB Output is correct
44 Correct 100 ms 4408 KB Output is correct
45 Correct 53 ms 4488 KB Output is correct
46 Correct 49 ms 4564 KB Output is correct
47 Correct 102 ms 4564 KB Output is correct
48 Correct 64 ms 4712 KB Output is correct
49 Correct 55 ms 4716 KB Output is correct
50 Correct 129 ms 4728 KB Output is correct
51 Correct 74 ms 4864 KB Output is correct
52 Correct 66 ms 4864 KB Output is correct
53 Correct 140 ms 4880 KB Output is correct
54 Correct 86 ms 4948 KB Output is correct
55 Correct 77 ms 5024 KB Output is correct
56 Correct 182 ms 5028 KB Output is correct
57 Correct 100 ms 5180 KB Output is correct
58 Correct 97 ms 5076 KB Output is correct
59 Correct 284 ms 5188 KB Output is correct
60 Correct 119 ms 5352 KB Output is correct
61 Correct 100 ms 5236 KB Output is correct
62 Correct 241 ms 5344 KB Output is correct
63 Correct 261 ms 5452 KB Output is correct
64 Correct 258 ms 5344 KB Output is correct
65 Correct 254 ms 5348 KB Output is correct
66 Correct 255 ms 5392 KB Output is correct
67 Correct 263 ms 5452 KB Output is correct
68 Correct 229 ms 5372 KB Output is correct
69 Correct 238 ms 5372 KB Output is correct
70 Correct 222 ms 5380 KB Output is correct
71 Correct 239 ms 5376 KB Output is correct
72 Correct 234 ms 5388 KB Output is correct
73 Correct 189 ms 5648 KB Output is correct
74 Correct 234 ms 5536 KB Output is correct
75 Correct 259 ms 5772 KB Output is correct
76 Correct 246 ms 5648 KB Output is correct
77 Correct 257 ms 5644 KB Output is correct
78 Correct 318 ms 5736 KB Output is correct
79 Correct 252 ms 5588 KB Output is correct
80 Correct 261 ms 5612 KB Output is correct
81 Correct 264 ms 5608 KB Output is correct
82 Correct 276 ms 5608 KB Output is correct
83 Correct 283 ms 5696 KB Output is correct
84 Correct 259 ms 5576 KB Output is correct
85 Correct 275 ms 5588 KB Output is correct
86 Correct 305 ms 5572 KB Output is correct
87 Correct 260 ms 5576 KB Output is correct
88 Correct 300 ms 5636 KB Output is correct
89 Correct 266 ms 5524 KB Output is correct
90 Correct 270 ms 5508 KB Output is correct
91 Correct 267 ms 5460 KB Output is correct
92 Correct 281 ms 5512 KB Output is correct