結果

問題 No.5006 Hidden Maze
ユーザー trineutron
提出日時 2022-06-12 15:46:00
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 3,514 bytes
コンパイル時間 2,443 ms
実行使用メモリ 22,756 KB
スコア 86,085
平均クエリ数 140.15
最終ジャッジ日時 2022-06-12 15:46:19
合計ジャッジ時間 10,201 ms
ジャッジサーバーID
(参考情報)
judge15 / judge16
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 100
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
using edge = tuple<int, 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() {
int default_cost = 2;
cin >> h >> w >> p;
vector cost_h(h, vector<int>(w - 1, 2));
vector cost_v(h - 1, vector<int>(w, 2));
while (true) {
vector dist(h, vector<int>(w, 1e9));
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;
int c;
if (i % 2 == 0) {
c = cost_v.at(x0 - i / 2).at(y0);
} else {
c = cost_h.at(x0).at(y0 - i / 2);
}
int 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;
int c;
if (i % 2 == 0) {
c = cost_v[x0 - i / 2][y0];
} else {
c = cost_h[x0][y0 - i / 2];
}
int 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];
if (d % 2 == 0) {
if (cost_v[x0 - d / 2][y0] == 1) continue;
cost_v[x0 - d / 2][y0] += 10000;
} else {
if (cost_h[x0][y0 - d / 2] == 1) continue;
cost_h[x0][y0 - d / 2] += 10000;
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i < h - 1) {
if (cost_v[i][j] == default_cost) {
cost_v[i][j]++;
}
}
if (j < w - 1) {
if (cost_h[i][j] == default_cost) {
cost_h[i][j]++;
}
}
}
}
default_cost++;
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0