結果

問題 No.5006 Hidden Maze
ユーザー trineutrontrineutron
提出日時 2022-06-12 19:00:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 3,618 bytes
コンパイル時間 2,054 ms
実行使用メモリ 22,868 KB
スコア 79,818
平均クエリ数 202.82
最終ジャッジ日時 2022-06-12 19:00:14
合計ジャッジ時間 10,106 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
21,940 KB
testcase_01 AC 26 ms
21,880 KB
testcase_02 AC 20 ms
22,564 KB
testcase_03 AC 22 ms
21,780 KB
testcase_04 AC 20 ms
21,940 KB
testcase_05 AC 28 ms
21,892 KB
testcase_06 AC 19 ms
22,108 KB
testcase_07 AC 87 ms
22,004 KB
testcase_08 AC 119 ms
22,252 KB
testcase_09 AC 92 ms
21,892 KB
testcase_10 AC 22 ms
22,868 KB
testcase_11 AC 20 ms
22,264 KB
testcase_12 AC 28 ms
21,768 KB
testcase_13 AC 18 ms
22,216 KB
testcase_14 AC 35 ms
22,564 KB
testcase_15 AC 30 ms
22,588 KB
testcase_16 AC 23 ms
22,444 KB
testcase_17 AC 25 ms
21,916 KB
testcase_18 AC 35 ms
21,964 KB
testcase_19 AC 24 ms
22,216 KB
testcase_20 AC 23 ms
22,264 KB
testcase_21 AC 20 ms
22,264 KB
testcase_22 AC 22 ms
21,952 KB
testcase_23 AC 21 ms
21,768 KB
testcase_24 AC 40 ms
21,916 KB
testcase_25 AC 19 ms
21,940 KB
testcase_26 AC 54 ms
21,768 KB
testcase_27 AC 36 ms
21,904 KB
testcase_28 AC 23 ms
21,904 KB
testcase_29 AC 108 ms
22,588 KB
testcase_30 AC 18 ms
21,880 KB
testcase_31 AC 26 ms
22,004 KB
testcase_32 AC 23 ms
22,588 KB
testcase_33 AC 40 ms
22,564 KB
testcase_34 AC 20 ms
21,940 KB
testcase_35 AC 33 ms
22,204 KB
testcase_36 AC 46 ms
22,444 KB
testcase_37 AC 27 ms
21,768 KB
testcase_38 AC 73 ms
22,588 KB
testcase_39 AC 30 ms
21,768 KB
testcase_40 AC 20 ms
22,264 KB
testcase_41 AC 25 ms
22,216 KB
testcase_42 AC 26 ms
22,432 KB
testcase_43 AC 29 ms
21,992 KB
testcase_44 AC 18 ms
21,780 KB
testcase_45 AC 41 ms
22,252 KB
testcase_46 AC 36 ms
22,228 KB
testcase_47 AC 18 ms
22,856 KB
testcase_48 AC 45 ms
21,880 KB
testcase_49 AC 58 ms
21,768 KB
testcase_50 AC 20 ms
22,264 KB
testcase_51 AC 22 ms
21,940 KB
testcase_52 AC 18 ms
22,216 KB
testcase_53 AC 34 ms
21,928 KB
testcase_54 AC 20 ms
22,444 KB
testcase_55 AC 29 ms
22,576 KB
testcase_56 AC 20 ms
22,004 KB
testcase_57 AC 76 ms
21,748 KB
testcase_58 AC 21 ms
21,768 KB
testcase_59 AC 28 ms
21,892 KB
testcase_60 AC 22 ms
21,892 KB
testcase_61 AC 17 ms
22,276 KB
testcase_62 AC 29 ms
21,940 KB
testcase_63 AC 24 ms
21,892 KB
testcase_64 AC 24 ms
21,992 KB
testcase_65 AC 24 ms
22,216 KB
testcase_66 AC 20 ms
22,552 KB
testcase_67 AC 53 ms
21,992 KB
testcase_68 AC 37 ms
21,928 KB
testcase_69 AC 22 ms
21,844 KB
testcase_70 AC 21 ms
22,540 KB
testcase_71 AC 22 ms
21,904 KB
testcase_72 AC 23 ms
22,588 KB
testcase_73 AC 26 ms
22,264 KB
testcase_74 AC 37 ms
22,444 KB
testcase_75 AC 55 ms
22,004 KB
testcase_76 AC 41 ms
21,940 KB
testcase_77 AC 46 ms
22,432 KB
testcase_78 AC 26 ms
21,980 KB
testcase_79 AC 45 ms
22,612 KB
testcase_80 AC 22 ms
22,600 KB
testcase_81 AC 23 ms
21,904 KB
testcase_82 AC 24 ms
22,564 KB
testcase_83 AC 20 ms
21,940 KB
testcase_84 AC 24 ms
22,252 KB
testcase_85 AC 35 ms
21,928 KB
testcase_86 AC 17 ms
21,928 KB
testcase_87 AC 40 ms
21,588 KB
testcase_88 AC 47 ms
21,992 KB
testcase_89 AC 114 ms
21,880 KB
testcase_90 AC 17 ms
21,892 KB
testcase_91 AC 21 ms
21,992 KB
testcase_92 AC 27 ms
22,612 KB
testcase_93 AC 22 ms
21,916 KB
testcase_94 AC 28 ms
21,892 KB
testcase_95 AC 20 ms
21,768 KB
testcase_96 AC 17 ms
21,892 KB
testcase_97 AC 24 ms
22,552 KB
testcase_98 AC 112 ms
21,768 KB
testcase_99 AC 23 ms
21,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using edge = tuple<int64_t, int, int>;

