結果
問題 | No.2411 Reverse Directions |
ユーザー |
👑 |
提出日時 | 2023-08-11 22:35:33 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 295 ms / 2,000 ms |
コード長 | 4,139 bytes |
コンパイル時間 | 2,344 ms |
コンパイル使用メモリ | 126,388 KB |
実行使用メモリ | 52,292 KB |
最終ジャッジ日時 | 2024-11-18 17:27:12 |
合計ジャッジ時間 | 5,700 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;import core.bitop;class EOFException : Throwable { this() { super("EOF"); } }string[] tokens;string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }int readInt() { return readToken.to!int; }long readLong() { return readToken.to!long; }bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1;(unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }enum DX = [+1, 0, -1, 0];enum DY = [0, +1, 0, -1];void main() {try {for (; ; ) {const M = readInt;const N = readInt;const K = readInt;const L = readInt - 1;const R = readInt;auto S = new string[M];foreach (x; 0 .. M) {S[x] = readToken;}DList!int que;auto dist = new int[][][](2, M, N);auto prv = new int[][][](2, M, N);foreach (h; 0 .. 2) {foreach (x; 0 .. M) {dist[h][x][] = -1;prv[h][x][] = -1;}int sx, sy;if (h == 0) {sx = 0;sy = 0;} else {sx = M - 1;sy = N - 1;}dist[h][sx][sy] = 0;que ~= sx;que ~= sy;for (; !que.empty; ) {const x = que.front; que.removeFront;const y = que.front; que.removeFront;foreach (dir; 0 .. 4) {int xx = x + DX[dir];int yy = y + DY[dir];if (0 <= xx && xx < M && 0 <= yy && yy < N && S[xx][yy] == '.') {if (!~dist[h][xx][yy]) {dist[h][xx][yy] = dist[h][x][y] + 1;prv[h][xx][yy] = dir;que ~= xx;que ~= yy;}}}}debug {foreach (x; 0 .. M) {writeln(dist[h][x]);}}}bool check(int d, int l) {return (~d && d <= l && (l - d) % 2 == 0);}foreach (x0; 0 .. M) foreach (y0; 0 .. N) if (S[x0][y0] == '.') {const d0 = dist[0][x0][y0];const d1 = dist[1][x0][y0];foreach (k; 0 .. 2) {bool ok = true;foreach (dir; [k, k ^ 2]) {const xx = x0 + DX[dir];const yy = y0 + DY[dir];ok = ok && (0 <= xx && xx < M && 0 <= yy && yy < N && S[xx][yy] == '.');}ok = ok && check(d0, L);ok = ok && ((R - L) % 2 == 0);ok = ok && check(d1, K - R);if (ok) {int[][2] fss;foreach (h; 0 .. 2) {int x = x0, y = y0;for (; ; ) {const dir = prv[h][x][y];if (!~dir) break;fss[h] ~= dir;x -= DX[dir];y -= DY[dir];}}fss[0].reverse;fss[1][] ^= 2;debug {writeln("fss = ", fss);}auto ans = new int[K];ans[] = -1;ans[0 .. d0] = fss[0][];ans[K - d1 .. K] = fss[1][];foreach (j; d0 .. K - d1) {ans[j] = ((j - d0) & 1) ? (k ^ 2) : k;}writeln("Yes");foreach (j; 0 .. K) {write("DRUL"[ans[j]]);}writeln;goto found;}}}writeln("No");found:}} catch (EOFException e) {}}