結果

問題 No.2411 Reverse Directions
ユーザー Kude
提出日時 2023-08-11 22:06:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 3,030 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 223,964 KB
最終ジャッジ日時 2025-02-16 01:27:03
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

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";
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0