const vector<int> dx{1, 0, -1, 0, 1, 0, -1, 0}, dy{0, 1, 0, -1, 0, 1, 0, -1};
const string ds{'D', 'R', 'U', 'L', 'D', 'R', 'U', 'L'};

random_device rng;

int h, w, p;

bool in_board(int x, int y) { return 0 <= x and x < h and 0 <= y and y < w; }

int main() {
    cin >> h >> w >> p;
    vector cost_h(h, vector<int64_t>(w - 1, 1000000));
    vector cost_v(h - 1, vector<int64_t>(w, 1000000));
    while (true) {
        vector dist(h, vector<int64_t>(w, 1e18));
        priority_queue<edge, vector<edge>, greater<edge>> q;
        q.emplace(0, 0, 0);
        while (not q.empty()) {
            auto [d0, x0, y0]{q.top()};
            q.pop();
            if (d0 >= dist[x0][y0]) continue;
            dist[x0][y0] = d0;
            for (int i = 0; i < 4; i++) {
                int x = x0 + dx[i], y = y0 + dy[i];
                if (not in_board(x, y)) continue;
                int64_t c;
                if (i % 2 == 0) {
                    c = cost_v.at(x0 - i / 2).at(y0);
                } else {
                    c = cost_h.at(x0).at(y0 - i / 2);
                }
                int64_t d = d0 + c;
                if (d >= dist[x][y]) continue;
                q.emplace(d, x, y);
            }
        }
        vector<int> dirs;
        string ans;
        int x0 = h - 1, y0 = w - 1;
        while (x0 or y0) {
            int d0 = dist[x0][y0];
            vector<int> ok;
            for (int i = 0; i < 4; i++) {
                int x = x0 + dx[i], y = y0 + dy[i];
                if (not in_board(x, y)) continue;
                int64_t c;
                if (i % 2 == 0) {
                    c = cost_v[x0 - i / 2][y0];
                } else {
                    c = cost_h[x0][y0 - i / 2];
                }
                int64_t d = d0 - c;
                if (d != dist[x][y]) continue;
                ok.emplace_back((i + 2) % 4);
            }
            int sz = ok.size();
            int d = ok.at(rng() % sz);
            dirs.emplace_back(d);
            ans += ds[d];
            x0 -= dx[d];
            y0 -= dy[d];
        }
        reverse(dirs.begin(), dirs.end());
        reverse(ans.begin(), ans.end());
        cout << ans << endl;

        int res;
        cin >> res;
        if (res == -1) {
            break;
        }
        for (int i = 0; i < res; i++) {
            int d = dirs[i];
            int x = x0 + dx[d], y = y0 + dy[d];
            if (d % 2 == 0) {
                cost_v[x0 - d / 2][y0] = 1;
            } else {
                cost_h[x0][y0 - d / 2] = 1;
            }
            x0 = x;
            y0 = y;
        }
        if (int(ans.size()) > res) {
            int d = dirs[res];
            int x = x0 + dx[d], y = y0 + dy[d];
            if (d % 2 == 0) {
                if (cost_v[x0 - d / 2][y0] == 1) continue;
                cost_v[x0 - d / 2][y0] += 10000000;
            } else {
                if (cost_h[x0][y0 - d / 2] == 1) continue;
                cost_h[x0][y0 - d / 2] += 10000000;
            }
            x0 = x;
            y0 = y;
        }
        for (int i = res + 1; i < int(ans.size()); i++) {
            int d = dirs[i];
            int x = x0 + dx[d], y = y0 + dy[d];
            if (d % 2 == 0) {
                if (cost_v[x0 - d / 2][y0] == 1) continue;
                cost_v[x0 - d / 2][y0] += 1;
            } else {
                if (cost_h[x0][y0 - d / 2] == 1) continue;
                cost_h[x0][y0 - d / 2] += 1;
            }
            x0 = x;
            y0 = y;
        }
    }
    return 0;
}
0