結果

問題 No.5015 Escape from Labyrinth
ユーザー trineutrontrineutron
提出日時 2023-04-10 03:33:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 800 ms / 3,000 ms
コード長 8,069 bytes
コンパイル時間 2,790 ms
コンパイル使用メモリ 216,968 KB
実行使用メモリ 8,840 KB
スコア 55,560
最終ジャッジ日時 2023-04-15 11:38:42
合計ジャッジ時間 52,167 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 608 ms
8,244 KB
testcase_01 AC 511 ms
8,352 KB
testcase_02 AC 438 ms
8,460 KB
testcase_03 AC 328 ms
8,372 KB
testcase_04 AC 521 ms
8,492 KB
testcase_05 AC 287 ms
8,552 KB
testcase_06 AC 482 ms
7,980 KB
testcase_07 AC 403 ms
8,628 KB
testcase_08 AC 303 ms
8,000 KB
testcase_09 AC 633 ms
8,504 KB
testcase_10 AC 273 ms
7,796 KB
testcase_11 AC 243 ms
8,188 KB
testcase_12 AC 463 ms
8,232 KB
testcase_13 AC 364 ms
8,636 KB
testcase_14 AC 459 ms
8,380 KB
testcase_15 AC 418 ms
8,496 KB
testcase_16 AC 382 ms
8,648 KB
testcase_17 AC 488 ms
8,760 KB
testcase_18 AC 237 ms
8,264 KB
testcase_19 AC 365 ms
8,508 KB
testcase_20 AC 390 ms
8,508 KB
testcase_21 AC 600 ms
8,504 KB
testcase_22 AC 244 ms
7,652 KB
testcase_23 AC 545 ms
8,492 KB
testcase_24 AC 456 ms
8,564 KB
testcase_25 AC 506 ms
8,504 KB
testcase_26 AC 406 ms
8,440 KB
testcase_27 AC 476 ms
8,512 KB
testcase_28 AC 608 ms
8,784 KB
testcase_29 AC 353 ms
8,480 KB
testcase_30 AC 347 ms
8,380 KB
testcase_31 AC 520 ms
8,496 KB
testcase_32 AC 579 ms
8,476 KB
testcase_33 AC 421 ms
8,188 KB
testcase_34 AC 474 ms
8,424 KB
testcase_35 AC 397 ms
8,520 KB
testcase_36 AC 588 ms
8,552 KB
testcase_37 AC 524 ms
8,232 KB
testcase_38 AC 561 ms
8,500 KB
testcase_39 AC 358 ms
8,516 KB
testcase_40 AC 193 ms
8,540 KB
testcase_41 AC 696 ms
8,564 KB
testcase_42 AC 467 ms
8,232 KB
testcase_43 AC 269 ms
8,396 KB
testcase_44 AC 529 ms
8,516 KB
testcase_45 AC 505 ms
8,460 KB
testcase_46 AC 420 ms
8,476 KB
testcase_47 AC 415 ms
8,444 KB
testcase_48 AC 626 ms
8,448 KB
testcase_49 AC 433 ms
8,228 KB
testcase_50 AC 403 ms
8,360 KB
testcase_51 AC 420 ms
8,640 KB
testcase_52 AC 653 ms
8,552 KB
testcase_53 AC 410 ms
8,668 KB
testcase_54 AC 537 ms
8,580 KB
testcase_55 AC 747 ms
8,340 KB
testcase_56 AC 316 ms
8,380 KB
testcase_57 AC 558 ms
8,540 KB
testcase_58 AC 584 ms
8,512 KB
testcase_59 AC 381 ms
8,840 KB
testcase_60 AC 353 ms
8,500 KB
testcase_61 AC 291 ms
8,208 KB
testcase_62 AC 543 ms
8,312 KB
testcase_63 AC 290 ms
8,608 KB
testcase_64 AC 452 ms
8,528 KB
testcase_65 AC 292 ms
8,588 KB
testcase_66 AC 344 ms
8,476 KB
testcase_67 AC 265 ms
8,452 KB
testcase_68 AC 800 ms
8,532 KB
testcase_69 AC 314 ms
8,784 KB
testcase_70 AC 532 ms
8,364 KB
testcase_71 AC 405 ms
8,180 KB
testcase_72 AC 449 ms
8,504 KB
testcase_73 AC 345 ms
8,192 KB
testcase_74 AC 311 ms
8,644 KB
testcase_75 AC 548 ms
8,772 KB
testcase_76 AC 544 ms
8,496 KB
testcase_77 AC 366 ms
8,576 KB
testcase_78 AC 433 ms
8,492 KB
testcase_79 AC 278 ms
8,552 KB
testcase_80 AC 449 ms
8,552 KB
testcase_81 AC 557 ms
8,568 KB
testcase_82 AC 252 ms
8,268 KB
testcase_83 AC 342 ms
8,528 KB
testcase_84 AC 245 ms
8,548 KB
testcase_85 AC 445 ms
8,312 KB
testcase_86 AC 277 ms
8,424 KB
testcase_87 AC 365 ms
8,276 KB
testcase_88 AC 188 ms
8,204 KB
testcase_89 AC 432 ms
8,460 KB
testcase_90 AC 386 ms
8,580 KB
testcase_91 AC 463 ms
8,432 KB
testcase_92 AC 581 ms
8,632 KB
testcase_93 AC 311 ms
8,360 KB
testcase_94 AC 312 ms
8,504 KB
testcase_95 AC 347 ms
8,376 KB
testcase_96 AC 431 ms
8,640 KB
testcase_97 AC 235 ms
7,444 KB
testcase_98 AC 423 ms
8,604 KB
testcase_99 AC 648 ms
8,596 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 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;
    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 *= 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;
            board[64 * ny + nx] = 1 - 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, 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, 20, 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