結果

問題 No.2411 Reverse Directions
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-08-11 22:35:33
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 4,139 bytes
コンパイル時間 2,743 ms
コンパイル使用メモリ 122,496 KB
実行使用メモリ 52,352 KB
最終ジャッジ日時 2024-04-29 13:39:56
合計ジャッジ時間 5,131 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 68 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 11 ms
7,808 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 280 ms
23,936 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 240 ms
22,304 KB
testcase_13 AC 88 ms
10,752 KB
testcase_14 AC 76 ms
11,464 KB
testcase_15 AC 21 ms
5,376 KB
testcase_16 AC 15 ms
5,376 KB
testcase_17 AC 80 ms
11,776 KB
testcase_18 AC 35 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 65 ms
10,880 KB
testcase_21 AC 106 ms
21,376 KB
testcase_22 AC 168 ms
11,392 KB
testcase_23 AC 139 ms
10,856 KB
testcase_24 AC 237 ms
52,352 KB
testcase_25 AC 32 ms
10,840 KB
testcase_26 AC 14 ms
5,376 KB
testcase_27 AC 71 ms
10,796 KB
testcase_28 AC 143 ms
12,416 KB
testcase_29 AC 262 ms
23,680 KB
testcase_30 AC 177 ms
23,296 KB
testcase_31 AC 214 ms
21,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {
  }
}
0