#include #include #include #include #include #include #include #include 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(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 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 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) { 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 from[H][W]; priority_queue > 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 + (turn > 500 ? wp : 1 / (1 - wp)); d1 += 0.1 * L2(x1, y1, H / 2, W / 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(); } } }; 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); }