結果

問題 No.5015 Escape from Labyrinth
ユーザー trineutrontrineutron
提出日時 2023-04-13 02:57:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 10,222 bytes
コンパイル時間 2,806 ms
コンパイル使用メモリ 224,692 KB
実行使用メモリ 9,340 KB
スコア 39,460
最終ジャッジ日時 2023-04-15 11:44:06
合計ジャッジ時間 208,083 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,294 ms
8,324 KB
testcase_01 AC 2,136 ms
9,052 KB
testcase_02 AC 2,586 ms
8,504 KB
testcase_03 AC 1,972 ms
8,560 KB
testcase_04 AC 2,346 ms
8,552 KB
testcase_05 AC 1,082 ms
8,980 KB
testcase_06 AC 2,329 ms
8,604 KB
testcase_07 RE -
testcase_08 AC 2,017 ms
8,700 KB
testcase_09 AC 2,107 ms
8,440 KB
testcase_10 AC 2,349 ms
8,132 KB
testcase_11 AC 2,227 ms
8,504 KB
testcase_12 AC 2,343 ms
8,680 KB
testcase_13 AC 2,289 ms
8,416 KB
testcase_14 AC 2,257 ms
8,544 KB
testcase_15 AC 2,521 ms
8,508 KB
testcase_16 AC 2,319 ms
8,588 KB
testcase_17 AC 2,525 ms
8,900 KB
testcase_18 AC 1,925 ms
8,592 KB
testcase_19 AC 2,560 ms
8,728 KB
testcase_20 AC 2,139 ms
8,584 KB
testcase_21 AC 871 ms
9,140 KB
testcase_22 AC 2,518 ms
9,096 KB
testcase_23 AC 1,566 ms
8,872 KB
testcase_24 AC 2,572 ms
8,896 KB
testcase_25 AC 2,041 ms
9,072 KB
testcase_26 AC 988 ms
8,556 KB
testcase_27 AC 2,827 ms
9,160 KB
testcase_28 AC 1,016 ms
9,052 KB
testcase_29 RE -
testcase_30 AC 2,934 ms
8,488 KB
testcase_31 AC 2,103 ms
8,984 KB
testcase_32 AC 2,232 ms
8,832 KB
testcase_33 AC 1,210 ms
8,960 KB
testcase_34 AC 656 ms
8,940 KB
testcase_35 AC 1,090 ms
8,652 KB
testcase_36 AC 2,565 ms
8,424 KB
testcase_37 AC 2,103 ms
8,444 KB
testcase_38 AC 2,437 ms
8,932 KB
testcase_39 AC 1,247 ms
9,164 KB
testcase_40 AC 2,452 ms
8,472 KB
testcase_41 AC 2,511 ms
8,552 KB
testcase_42 AC 958 ms
8,688 KB
testcase_43 AC 2,197 ms
8,492 KB
testcase_44 AC 1,124 ms
8,828 KB
testcase_45 AC 2,388 ms
9,056 KB
testcase_46 AC 2,008 ms
8,284 KB
testcase_47 AC 2,718 ms
8,512 KB
testcase_48 AC 985 ms
9,340 KB
testcase_49 AC 758 ms
9,028 KB
testcase_50 AC 2,194 ms
8,436 KB
testcase_51 AC 2,429 ms
8,140 KB
testcase_52 AC 1,050 ms
8,904 KB
testcase_53 AC 2,717 ms
8,864 KB
testcase_54 AC 2,572 ms
9,144 KB
testcase_55 AC 1,150 ms
8,968 KB
testcase_56 AC 2,186 ms
8,364 KB
testcase_57 AC 1,074 ms
8,908 KB
testcase_58 AC 1,016 ms
8,968 KB
testcase_59 AC 2,502 ms
8,228 KB
testcase_60 AC 2,069 ms
8,476 KB
testcase_61 AC 874 ms
8,540 KB
testcase_62 AC 1,014 ms
8,644 KB
testcase_63 AC 1,082 ms
9,128 KB
testcase_64 AC 2,113 ms
8,784 KB
testcase_65 AC 2,142 ms
8,304 KB
testcase_66 AC 2,696 ms
8,560 KB
testcase_67 AC 2,094 ms
8,760 KB
testcase_68 AC 1,138 ms
9,216 KB
testcase_69 RE -
testcase_70 AC 2,372 ms
8,424 KB
testcase_71 RE -
testcase_72 AC 2,306 ms
8,476 KB
testcase_73 AC 2,281 ms
8,704 KB
testcase_74 AC 2,280 ms
8,372 KB
testcase_75 AC 2,558 ms
8,800 KB
testcase_76 AC 2,219 ms
8,784 KB
testcase_77 AC 1,217 ms
9,084 KB
testcase_78 RE -
testcase_79 AC 2,510 ms
9,108 KB
testcase_80 AC 1,119 ms
8,688 KB
testcase_81 AC 2,544 ms
8,660 KB
testcase_82 AC 2,439 ms
8,756 KB
testcase_83 AC 1,237 ms
9,200 KB
testcase_84 AC 2,341 ms
8,648 KB
testcase_85 AC 2,312 ms
8,204 KB
testcase_86 AC 1,034 ms
8,828 KB
testcase_87 AC 1,102 ms
8,792 KB
testcase_88 AC 2,774 ms
8,584 KB
testcase_89 AC 971 ms
9,048 KB
testcase_90 AC 2,306 ms
9,128 KB
testcase_91 AC 2,555 ms
8,476 KB
testcase_92 AC 2,520 ms
9,232 KB
testcase_93 AC 640 ms
9,104 KB
testcase_94 AC 2,265 ms
8,660 KB
testcase_95 AC 2,056 ms
8,468 KB
testcase_96 AC 2,452 ms
8,984 KB
testcase_97 AC 2,545 ms
8,300 KB
testcase_98 AC 2,422 ms
8,568 KB
testcase_99 AC 1,129 ms
9,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using std::array, std::vector, std::string;
using vertex = std::pair<int, int>;

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";

