結果

問題 No.5015 Escape from Labyrinth
ユーザー trineutrontrineutron
提出日時 2023-04-13 00:56:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,605 ms / 3,000 ms
コード長 8,729 bytes
コンパイル時間 3,877 ms
コンパイル使用メモリ 218,480 KB
実行使用メモリ 18,880 KB
スコア 65,580
最終ジャッジ日時 2023-04-15 11:41:45
合計ジャッジ時間 85,437 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 987 ms
18,044 KB
testcase_01 AC 870 ms
18,040 KB
testcase_02 AC 696 ms
17,432 KB
testcase_03 AC 601 ms
17,240 KB
testcase_04 AC 948 ms
18,100 KB
testcase_05 AC 321 ms
17,768 KB
testcase_06 AC 1,063 ms
18,692 KB
testcase_07 AC 654 ms
18,356 KB
testcase_08 AC 758 ms
18,468 KB
testcase_09 AC 592 ms
18,116 KB
testcase_10 AC 587 ms
17,964 KB
testcase_11 AC 383 ms
17,180 KB
testcase_12 AC 692 ms
17,376 KB
testcase_13 AC 694 ms
17,848 KB
testcase_14 AC 886 ms
17,528 KB
testcase_15 AC 636 ms
17,928 KB
testcase_16 AC 606 ms
17,636 KB
testcase_17 AC 694 ms
18,296 KB
testcase_18 AC 411 ms
18,296 KB
testcase_19 AC 863 ms
17,728 KB
testcase_20 AC 645 ms
18,208 KB
testcase_21 AC 782 ms
17,936 KB
testcase_22 AC 462 ms
17,676 KB
testcase_23 AC 945 ms
17,856 KB
testcase_24 AC 841 ms
18,060 KB
testcase_25 AC 828 ms
18,256 KB
testcase_26 AC 641 ms
18,056 KB
testcase_27 AC 966 ms
18,636 KB
testcase_28 AC 828 ms
18,112 KB
testcase_29 AC 671 ms
17,720 KB
testcase_30 AC 414 ms
18,536 KB
testcase_31 AC 941 ms
18,532 KB
testcase_32 AC 986 ms
17,700 KB
testcase_33 AC 792 ms
18,188 KB
testcase_34 AC 589 ms
17,924 KB
testcase_35 AC 729 ms
17,148 KB
testcase_36 AC 840 ms
18,228 KB
testcase_37 AC 874 ms
17,700 KB
testcase_38 AC 869 ms
18,296 KB
testcase_39 AC 664 ms
18,468 KB
testcase_40 AC 360 ms
18,296 KB
testcase_41 AC 1,262 ms
18,624 KB
testcase_42 AC 797 ms
18,816 KB
testcase_43 AC 783 ms
18,300 KB
testcase_44 AC 670 ms
17,844 KB
testcase_45 AC 1,316 ms
18,356 KB
testcase_46 AC 487 ms
18,232 KB
testcase_47 AC 770 ms
17,448 KB
testcase_48 AC 896 ms
18,000 KB
testcase_49 AC 690 ms
18,652 KB
testcase_50 AC 680 ms
17,520 KB
testcase_51 AC 917 ms
17,784 KB
testcase_52 AC 995 ms
18,880 KB
testcase_53 AC 835 ms
18,300 KB
testcase_54 AC 630 ms
18,456 KB
testcase_55 AC 1,461 ms
18,212 KB
testcase_56 AC 637 ms
17,588 KB
testcase_57 AC 870 ms
17,624 KB
testcase_58 AC 1,111 ms
18,320 KB
testcase_59 AC 637 ms
18,296 KB
testcase_60 AC 470 ms
17,836 KB
testcase_61 AC 548 ms
17,256 KB
testcase_62 AC 1,057 ms
18,472 KB
testcase_63 AC 554 ms
18,616 KB
testcase_64 AC 944 ms
18,332 KB
testcase_65 AC 680 ms
17,588 KB
testcase_66 AC 744 ms
18,636 KB
testcase_67 AC 541 ms
18,192 KB
testcase_68 AC 1,605 ms
17,980 KB
testcase_69 AC 560 ms
17,836 KB
testcase_70 AC 1,002 ms
18,520 KB
testcase_71 AC 698 ms
18,196 KB
testcase_72 AC 986 ms
18,592 KB
testcase_73 AC 490 ms
17,432 KB
testcase_74 AC 937 ms
17,564 KB
testcase_75 AC 651 ms
17,844 KB
testcase_76 AC 866 ms
18,088 KB
testcase_77 AC 850 ms
17,448 KB
testcase_78 AC 761 ms
17,796 KB
testcase_79 AC 585 ms
17,672 KB
testcase_80 AC 708 ms
17,496 KB
testcase_81 AC 875 ms
17,972 KB
testcase_82 AC 307 ms
16,924 KB
testcase_83 AC 427 ms
18,640 KB
testcase_84 AC 688 ms
17,460 KB
testcase_85 AC 911 ms
17,412 KB
testcase_86 AC 482 ms
18,232 KB
testcase_87 AC 722 ms
18,620 KB
testcase_88 AC 323 ms
17,052 KB
testcase_89 AC 700 ms
17,720 KB
testcase_90 AC 734 ms
17,760 KB
testcase_91 AC 827 ms
18,736 KB
testcase_92 AC 962 ms
18,424 KB
testcase_93 AC 876 ms
18,084 KB
testcase_94 AC 529 ms
17,832 KB
testcase_95 AC 719 ms
17,176 KB
testcase_96 AC 1,126 ms
18,040 KB
testcase_97 AC 368 ms
16,524 KB
testcase_98 AC 746 ms
18,452 KB
testcase_99 AC 834 ms
17,892 KB
権限があれば一括ダウンロードができます

ソースコード

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 D, H;

int ky, kx;
int gy, gx;

int param = 10000;

array<int, 64 * 64 * 64> cost, 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_key[64 * y + x];
        }
        res -= block;
        res *= 10 * D;
        res += param * 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 < 4; action++) {
                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;
    // 入力の受け取り
    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] = distance[64 * ky + kx];
    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);
        }
    }

    vector<int> ans;
    while (not board.escaped) {
        int action = beam_search(board, 100, 4);
        ans.emplace_back(action);
        if (action == -1) {
            ans.clear();
            break;
        }
        board.advance(action);
        board.after();
    }

    if (not board.escaped) {
        param = 100;
        board = Board(S, E);
        while (not board.escaped) {
            int action = beam_search(board, 100, 4);
            if (action == -1) {
                break;
            }
            ans.emplace_back(action);
            board.advance(action);
            board.after();
        }
    }

    for (auto &&action : ans) {
        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;
        }
    }

    return 0;
}
0