답안 #337423

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
337423 2020-12-20T16:59:41 Z arbor Mecho (IOI09_mecho) C++17
100 / 100
183 ms 7020 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 805;
int N, S, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
char a[MN][MN];
int bee[MN][MN], dis[MN][MN];
int sx, sy, ex, ey;

bool good(int t) {
    if (t >= bee[sx][sy]) return false;
    memset(dis, 0x3f, sizeof(dis));
    dis[sx][sy] = t + 1;
    queue<pair<pii, int>> q;
    q.push({{sx, sy}, S});
    while (!q.empty()) {
        auto [p, s] = q.front(); q.pop();
        auto [x, y] = p;
        if (!s && dis[x][y] >= bee[x][y]) continue;
        for (int k = 0; k < 4; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (nx < 1 || nx > N || ny < 1 || ny > N || a[nx][ny] == 'T') continue;
            if (!s) {
                int nd = dis[x][y] + 1;
                if (dis[nx][ny] > nd && nd <= bee[nx][ny]) {
                    dis[nx][ny] = nd;
                    q.push({{nx, ny}, S - 1});
                }
            } else {
                int nd = dis[x][y];
                if (dis[nx][ny] > nd && nd <= bee[nx][ny]) {
                    dis[nx][ny] = nd;
                    q.push({{nx, ny}, s - 1});
                }
            }
        }
    }
    return dis[ex][ey] < 1e9;
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> N >> S;
    queue<pii> q;
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++) {
            cin >> a[i][j];
            bee[i][j] = 1e9;
            if (a[i][j] == 'M') sx = i, sy = j;
            if (a[i][j] == 'D') ex = i, ey = j;
            if (a[i][j] == 'H') q.emplace(i, j), bee[i][j] = 0;
        }
    while (!q.empty()) {
        auto [x, y] = q.front(); q.pop();
        for (int k = 0; k < 4; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (nx < 1 || nx > N || ny < 1 || ny > N || a[nx][ny] == 'T' || a[nx][ny] == 'D') continue;
            if (bee[nx][ny] > bee[x][y] + 1) bee[nx][ny] = bee[x][y] + 1, q.emplace(nx, ny);
        }
    }
    int l = 0, r = 5e5;
    while (l < r) {
        int m = (l + r) / 2;
        if (!good(m)) r = m;
        else l = m + 1;
    }
    cout << l - 1 << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2924 KB Output is correct
2 Correct 2 ms 2924 KB Output is correct
3 Correct 3 ms 2940 KB Output is correct
4 Correct 2 ms 2924 KB Output is correct
5 Correct 2 ms 2924 KB Output is correct
6 Correct 2 ms 2924 KB Output is correct
7 Correct 104 ms 6764 KB Output is correct
8 Correct 2 ms 2924 KB Output is correct
9 Correct 2 ms 2924 KB Output is correct
10 Correct 2 ms 2944 KB Output is correct
11 Correct 2 ms 2924 KB Output is correct
12 Correct 3 ms 3180 KB Output is correct
13 Correct 2 ms 3052 KB Output is correct
14 Correct 3 ms 3180 KB Output is correct
15 Correct 2 ms 2924 KB Output is correct
16 Correct 2 ms 2924 KB Output is correct
17 Correct 2 ms 2924 KB Output is correct
18 Correct 2 ms 2924 KB Output is correct
19 Correct 2 ms 3052 KB Output is correct
20 Correct 2 ms 3052 KB Output is correct
21 Correct 2 ms 3052 KB Output is correct
22 Correct 3 ms 3072 KB Output is correct
23 Correct 3 ms 3052 KB Output is correct
24 Correct 2 ms 3052 KB Output is correct
25 Correct 3 ms 3180 KB Output is correct
26 Correct 3 ms 3180 KB Output is correct
27 Correct 3 ms 3180 KB Output is correct
28 Correct 3 ms 3180 KB Output is correct
29 Correct 3 ms 3200 KB Output is correct
30 Correct 3 ms 3180 KB Output is correct
31 Correct 3 ms 3180 KB Output is correct
32 Correct 3 ms 3212 KB Output is correct
33 Correct 8 ms 4332 KB Output is correct
34 Correct 8 ms 4332 KB Output is correct
35 Correct 29 ms 4332 KB Output is correct
36 Correct 9 ms 4588 KB Output is correct
37 Correct 9 ms 4588 KB Output is correct
38 Correct 36 ms 4588 KB Output is correct
39 Correct 11 ms 4844 KB Output is correct
40 Correct 11 ms 4844 KB Output is correct
41 Correct 46 ms 4844 KB Output is correct
42 Correct 13 ms 5100 KB Output is correct
43 Correct 15 ms 5228 KB Output is correct
44 Correct 59 ms 5100 KB Output is correct
45 Correct 13 ms 5356 KB Output is correct
46 Correct 15 ms 5356 KB Output is correct
47 Correct 72 ms 5356 KB Output is correct
48 Correct 16 ms 5612 KB Output is correct
49 Correct 18 ms 5740 KB Output is correct
50 Correct 86 ms 5612 KB Output is correct
51 Correct 18 ms 5868 KB Output is correct
52 Correct 23 ms 5868 KB Output is correct
53 Correct 101 ms 5868 KB Output is correct
54 Correct 22 ms 6124 KB Output is correct
55 Correct 24 ms 6124 KB Output is correct
56 Correct 116 ms 6124 KB Output is correct
57 Correct 22 ms 6380 KB Output is correct
58 Correct 22 ms 6380 KB Output is correct
59 Correct 137 ms 6400 KB Output is correct
60 Correct 26 ms 6636 KB Output is correct
61 Correct 32 ms 6636 KB Output is correct
62 Correct 158 ms 6636 KB Output is correct
63 Correct 125 ms 6636 KB Output is correct
64 Correct 183 ms 6636 KB Output is correct
65 Correct 176 ms 6636 KB Output is correct
66 Correct 161 ms 6636 KB Output is correct
67 Correct 138 ms 6656 KB Output is correct
68 Correct 59 ms 6764 KB Output is correct
69 Correct 59 ms 6764 KB Output is correct
70 Correct 50 ms 6764 KB Output is correct
71 Correct 42 ms 6764 KB Output is correct
72 Correct 40 ms 6764 KB Output is correct
73 Correct 43 ms 7020 KB Output is correct
74 Correct 78 ms 6892 KB Output is correct
75 Correct 85 ms 6892 KB Output is correct
76 Correct 76 ms 6892 KB Output is correct
77 Correct 83 ms 6912 KB Output is correct
78 Correct 94 ms 7020 KB Output is correct
79 Correct 71 ms 6892 KB Output is correct
80 Correct 75 ms 6892 KB Output is correct
81 Correct 85 ms 6892 KB Output is correct
82 Correct 74 ms 6892 KB Output is correct
83 Correct 102 ms 7020 KB Output is correct
84 Correct 81 ms 6892 KB Output is correct
85 Correct 92 ms 6892 KB Output is correct
86 Correct 95 ms 6904 KB Output is correct
87 Correct 94 ms 6892 KB Output is correct
88 Correct 99 ms 6892 KB Output is correct
89 Correct 98 ms 6892 KB Output is correct
90 Correct 103 ms 6892 KB Output is correct
91 Correct 103 ms 7020 KB Output is correct
92 Correct 98 ms 6764 KB Output is correct