結果
| 問題 |
No.2411 Reverse Directions
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2023-08-21 14:33:03 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 40 ms / 2,000 ms |
| コード長 | 2,694 bytes |
| コンパイル時間 | 704 ms |
| コンパイル使用メモリ | 61,228 KB |
| 実行使用メモリ | 18,876 KB |
| 最終ジャッジ日時 | 2024-12-15 03:00:36 |
| 合計ジャッジ時間 | 3,662 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
/* -*- 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;
}
tnakao0123