結果

問題 No.5006 Hidden Maze
ユーザー t33ft33f
提出日時 2022-06-12 18:51:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 7,998 bytes
コンパイル時間 2,077 ms
実行使用メモリ 22,844 KB
スコア 88,179
平均クエリ数 119.21
最終ジャッジ日時 2022-06-12 18:51:23
合計ジャッジ時間 8,218 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
21,892 KB
testcase_01 AC 33 ms
22,648 KB
testcase_02 AC 21 ms
22,844 KB
testcase_03 AC 23 ms
22,276 KB
testcase_04 AC 25 ms
21,952 KB
testcase_05 AC 25 ms
22,004 KB
testcase_06 AC 18 ms
22,588 KB
testcase_07 AC 22 ms
21,600 KB
testcase_08 AC 87 ms
22,516 KB
testcase_09 AC 25 ms
21,952 KB
testcase_10 AC 18 ms
21,992 KB
testcase_11 AC 20 ms
22,264 KB
testcase_12 AC 21 ms
21,904 KB
testcase_13 AC 18 ms
21,928 KB
testcase_14 AC 58 ms
21,880 KB
testcase_15 AC 20 ms
21,792 KB
testcase_16 AC 22 ms
22,276 KB
testcase_17 AC 21 ms
22,444 KB
testcase_18 AC 27 ms
21,904 KB
testcase_19 AC 17 ms
21,892 KB
testcase_20 AC 18 ms
21,928 KB
testcase_21 AC 19 ms
21,940 KB
testcase_22 AC 17 ms
22,600 KB
testcase_23 AC 20 ms
22,576 KB
testcase_24 AC 20 ms
22,264 KB
testcase_25 AC 20 ms
21,756 KB
testcase_26 AC 86 ms
22,576 KB
testcase_27 AC 34 ms
21,892 KB
testcase_28 AC 21 ms
21,768 KB
testcase_29 AC 34 ms
21,940 KB
testcase_30 AC 20 ms
21,980 KB
testcase_31 AC 17 ms
21,756 KB
testcase_32 AC 22 ms
22,276 KB
testcase_33 AC 22 ms
21,892 KB
testcase_34 AC 18 ms
22,444 KB
testcase_35 AC 18 ms
22,276 KB
testcase_36 AC 20 ms
22,620 KB
testcase_37 AC 22 ms
22,560 KB
testcase_38 AC 21 ms
21,940 KB
testcase_39 AC 25 ms
22,264 KB
testcase_40 AC 19 ms
22,004 KB
testcase_41 AC 20 ms
21,928 KB
testcase_42 AC 19 ms
21,892 KB
testcase_43 AC 23 ms
21,820 KB
testcase_44 AC 17 ms
21,940 KB
testcase_45 AC 21 ms
22,612 KB
testcase_46 AC 89 ms
22,004 KB
testcase_47 AC 18 ms
21,940 KB
testcase_48 AC 18 ms
21,904 KB
testcase_49 AC 84 ms
22,632 KB
testcase_50 AC 19 ms
21,892 KB
testcase_51 AC 18 ms
22,216 KB
testcase_52 AC 26 ms
21,940 KB
testcase_53 AC 32 ms
21,916 KB
testcase_54 AC 20 ms
21,980 KB
testcase_55 AC 23 ms
22,228 KB
testcase_56 AC 27 ms
21,928 KB
testcase_57 AC 57 ms
21,940 KB
testcase_58 AC 17 ms
21,928 KB
testcase_59 AC 19 ms
22,264 KB
testcase_60 AC 18 ms
22,600 KB
testcase_61 AC 17 ms
21,992 KB
testcase_62 AC 18 ms
21,940 KB
testcase_63 AC 22 ms
22,828 KB
testcase_64 AC 21 ms
22,004 KB
testcase_65 AC 18 ms
22,588 KB
testcase_66 AC 18 ms
22,456 KB
testcase_67 AC 18 ms
22,576 KB
testcase_68 AC 24 ms
21,940 KB
testcase_69 AC 20 ms
22,216 KB
testcase_70 AC 17 ms
21,992 KB
testcase_71 AC 20 ms
22,228 KB
testcase_72 AC 18 ms
21,940 KB
testcase_73 AC 22 ms
22,564 KB
testcase_74 AC 21 ms
22,596 KB
testcase_75 AC 28 ms
22,216 KB
testcase_76 AC 18 ms
22,028 KB
testcase_77 AC 23 ms
22,576 KB
testcase_78 AC 19 ms
22,576 KB
testcase_79 AC 21 ms
21,780 KB
testcase_80 AC 18 ms
22,608 KB
testcase_81 AC 22 ms
21,928 KB
testcase_82 AC 18 ms
21,940 KB
testcase_83 AC 17 ms
21,940 KB
testcase_84 AC 59 ms
21,928 KB
testcase_85 AC 44 ms
21,880 KB
testcase_86 AC 18 ms
21,904 KB
testcase_87 AC 21 ms
22,444 KB
testcase_88 AC 21 ms
21,768 KB
testcase_89 AC 71 ms
21,880 KB
testcase_90 AC 20 ms
21,916 KB
testcase_91 AC 18 ms
22,004 KB
testcase_92 AC 19 ms
21,940 KB
testcase_93 AC 19 ms
21,952 KB
testcase_94 AC 19 ms
21,892 KB
testcase_95 AC 19 ms
21,904 KB
testcase_96 AC 25 ms
21,756 KB
testcase_97 AC 37 ms
22,216 KB
testcase_98 AC 22 ms
21,880 KB
testcase_99 AC 18 ms
21,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iomanip>
#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}};

double L2(int x, int y, int z, int w) {
    const int dx = x - z, dy = y - w;
    return sqrt(dx * dx + dy * dy);
}

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

class Solver {
    double wall_rate = 150.0 / 760;
    const 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_wall_rate() {
        double tried = 0, ng = 0;
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++) {
                if (passed[0][i][j]) tried++;
                else if (fail_count[0][i][j] > 0) {
                    tried++;
                    ng += wall_prob[0][i][j];
                }
                if (passed[1][i][j]) tried++;
                else if (fail_count[1][i][j] > 0) {
                    tried++;
                    ng += wall_prob[1][i][j];
                }
            }
        // 大きめに推定される?(サンプルが一様でないので?)
        wall_rate = 0.99 * wall_rate + 0.01 * ng / tried;
    };
    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) const {
        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] = 1e100;
        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];
                    }
                }();
                if (wp > 1 - 1e-3) continue;
                double d1 = -d;
                d1 += 1 / (1 - wp);
                d1 += rand(0, 0.01);
                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]++;
                    }
                    break;
                case 'D':
                    if (x + 1 < H) {
                        fail_count[1][x][y]++;
                    }
                    break;
                case 'L':
                    if (y > 0) {
                        fail_count[0][x][y - 1]++;
                    }
                    break;
                default:
                    assert(out[r] == 'R');
                    if (y + 1 < W) {
                        fail_count[0][x][y]++;
                    }
                }
            }
            // update_wall_rate();
            update_prob();
        }
    }
};

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