結果

問題 No.2411 Reverse Directions
ユーザー KudeKude
提出日時 2023-08-11 22:06:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 3,030 bytes
コンパイル時間 2,832 ms
コンパイル使用メモリ 235,296 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-29 13:04:30
合計ジャッジ時間 4,376 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 5 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 13 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 17 ms
6,944 KB
testcase_13 AC 6 ms
6,948 KB
testcase_14 AC 7 ms
6,944 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 7 ms
6,940 KB
testcase_18 AC 3 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 6 ms
6,944 KB
testcase_21 AC 6 ms
6,940 KB
testcase_22 AC 13 ms
6,940 KB
testcase_23 AC 9 ms
6,940 KB
testcase_24 AC 17 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 5 ms
6,940 KB
testcase_28 AC 10 ms
6,944 KB
testcase_29 AC 17 ms
6,944 KB
testcase_30 AC 12 ms
6,940 KB
testcase_31 AC 13 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
const int di[] = {1, 0, -1, 0};
const int dj[] = {0, 1, 0, -1};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k, l, r;
  cin >> n >> m >> k >> l >> r;
  l--;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  constexpr int INF = 1001001001;
  auto idxchk = [&](int i, int j) { return 0 <= i && i < n && 0 <= j && j < m; };
  auto bfs = [&](int si, int sj) {
    VVI dist(n, VI(m, INF));
    queue<P> q;
    auto add = [&](int i, int j, int d) {
      if (dist[i][j] == INF) {
        dist[i][j] = d;
        q.emplace(i, j);
      }
    };
    add(si, sj, 0);
    while(q.size()) {
      auto [i, j] = q.front(); q.pop();
      rep(k, 4) {
        int ni = i + di[k], nj = j + dj[k];
        if (!idxchk(ni, nj)) continue;
        if (s[ni][nj] == '#') continue;
        add(ni, nj, dist[i][j] + 1);
      }
    }
    return dist;
  };
  int c0 = l, c1 = k - r;
  VVI d0 = bfs(0, 0), d1 = bfs(n - 1, m - 1);
  rep(i, n) rep(j, m) {
    int r0 = c0 - d0[i][j];
    int r1 = c1 - d1[i][j];
    if (r0 < 0 || r1 < 0 || r0 % 2 || r1 % 2 || (r - l) % 2) continue;
    bool ok = false;
    if (idxchk(i-1, j) && idxchk(i+1, j) && s[i-1][j] == '.' && s[i+1][j] == '.') ok = true;
    else if (idxchk(i, j-1) && idxchk(i, j+1) && s[i][j-1] == '.' && s[i][j+1] == '.') ok = true;
    if (ok) {
      cout << "Yes\n";
      string ans;
      int x = i, y = j;
      while(d0[x][y]) {
        rep(k, 4) {
          int nx = x + di[k], ny = y + dj[k];
          if (!idxchk(nx, ny)) continue;
          if (d0[nx][ny] == d0[x][y] - 1) {
            ans += "ULDR"[k];
            x = nx, y = ny;
            break;
          }
        }
      }
      reverse(all(ans));
      int cnt = (r0 + r1 + (r - l)) / 2;
      if (idxchk(i-1, j) && idxchk(i+1, j) && s[i-1][j] == '.' && s[i+1][j] == '.') {
        rrep(_, cnt) ans += 'U', ans += 'D';
      } else {
        rrep(_, cnt) ans += 'L', ans += 'R';
      }
      x = i, y = j;
      while(d1[x][y]) {
        rep(k, 4) {
          int nx = x + di[k], ny = y + dj[k];
          if (!idxchk(nx, ny)) continue;
          if (d1[nx][ny] == d1[x][y] - 1) {
            ans += "DRUL"[k];
            x = nx, y = ny;
            break;
          }
        }
      }
      cout << ans << '\n';
      return 0;
    }
  }
  cout << "No\n";
}
0