結果

問題 No.5015 Escape from Labyrinth
ユーザー trineutrontrineutron
提出日時 2023-04-10 03:48:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,255 bytes
コンパイル時間 4,132 ms
コンパイル使用メモリ 217,996 KB
実行使用メモリ 20,004 KB
スコア 62,450
最終ジャッジ日時 2023-04-15 11:40:38
合計ジャッジ時間 90,301 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,085 ms
15,944 KB
testcase_01 AC 865 ms
15,748 KB
testcase_02 AC 1,287 ms
16,492 KB
testcase_03 AC 370 ms
14,184 KB
testcase_04 AC 1,054 ms
15,264 KB
testcase_05 AC 434 ms
15,876 KB
testcase_06 AC 1,175 ms
16,432 KB
testcase_07 AC 590 ms
16,760 KB
testcase_08 AC 829 ms
16,548 KB
testcase_09 AC 838 ms
15,968 KB
testcase_10 AC 587 ms
16,020 KB
testcase_11 AC 718 ms
16,040 KB
testcase_12 AC 752 ms
15,284 KB
testcase_13 AC 716 ms
15,900 KB
testcase_14 AC 841 ms
15,488 KB
testcase_15 AC 632 ms
14,692 KB
testcase_16 AC 768 ms
15,596 KB
testcase_17 AC 1,162 ms
16,608 KB
testcase_18 AC 435 ms
16,328 KB
testcase_19 AC 960 ms
15,704 KB
testcase_20 AC 752 ms
16,268 KB
testcase_21 AC 672 ms
15,896 KB
testcase_22 AC 465 ms
15,988 KB
testcase_23 AC 1,137 ms
16,280 KB
testcase_24 AC 785 ms
15,436 KB
testcase_25 AC 832 ms
16,284 KB
testcase_26 AC 783 ms
15,748 KB
testcase_27 AC 1,065 ms
16,712 KB
testcase_28 AC 1,126 ms
16,320 KB
testcase_29 AC 489 ms
15,724 KB
testcase_30 AC 929 ms
16,280 KB
testcase_31 AC 959 ms
16,520 KB
testcase_32 AC 866 ms
15,848 KB
testcase_33 AC 736 ms
16,164 KB
testcase_34 AC 666 ms
15,728 KB
testcase_35 AC 646 ms
15,520 KB
testcase_36 AC 898 ms
15,808 KB
testcase_37 AC 766 ms
15,816 KB
testcase_38 AC 1,156 ms
16,044 KB
testcase_39 AC 567 ms
16,252 KB
testcase_40 AC 351 ms
16,280 KB
testcase_41 AC 1,272 ms
16,492 KB
testcase_42 AC 809 ms
16,952 KB
testcase_43 AC 589 ms
16,348 KB
testcase_44 AC 1,251 ms
16,340 KB
testcase_45 AC 1,375 ms
16,544 KB
testcase_46 AC 635 ms
16,428 KB
testcase_47 AC 752 ms
15,484 KB
testcase_48 AC 982 ms
16,068 KB
testcase_49 AC 865 ms
16,212 KB
testcase_50 AC 1,056 ms
16,784 KB
testcase_51 AC 1,011 ms
16,080 KB
testcase_52 AC 600 ms
16,208 KB
testcase_53 AC 776 ms
16,424 KB
testcase_54 AC 584 ms
16,308 KB
testcase_55 AC 1,027 ms
16,516 KB
testcase_56 AC 728 ms
15,752 KB
testcase_57 AC 1,482 ms
16,232 KB
testcase_58 WA -
testcase_59 AC 653 ms
15,948 KB
testcase_60 AC 541 ms
15,836 KB
testcase_61 AC 429 ms
14,956 KB
testcase_62 AC 959 ms
16,596 KB
testcase_63 AC 483 ms
16,544 KB
testcase_64 AC 867 ms
16,428 KB
testcase_65 AC 691 ms
15,484 KB
testcase_66 AC 706 ms
16,284 KB
testcase_67 AC 728 ms
16,164 KB
testcase_68 AC 1,183 ms
15,944 KB
testcase_69 AC 694 ms
15,704 KB
testcase_70 AC 812 ms
16,488 KB
testcase_71 AC 1,175 ms
16,336 KB
testcase_72 AC 840 ms
16,604 KB
testcase_73 AC 816 ms
15,828 KB
testcase_74 AC 682 ms
16,336 KB
testcase_75 AC 1,173 ms
15,744 KB
testcase_76 AC 818 ms
15,544 KB
testcase_77 AC 880 ms
15,492 KB
testcase_78 AC 736 ms
15,836 KB
testcase_79 AC 676 ms
15,680 KB
testcase_80 AC 725 ms
15,420 KB
testcase_81 AC 998 ms
16,080 KB
testcase_82 AC 326 ms
15,508 KB
testcase_83 AC 468 ms
16,540 KB
testcase_84 AC 655 ms
16,012 KB
testcase_85 AC 747 ms
16,416 KB
testcase_86 AC 537 ms
16,208 KB
testcase_87 AC 602 ms
16,628 KB
testcase_88 AC 313 ms
14,812 KB
testcase_89 AC 767 ms
16,336 KB
testcase_90 AC 683 ms
15,728 KB
testcase_91 AC 822 ms
17,048 KB
testcase_92 AC 866 ms
15,988 KB
testcase_93 AC 790 ms
15,284 KB
testcase_94 AC 555 ms
15,684 KB
testcase_95 TLE -
testcase_96 -- -
testcase_97 -- -
testcase_98 -- -
testcase_99 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using std::array, std::vector, std::string;

