結果

問題 No.5006 Hidden Maze
ユーザー t33ft33f
提出日時 2022-06-12 16:04:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 7,573 bytes
コンパイル時間 1,165 ms
実行使用メモリ 22,864 KB
スコア 58,621
平均クエリ数 414.79
最終ジャッジ日時 2022-06-12 16:07:13
合計ジャッジ時間 10,488 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
21,928 KB
testcase_01 AC 27 ms
21,892 KB
testcase_02 AC 17 ms
21,588 KB
testcase_03 AC 19 ms
22,548 KB
testcase_04 AC 22 ms
21,880 KB
testcase_05 AC 82 ms
21,916 KB
testcase_06 AC 27 ms
22,420 KB
testcase_07 AC 77 ms
22,564 KB
testcase_08 AC 81 ms
21,892 KB
testcase_09 AC 80 ms
21,916 KB
testcase_10 AC 21 ms
22,204 KB
testcase_11 AC 29 ms
21,756 KB
testcase_12 AC 26 ms
22,252 KB
testcase_13 AC 25 ms
22,772 KB
testcase_14 AC 41 ms
22,864 KB
testcase_15 AC 30 ms
22,588 KB
testcase_16 AC 76 ms
22,600 KB
testcase_17 AC 21 ms
21,880 KB
testcase_18 AC 56 ms
21,904 KB
testcase_19 AC 62 ms
21,768 KB
testcase_20 AC 76 ms
21,980 KB
testcase_21 AC 26 ms
21,928 KB
testcase_22 AC 19 ms
22,252 KB
testcase_23 AC 22 ms
22,600 KB
testcase_24 AC 28 ms
21,588 KB
testcase_25 AC 56 ms
22,576 KB
testcase_26 AC 85 ms
21,756 KB
testcase_27 AC 56 ms
22,588 KB
testcase_28 AC 75 ms
21,952 KB
testcase_29 AC 72 ms
21,928 KB
testcase_30 AC 23 ms
22,576 KB
testcase_31 AC 26 ms
22,420 KB
testcase_32 AC 42 ms
21,940 KB
testcase_33 AC 26 ms
22,192 KB
testcase_34 AC 22 ms
21,916 KB
testcase_35 AC 27 ms
21,952 KB
testcase_36 AC 25 ms
21,916 KB
testcase_37 AC 33 ms
21,768 KB
testcase_38 AC 63 ms
21,980 KB
testcase_39 AC 22 ms
21,904 KB
testcase_40 AC 19 ms
21,892 KB
testcase_41 AC 23 ms
22,796 KB
testcase_42 AC 25 ms
21,768 KB
testcase_43 AC 46 ms
21,744 KB
testcase_44 AC 22 ms
22,264 KB
testcase_45 AC 34 ms
22,560 KB
testcase_46 AC 29 ms
22,252 KB
testcase_47 AC 25 ms
21,892 KB
testcase_48 AC 75 ms
21,820 KB
testcase_49 AC 87 ms
21,780 KB
testcase_50 AC 78 ms
21,928 KB
testcase_51 AC 23 ms
22,180 KB
testcase_52 AC 21 ms
22,432 KB
testcase_53 AC 77 ms
21,880 KB
testcase_54 AC 50 ms
21,928 KB
testcase_55 AC 21 ms
21,992 KB
testcase_56 AC 24 ms
21,904 KB
testcase_57 AC 83 ms
21,980 KB
testcase_58 AC 78 ms
21,980 KB
testcase_59 AC 31 ms
21,880 KB
testcase_60 AC 20 ms
21,768 KB
testcase_61 AC 25 ms
22,252 KB
testcase_62 AC 28 ms
22,600 KB
testcase_63 AC 20 ms
21,928 KB
testcase_64 AC 80 ms
21,780 KB
testcase_65 AC 27 ms
21,928 KB
testcase_66 AC 19 ms
21,904 KB
testcase_67 AC 28 ms
22,204 KB
testcase_68 AC 54 ms
22,624 KB
testcase_69 AC 61 ms
21,892 KB
testcase_70 AC 80 ms
21,892 KB
testcase_71 AC 21 ms
21,980 KB
testcase_72 AC 38 ms
22,560 KB
testcase_73 AC 84 ms
22,420 KB
testcase_74 AC 30 ms
22,540 KB
testcase_75 AC 74 ms
21,928 KB
testcase_76 AC 80 ms
21,892 KB
testcase_77 AC 78 ms
21,780 KB
testcase_78 AC 46 ms
21,940 KB
testcase_79 AC 79 ms
21,892 KB
testcase_80 AC 25 ms
21,756 KB
testcase_81 AC 23 ms
21,916 KB
testcase_82 AC 23 ms
21,744 KB
testcase_83 AC 20 ms
22,704 KB
testcase_84 AC 22 ms
22,576 KB
testcase_85 AC 79 ms
21,928 KB
testcase_86 AC 41 ms
21,904 KB
testcase_87 AC 28 ms
22,252 KB
testcase_88 AC 41 ms
21,820 KB
testcase_89 AC 75 ms
21,780 KB
testcase_90 AC 81 ms
21,880 KB
testcase_91 AC 20 ms
21,780 KB
testcase_92 AC 30 ms
22,576 KB
testcase_93 AC 28 ms
21,940 KB
testcase_94 AC 66 ms
22,600 KB
testcase_95 AC 55 ms
21,768 KB
testcase_96 AC 18 ms
22,600 KB
testcase_97 AC 77 ms
22,576 KB
testcase_98 AC 68 ms
22,372 KB
testcase_99 AC 41 ms
22,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <numeric>
#include <algorithm>
#include <tuple>
#include <queue>
#include <cmath>
#include <chrono>
#include <cassert>
#include <iostream>
using namespace std;

