結果

問題 No.5006 Hidden Maze
ユーザー t33ft33f
提出日時 2022-06-12 17:35:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 9,148 bytes
コンパイル時間 1,699 ms
実行使用メモリ 22,880 KB
スコア 77,712
平均クエリ数 223.88
最終ジャッジ日時 2022-06-12 17:35:23
合計ジャッジ時間 10,543 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
21,880 KB
testcase_01 AC 29 ms
21,892 KB
testcase_02 AC 25 ms
21,940 KB
testcase_03 AC 28 ms
21,928 KB
testcase_04 AC 31 ms
21,992 KB
testcase_05 AC 50 ms
22,456 KB
testcase_06 AC 36 ms
22,228 KB
testcase_07 AC 40 ms
22,588 KB
testcase_08 AC 36 ms
21,892 KB
testcase_09 AC 99 ms
21,904 KB
testcase_10 AC 25 ms
21,940 KB
testcase_11 AC 23 ms
21,916 KB
testcase_12 AC 27 ms
22,444 KB
testcase_13 AC 30 ms
21,928 KB
testcase_14 AC 23 ms
21,916 KB
testcase_15 AC 26 ms
21,940 KB
testcase_16 AC 23 ms
21,836 KB
testcase_17 AC 50 ms
22,004 KB
testcase_18 AC 39 ms
21,904 KB
testcase_19 AC 24 ms
22,576 KB
testcase_20 AC 21 ms
22,004 KB
testcase_21 AC 23 ms
22,564 KB
testcase_22 AC 20 ms
22,444 KB
testcase_23 AC 27 ms
21,952 KB
testcase_24 AC 31 ms
21,904 KB
testcase_25 AC 31 ms
21,904 KB
testcase_26 AC 102 ms
21,600 KB
testcase_27 AC 79 ms
21,940 KB
testcase_28 AC 32 ms
21,880 KB
testcase_29 AC 100 ms
21,904 KB
testcase_30 AC 25 ms
21,780 KB
testcase_31 AC 25 ms
22,276 KB
testcase_32 AC 38 ms
22,608 KB
testcase_33 AC 30 ms
21,916 KB
testcase_34 AC 20 ms
21,992 KB
testcase_35 AC 28 ms
21,916 KB
testcase_36 AC 45 ms
22,004 KB
testcase_37 AC 83 ms
21,904 KB
testcase_38 AC 42 ms
21,880 KB
testcase_39 AC 42 ms
21,892 KB
testcase_40 AC 21 ms
21,904 KB
testcase_41 AC 25 ms
21,928 KB
testcase_42 AC 21 ms
22,204 KB
testcase_43 AC 33 ms
21,992 KB
testcase_44 AC 22 ms
22,468 KB
testcase_45 AC 29 ms
22,500 KB
testcase_46 AC 104 ms
21,940 KB
testcase_47 AC 29 ms
21,792 KB
testcase_48 AC 97 ms
21,952 KB
testcase_49 AC 94 ms
22,576 KB
testcase_50 AC 24 ms
22,432 KB
testcase_51 AC 25 ms
21,824 KB
testcase_52 AC 32 ms
22,004 KB
testcase_53 AC 41 ms
22,624 KB
testcase_54 AC 39 ms
22,468 KB
testcase_55 AC 33 ms
22,264 KB
testcase_56 AC 43 ms
21,940 KB
testcase_57 AC 31 ms
22,228 KB
testcase_58 AC 42 ms
21,904 KB
testcase_59 AC 22 ms
21,992 KB
testcase_60 AC 22 ms
22,608 KB
testcase_61 AC 24 ms
21,768 KB
testcase_62 AC 26 ms
22,300 KB
testcase_63 AC 28 ms
21,904 KB
testcase_64 AC 40 ms
22,692 KB
testcase_65 AC 33 ms
22,588 KB
testcase_66 AC 34 ms
21,880 KB
testcase_67 AC 44 ms
21,780 KB
testcase_68 AC 64 ms
22,624 KB
testcase_69 AC 55 ms
22,204 KB
testcase_70 AC 22 ms
21,760 KB
testcase_71 AC 24 ms
21,760 KB
testcase_72 AC 21 ms
21,904 KB
testcase_73 AC 41 ms
22,588 KB
testcase_74 AC 23 ms
22,016 KB
testcase_75 AC 29 ms
21,892 KB
testcase_76 AC 45 ms
21,780 KB
testcase_77 AC 63 ms
21,904 KB
testcase_78 AC 44 ms
22,216 KB
testcase_79 AC 74 ms
21,980 KB
testcase_80 AC 21 ms
22,632 KB
testcase_81 AC 26 ms
22,844 KB
testcase_82 AC 24 ms
22,444 KB
testcase_83 AC 22 ms
21,940 KB
testcase_84 AC 31 ms
22,456 KB
testcase_85 AC 43 ms
22,456 KB
testcase_86 AC 29 ms
21,904 KB
testcase_87 AC 32 ms
22,016 KB
testcase_88 AC 49 ms
22,620 KB
testcase_89 AC 56 ms
21,892 KB
testcase_90 AC 23 ms
22,716 KB
testcase_91 AC 26 ms
21,904 KB
testcase_92 AC 29 ms
22,880 KB
testcase_93 AC 28 ms
21,928 KB
testcase_94 AC 34 ms
22,456 KB
testcase_95 AC 21 ms
22,204 KB
testcase_96 AC 29 ms
22,540 KB
testcase_97 AC 93 ms
22,468 KB
testcase_98 AC 40 ms
22,408 KB
testcase_99 AC 38 ms
21,940 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() {
        int tried = 0, ok = 0;
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++) {
                if (passed[0][i][j]) { tried++; ok++; }
                else if (fail_count[0][i][j] > 0) tried++;
                if (passed[1][i][j]) { tried++; ok++; }
                else if (fail_count[1][i][j] > 0) tried++;
            }
        // 大きめに推定される?(サンプルが一様でないので?)
        wall_rate = 0.99 * wall_rate + 0.01 * (tried - ok) / 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 {
        if (turn < 0) {
            return create_exploring_path();
        } else {
            return create_shortest_path(turn);
        }
    }
    string create_exploring_path() const {
        return "";
    }
    string create_shortest_path(int turn) const {
        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] = 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) * (fail_prob <= 0.1 ? min(1.0, double(x1 * x1 + y1 * y1) / 25) : 1.0);
                d1 += 0.2 * L2(x1, y1, H / 2, W / 2) * pow(1 - turn / 1000.0, 2);
                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);
                    }
                }
            }
            update_wall_rate();
            // for (int i = 0; i < 5; i++) {
            //     for (int j = 0; j < 5; j++)
            //         cerr << setprecision(4) << '\t' << wall_prob[0][i][j] << '\t';
            //     cerr << endl;
            //     for (int j = 0; j < 5; j++)
            //         cerr << setprecision(4) << wall_prob[1][i][j] << '\t' << '\t';
            //     cerr << endl;
            // }
        }
    }
};

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