auto start = std::chrono::steady_clock::now();

auto elapsed() { return (std::chrono::steady_clock::now() - start).count(); }

constexpr int N = 60;         // 迷宮の大きさ
constexpr int max_hp = 1500;  // 初期体力
constexpr int dy[4] = {-1, 1, 0, 0};
constexpr int dx[4] = {0, 0, -1, 1};
constexpr auto dir = "UDLR";

enum { EMPTY, BLOCK, E2, E3, E4, E5, WALL, JEWELL, FIRE, KEY, GOAL };

// 範囲外かどうか
bool range_out(size_t y, size_t x) { return x >= N or y >= N; }

// 探知機の情報
struct enemy {
    int y, x, d;
    enemy(int y, int x, int d) : y(y), x(x), d(d) {}
};

int ky, kx;
int gy, gx;

array<int, 64 * 64> distance, distance_key;

// 盤面の情報
struct Board {
    array<int, 64 * 64> board;
    int score = 0;
    int fire = 0;
    int hp = max_hp;
    int block = 0;
    bool key = false;
    bool escaped = false;
    int y, x;
    int turn = 0;
    int first_action = -1;

    Board(const vector<string> &S, const vector<enemy> &E) {
        board.fill(EMPTY);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                const int idx = 64 * i + j;
                switch (S[i][j]) {
                    case '#':
                        board[idx] = WALL;
                        break;
                    case 'J':
                        board[idx] = JEWELL;
                        break;
                    case 'F':
                        board[idx] = FIRE;
                        break;
                    case 'K':
                        board[idx] = KEY;
                        ky = i;
                        kx = j;
                        break;
                    case 'G':
                        board[idx] = GOAL;
                        gy = i;
                        gx = j;
                        break;
                    case 'S':
                        y = i;
                        x = j;
                        break;
                }
            }
        }
        for (const auto &[i, j, d] : E) {
            board[64 * i + j] = d;
        }
    }

    int evaluate() const {
        int res = 0;
        if (key) {
            res -= distance[64 * y + x];
        } else {
            res -= distance[64 * ky + kx] + distance_key[64 * y + x];
        }
        res -= block;
        res *= 2;
        res += 10000 * score + hp;
        return res;
    }

    // action: 0 to 12
    // return: is_legal
    bool advance(const int action) {
        turn++;
        const int d = action & 3;
        int ny = y + dy[d], nx = x + dx[d];
        if (action < 4) {  // move
            if (range_out(ny, nx)) return false;
            int &state = board[64 * ny + nx];
            y = ny;
            x = nx;
            if (state == WALL or state == BLOCK or
                (2 <= state and state <= 5)) {
                return false;
            } else if (state == KEY) {
                key = true;
                state = EMPTY;
            } else if (state == JEWELL) {
                score++;
                state = EMPTY;
            } else if (state == FIRE) {
                fire++;
                state = EMPTY;
            } else if (state == GOAL and key) {
                escaped = true;
            }
            return true;
        } else if (action < 8) {  // block
            if (range_out(ny, nx)) return false;
            if (board[64 * ny + nx] >= 2) return false;
            block -= board[64 * ny + nx];
            board[64 * ny + nx] = 1 - board[64 * ny + nx];
            block += board[64 * ny + nx];
            return true;
        } else if (action < 12) {  // fire
            if (fire == 0) return false;
            while (not range_out(ny, nx)) {
                int state = board[64 * ny + nx];
                if (2 <= state and state <= 5) {  // enemy
                    board[64 * ny + nx] = EMPTY;
                    fire--;
                    return true;
                } else if (state == WALL or state == BLOCK) {
                    return false;
                }
                ny += dy[d];
                nx += dx[d];
            }
            return false;
        } else {
            return true;
        }
    }

    void after() {
        for (int d = 0; d < 4; d++) {
            int y_next = y + dy[d], x_next = x + dx[d];
            while (not range_out(y_next, x_next)) {
                int state = board[64 * y_next + x_next];
                if (2 <= state and state <= 5) {  // enemy
                    if (turn % state == 0) hp -= d;
                    break;
                }
                if (state == WALL or state == BLOCK) break;
                y_next += dy[d];
                x_next += dx[d];
            }
        }
        hp--;
    }
};

