結果

問題 No.2411 Reverse Directions
ユーザー tnakao0123tnakao0123
提出日時 2023-08-21 14:33:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 2,694 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 60,504 KB
実行使用メモリ 18,568 KB
最終ジャッジ日時 2023-08-21 14:33:08
合計ジャッジ時間 4,454 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,652 KB
testcase_01 AC 5 ms
10,544 KB
testcase_02 AC 4 ms
10,560 KB
testcase_03 AC 4 ms
10,624 KB
testcase_04 AC 10 ms
12,376 KB
testcase_05 AC 4 ms
10,616 KB
testcase_06 AC 4 ms
10,620 KB
testcase_07 AC 8 ms
12,920 KB
testcase_08 AC 5 ms
10,612 KB
testcase_09 AC 5 ms
10,544 KB
testcase_10 AC 29 ms
18,568 KB
testcase_11 AC 4 ms
10,676 KB
testcase_12 AC 41 ms
17,348 KB
testcase_13 AC 5 ms
10,692 KB
testcase_14 AC 14 ms
12,952 KB
testcase_15 AC 6 ms
11,388 KB
testcase_16 AC 6 ms
11,272 KB
testcase_17 AC 16 ms
12,844 KB
testcase_18 AC 7 ms
11,488 KB
testcase_19 AC 4 ms
10,664 KB
testcase_20 AC 14 ms
12,584 KB
testcase_21 AC 18 ms
13,424 KB
testcase_22 AC 31 ms
15,624 KB
testcase_23 AC 5 ms
10,744 KB
testcase_24 AC 42 ms
17,004 KB
testcase_25 AC 5 ms
10,664 KB
testcase_26 AC 7 ms
11,032 KB
testcase_27 AC 12 ms
12,148 KB
testcase_28 AC 22 ms
14,972 KB
testcase_29 AC 42 ms
17,948 KB
testcase_30 AC 27 ms
15,704 KB
testcase_31 AC 28 ms
14,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2411.cc:  No.2411 Reverse Directions - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_H = 500;
const int MAX_W = 500;
const int MAX_N = MAX_H * MAX_W;
const int MAX_K = 500000;

enum { L, R, U, D };
const char dcs[] = "LRUD";

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;

/* global variables */

char fs[MAX_H][MAX_W + 4];
vi nbrs[MAX_N];
int ds0[MAX_N], ds1[MAX_N], ps0[MAX_N], ps1[MAX_N];

/* subroutines */

void bfs(int n, int st, int ds[], int ps[]) {
  fill(ds, ds + n, -1);
  ds[st] = 0, ps[st] = -1;

  qi q;
  q.push(st);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    for (auto v: nbrs[u])
      if (ds[v] < 0) {
	ds[v] = ds[u] + 1, ps[v] = u;
	q.push(v);
      }
  }
}

void makepath(int u, int nch, int ud, int l, int ps[], vi &vs) {
  while (l > ud) {
    vs.push_back(nch), vs.push_back(nch ^ 1);
    l -= 2;
  }
  while (ps[u] >= 0) {
    int dd = ps[u] - u;
    if (dd == -1) vs.push_back(L);
    else if (dd == 1) vs.push_back(R);
    else if (dd < -1) vs.push_back(U);
    else if (dd > 1) vs.push_back(D);
    u = ps[u];
  }
}

void revpath(vi &vs) {
  reverse(vs.begin(), vs.end());
  for (auto &v: vs) v ^= 1;
}

/* main */

int main() {
  int h, w, k, l, r;
  scanf("%d%d%d%d%d", &h, &w, &k, &l, &r), l--;
  for (int y = 0; y < h; y++) scanf("%s", fs[y]);
  int l0 = l, l1 = r - l, l2 = k - r;

  if (l1 & 1) { puts("No"); return 0; }
  
  int n = h * w;
  for (int y = 0, u = 0; y < h; y++)
    for (int x = 0; x < w; x++, u++)
      if (fs[y][x] == '.') {
	if (y + 1 < h && fs[y + 1][x] == '.')
	  nbrs[u].push_back(u + w), nbrs[u + w].push_back(u);
	if (x + 1 < w && fs[y][x + 1] == '.')
	  nbrs[u].push_back(u + 1), nbrs[u + 1].push_back(u);
      }

  bfs(n, 0, ds0, ps0);
  bfs(n, n - 1, ds1, ps1);
  int gd0 = ds0[n - 1];
  if (gd0 < 0 || gd0 > k || ((k - gd0) & 1)) { puts("No"); return 0; }

  for (int y = 0, u = 0; y < h; y++)
    for (int x = 0; x < w; x++, u++) {
      int d0 = ds0[u], d1 = ds1[u];
      if (d0 >= 0 && l0 >= d0 && ! ((l0 - d0) & 1) &&
	  d1 >= 0 && l2 >= d1 && ! ((l2 - d1) & 1)) {
	bool f0 = (y > 0 && y + 1 < h && ds0[u - w] >= 0 && ds0[u + w] >= 0);
	bool f1 = (x > 0 && x + 1 < w && ds0[u - 1] >= 0 && ds0[u + 1] >= 0);
	if (f0 || f1) {
	  vi vs;
	  int nch = f0 ? D : L;
	  makepath(u, nch, d0, l0, ps0, vs);
	  revpath(vs);
	  while (l1 > 0) {
	    vs.push_back(nch), vs.push_back(nch ^ 1);
	    l1 -= 2;
	  }
	  makepath(u, nch, d1, l2, ps1, vs);

	  puts("Yes");
	  for (auto v: vs) putchar(dcs[v]);
	  putchar('\n');

	  return 0;
	}
      }
    }

  puts("No");
  return 0;
}
0