constexpr int inf = 1e9;

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;

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;
        }
    }

    double evaluate() const {
        if (escaped) return score;
        int dist = distance_key[64 * 64 * turn + 64 * y + x];
        if (key) {
            dist = distance[64 * 64 * turn + 64 * y + x];
        }
        double res = hp - dist;
        if (res <= 0) return -inf;
        return score + (0.1 * hp + 0.0001 * res) / (0.5 * D + 1.0);
    }

    // action: 0 to 12
    // return: is_legal
    bool advance(const int action) {
        turn++;
        if (turn == 60) turn = 0;
        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 < 8) 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;
    // 入力の受け取り
    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);

    cost.fill(1);
    for (int turn = 0; turn < 60; turn++) {
        for (int y = 0; y < N; y++) {
            for (int x = 0; x < N; x++) {
                for (int d = 0; d < 4; d++) {
                    int ny = y + dy[d], nx = x + dx[d];
                    while (not range_out(ny, nx)) {
                        int state = board.board[64 * ny + nx];
                        if (2 <= state and state <= 5) {  // enemy
                            if (turn % state == 0) {
                                cost[64 * 64 * turn + 64 * y + x] += D;
                            }
                            break;
                        }
                        if (state == WALL or state == BLOCK) break;
                        ny += dy[d];
                        nx += dx[d];
                    }
                }
            }
        }
    }

    std::priority_queue<vertex, vector<vertex>, std::greater<vertex>> que;
    distance.fill(inf);
    for (int turn = 0; turn < 60; turn++) {
        que.emplace(0, 64 * 64 * turn + 64 * gy + gx);
    }
    while (not que.empty()) {
        auto [d0, idx] = que.top();
        que.pop();
        if (distance[idx] <= d0) continue;
        distance[idx] = d0;
        int turn = idx / (64 * 64), y0 = idx % (64 * 64) / 64, x0 = idx % 64;
        int state = S[y0][x0];
        if (state == '#' or state == 'E') {
            continue;
        }
        int nt = turn > 0 ? turn - 1 : 59;
        for (int d = 0; d < 4; d++) {
            int ny = y0 + dy[d], nx = x0 + dx[d];
            if (range_out(ny, nx)) continue;
            int n_idx = 64 * 64 * nt + 64 * ny + nx;
            int nd = distance[idx] + cost[idx];
            if (state == 'G') {
                nd = distance[idx];
            }
            if (distance[n_idx] <= nd) continue;
            que.emplace(nd, n_idx);
        }
        que.emplace(distance[idx] + cost[idx], 64 * 64 * nt + 64 * y0 + x0);
    }
    distance_key.fill(inf);
    for (int turn = 0; turn < 60; turn++) {
        que.emplace(distance[64 * 64 * turn + 64 * ky + kx],
                    64 * 64 * turn + 64 * ky + kx);
    }
    while (not que.empty()) {
        auto [d0, idx] = que.top();
        que.pop();
        if (distance_key[idx] <= d0) continue;
        distance_key[idx] = d0;
        int turn = idx / (64 * 64), y0 = idx % (64 * 64) / 64, x0 = idx % 64;
        int state = S[y0][x0];
        if (state == '#' or state == 'E') {
            continue;
        }
        int nt = turn > 0 ? turn - 1 : 59;
        for (int d = 0; d < 4; d++) {
            int ny = y0 + dy[d], nx = x0 + dx[d];
            if (range_out(ny, nx)) continue;
            int n_idx = 64 * 64 * nt + 64 * ny + nx;
            int nd = distance_key[idx] + cost[idx];
            if (distance_key[n_idx] <= nd) continue;
            que.emplace(nd, n_idx);
        }
        que.emplace(distance_key[idx] + cost[idx], 64 * 64 * nt + 64 * y0 + x0);
    }

    vector<int> ans;
    while (not board.escaped) {
        int action = beam_search(board, 5, 5);
        ans.emplace_back(action);
        assert(action != -1);
        board.advance(action);
        board.after();
        // if (board.turn % 60 == 0) std::cerr << board.evaluate() << std::endl;
    }

    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