bool operator<(const Board &a, const Board &b) {
    return a.evaluate() < b.evaluate();
}

int beam_search(const Board &board, const int beam_width,
                const int beam_depth) {
    std::priority_queue<Board> beam;
    Board best_board{board};

    beam.emplace(board);
    for (int t = 0; t < beam_depth; t++) {
        std::priority_queue<Board> next_beam;
        for (int i = 0; i < beam_width; i++) {
            if (beam.empty()) break;
            Board board_now{beam.top()};
            beam.pop();
            for (int action = 0; action < 13; action++) {
                if (4 <= action and action < 13) continue;
                Board board_next{board_now};
                if (not board_next.advance(action)) continue;
                if (not board_next.escaped) {
                    board_next.after();
                }
                if (board_next.hp <= 0) continue;
                if (t == 0) {
                    board_next.first_action = action;
                }
                next_beam.emplace(board_next);
            }
        }

        beam = next_beam;
        best_board = beam.top();

        if (best_board.escaped) break;
    }

    return best_board.first_action;
}

int main() {
    // 入力の受け取り
    int N, D, H;
    std::cin >> N >> D >> H;
    vector<string> S(N);
    for (int i = 0; i < N; i++) std::cin >> S[i];
    int M;
    std::cin >> M;
    vector<enemy> E;
    for (int i = 0; i < M; i++) {
        int y, x, d;
        std::cin >> y >> x >> d;
        E.emplace_back(y, x, d);
    }

    Board board(S, E);

    std::queue<int> que;
    distance.fill(N * N);
    distance[64 * gy + gx] = 0;
    que.push(64 * gy + gx);
    while (not que.empty()) {
        int idx = que.front();
        que.pop();
        int y0 = idx / 64, x0 = idx % 64;
        for (int d = 0; d < 4; d++) {
            int ny = y0 + dy[d], nx = x0 + dx[d];
            if (range_out(ny, nx)) continue;
            int state = S[ny][nx];
            if (state == '#' or state == 'E') {
                continue;
            }
            if (distance[64 * ny + nx] != N * N) continue;
            distance[64 * ny + nx] = distance[idx] + 1;
            que.push(64 * ny + nx);
        }
    }
    distance_key.fill(N * N);
    distance_key[64 * ky + kx] = 0;
    que.push(64 * ky + kx);
    while (not que.empty()) {
        int idx = que.front();
        que.pop();
        int y0 = idx / 64, x0 = idx % 64;
        for (int d = 0; d < 4; d++) {
            int ny = y0 + dy[d], nx = x0 + dx[d];
            if (range_out(ny, nx)) continue;
            int state = S[ny][nx];
            if (state == '#' or state == 'E') {
                continue;
            }
            if (distance_key[64 * ny + nx] != N * N) continue;
            distance_key[64 * ny + nx] = distance_key[idx] + 1;
            que.push(64 * ny + nx);
        }
    }

    while (not board.escaped) {
        int action = beam_search(board, 100, 4);
        if (action == -1) break;
        if (action < 4) {
            std::cout << "M " << dir[action] << std::endl;
        } else if (action < 8) {
            std::cout << "B " << dir[action & 3] << std::endl;
        } else if (action < 12) {
            std::cout << "F " << dir[action & 3] << std::endl;
        } else {
            std::cout << "S" << std::endl;
        }
        board.advance(action);
        board.after();
    }

    return 0;
}
0