unsigned int xor128() {
    static unsigned int x=123456789, y=362436069, z=521288629, w=88675123;
    unsigned int t;
    t=(x^(x<<11)); x=y; y=z; z=w;
    return (w=(w^(w>>19))^(t^(t>>8)));
}

inline bool rand_bool(double prob) {
    constexpr double x = 1LL<<32; // uint_max+1
    return xor128() < prob * x;
}

inline bool rand_bool() {
    return xor128() < 1u<<31;
}

inline double rand(double lb, double ub) {
    assert(lb < ub);
    unsigned int x = xor128();
    return lb + (ub - lb) * x / double(1LL<<32);
}

inline int rand_int(int n) { return xor128()%n; }

int timelimit = 2 * 1000, margin = 50;

class Timer {
  chrono::system_clock::time_point start_time = chrono::system_clock::now();
public:
  Timer() {}
  int get_elapsed_time() {
    auto diff = chrono::system_clock::now() - start_time;
    return chrono::duration_cast<chrono::milliseconds>(diff).count();
  }
} timer;


constexpr int H = 20, W = 20;

bool is_valid(int x, int y) {
    return 0 <= x && x < H && 0 <= y && y < W;
}

const char dirs[] = {'D', 'R', 'U', 'L'};
const pair<int, int> dxdy[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

bool is_opposite(char a, char b) {
    if (a == 'D') return b == 'U';
    if (a == 'U') return b == 'D';
    if (a == 'L') return b == 'R';
    assert(a == 'R');
    return b == 'L';
}

pair<int, int> make_move(int i, int j, char c) {
    if (c == 'D') return {i + 1, j};
    if (c == 'U') return {i - 1, j};
    if (c == 'L') return {i, j - 1};
    assert(c == 'R');
    return {i, j + 1};
}

class ReactiveWrapper {
    bool write_log = false;
public:
    void enable_logging() { write_log = true; }
    void disable_logging() { write_log = true; }
    int write(const string &s) {
        cout << s << endl;
        int n; cin >> n;
        if (write_log) { cerr << s << '\n' << n << endl; }
        return n;
    }
};

constexpr int OUTPUT_MAX = 400;

class Solver {
    double wall_rate = 150.0 / 760;
    double fail_prob;
    double wall_prob[2][H][W];
    int fail_count[2][H][W];
    bool passed[2][H][W];
public:
    Solver(int p) : fail_prob(p * 0.01) {
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++) {
                fail_count[0][i][j] = 0;
                fail_count[1][i][j] = 0;
                passed[0][i][j] = false;
                passed[1][i][j] = false;
            }
        update_prob();
    }
    void update_prob() {
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++)
                update_prob(i, j);
    }
    void update_prob(int i, int j) {
        wall_prob[0][i][j] = (passed[0][i][j] ? 0.0 :
                              wall_rate / (wall_rate + pow(fail_prob, fail_count[0][i][j]) * (1 - wall_rate)));
        wall_prob[1][i][j] = (passed[1][i][j] ? 0.0 :
                              wall_rate / (wall_rate + pow(fail_prob, fail_count[1][i][j]) * (1 - wall_rate)));
    }
    string create_path(int turn) {
        int priority[H][W][4];
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++) {
                iota(priority[i][j], priority[i][j] + 4, 0);
                for (int k = 1; k < 4; k++) {
                    const int l = rand_int(k);
                    if (l != k) swap(priority[i][j][k], priority[i][j][l]);
                }
            }

        double dist[H][W];
        tuple<char, int, int> from[H][W];
        priority_queue<tuple<double, int, int> > pq;
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) dist[i][j] = 100;
        dist[0][0] = 0;
        pq.emplace(0, 0, 0);
        while (!pq.empty()) {
            const auto [d, x, y] = pq.top();
            pq.pop();
            if (dist[x][y] < d) continue;
            for (int k = 0; k < 4; k++) {
                const char c = dirs[k];
                const auto [dx, dy] = dxdy[k];
                const int x1 = x + dx, y1 = y + dy;
                if (!is_valid(x1, y1)) continue;
                const double wp = [this, c, x1, y1, x = x, y = y] {
                    switch (c) {
                    case 'L':
                        return wall_prob[0][x1][y1];
                    case 'R':
                        return wall_prob[0][x][y];
                    case 'D':
                        return wall_prob[1][x][y];
                    default:
                        assert(c == 'U');
                        return wall_prob[1][x1][y1];
                    }
                }();
                const double d1 = -d + wp;
                if (dist[x1][y1] > d1) {
                    dist[x1][y1] = d1;
                    pq.emplace(-d1, x1, y1);
                    from[x1][y1] = {c, x, y};
                }
            }
        }
        string out;
        for (int x = H - 1, y = W - 1; x != 0 || y != 0; ) {
            // cerr << x << ' ' << y << ' ' << dist[x][y] << endl;
            assert(is_valid(x, y));
            const auto [c, x1, y1] = from[x][y];
            out.push_back(c);
            x = x1;
            y = y1;
        }
        reverse(out.begin(), out.end());
        return out;
    }
    void run(ReactiveWrapper wrapper) {
        // wrapper.enable_logging();
        for (int turn = 1; ; turn++) {
            string out = create_path(turn);
            const int r = wrapper.write(out);
            if (r == -1) break;
            int x = 0, y = 0;
            for (int i = 0; i < r; i++) {
                switch (out[i]) {
                case 'U':
                    assert(x > 0);
                    x--;
                    passed[1][x][y] = true;
                    wall_prob[1][x][y] = 0;
                    break;
                case 'D':
                    assert(x + 1 < H);
                    passed[1][x][y] = true;
                    wall_prob[1][x][y] = 0;
                    x++;
                    break;
                case 'L':
                    assert(y > 0);
                    y--;
                    passed[0][x][y] = true;
                    wall_prob[0][x][y] = 0;
                    break;
                default:
                    assert(out[i] == 'R');
                    assert(y + 1 < W);
                    passed[0][x][y] = true;
                    wall_prob[0][x][y] = 0;
                    y++;
                }
            }
            if (r < out.size()) {
                switch(out[r]) {
                case 'U':
                    if (x > 0) {
                        fail_count[1][x - 1][y]++;
                        update_prob(x - 1, y);
                    }
                    break;
                case 'D':
                    if (x + 1 < H) {
                        fail_count[1][x][y]++;
                        update_prob(x, y);
                    }
                    break;
                case 'L':
                    if (y > 0) {
                        fail_count[0][x][y - 1]++;
                        update_prob(x, y - 1);
                    }
                    break;
                default:
                    assert(out[r] == 'R');
                    if (y + 1 < W) {
                        fail_count[0][x][y]++;
                        update_prob(x, y);
                    }
                }
            }
        }
    }
};

int main() {
    { int h, w; cin >> h >> w; assert(h == H && w == W); }
    int p;
    cin >> p;
    assert(6 <= p && p <= 15);

    ReactiveWrapper rw;
    Solver s(p);
    s.run(rw);
}
0