結果

問題 No.5015 Escape from Labyrinth
ユーザー trineutrontrineutron
提出日時 2023-04-15 00:37:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 10,091 bytes
コンパイル時間 3,159 ms
実行使用メモリ 819,200 KB
スコア 0
最終ジャッジ日時 2023-04-15 11:41:53
合計ジャッジ時間 6,495 ms
ジャッジサーバーID
(参考情報)
judge15 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
testcase_93 -- -
testcase_94 -- -
testcase_95 -- -
testcase_96 -- -
testcase_97 -- -
testcase_98 -- -
testcase_99 -- -
権限があれば一括ダウンロードができます

ソースコード

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<int8_t, 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;
    vector<int8_t> actions;

    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;
            auto &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();
}

vector<int8_t> chokudai_search(const Board &board) {
    array<std::priority_queue<Board>, max_hp + 1> beam;
    Board best_board{board};
    int best = -1;

    beam[0].emplace(board);
    while (true) {
        for (int t = 0; t < max_hp; t++) {
            if (beam[t].empty()) continue;
            Board board_now{beam[t].top()};
            beam[t].pop();
            if (board_now.escaped) {
                if (board_now.score > best) {
                    best = board_now.score;
                    best_board = board_now;
                }
                break;
            }
            for (int action = 0; action < 13; action++) {
                if (elapsed() >= 2000000000) {
                    return best_board.actions;
                }
                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;
                board_next.actions.push_back(action);
                beam[t + 1].emplace(board_next);
            }
        }
    }
    return best_board.actions;
}

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<int8_t> ans = chokudai_search(board);

    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