結果

問題 No.2411 Reverse Directions
ユーザー square1001square1001
提出日時 2023-08-11 21:52:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 2,804 bytes
コンパイル時間 1,025 ms
コンパイル使用メモリ 90,588 KB
実行使用メモリ 6,812 KB
最終ジャッジ日時 2023-08-11 21:52:53
合計ジャッジ時間 3,491 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 8 ms
5,868 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 21 ms
6,812 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 22 ms
5,172 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 8 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 8 ms
4,380 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 15 ms
4,752 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 19 ms
5,220 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 6 ms
4,380 KB
testcase_28 AC 12 ms
4,472 KB
testcase_29 AC 23 ms
5,172 KB
testcase_30 AC 16 ms
4,652 KB
testcase_31 AC 13 ms
4,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <queue>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int INF = 1012345678;
const vector<int> dx = { 0, 1, 0, -1 };
const vector<int> dy = { 1, 0, -1, 0 };
const string ds = "RDLU";

vector<vector<int> > distance(int H, int W, const vector<string>& S, int sx, int sy) {
	vector<vector<int> > dist(H, vector<int>(W, INF));
	dist[sx][sy] = 0;
	queue<int> que;
	que.push(sx * W + sy);
	while (!que.empty()) {
		int cx = que.front() / W;
		int cy = que.front() % W;
		que.pop();
		for (int i = 0; i < 4; i++) {
			int tx = cx + dx[i];
			int ty = cy + dy[i];
			if (0 <= tx && tx < H && 0 <= ty && ty < W && S[tx][ty] == '.' && dist[tx][ty] == INF) {
				dist[tx][ty] = dist[cx][cy] + 1;
				que.push(tx * W + ty);
			}
		}
	}
	return dist;
}

int main() {
	int H, W, K, L, R;
	cin >> H >> W >> K >> L >> R;
	L -= 1;
	vector<string> S(H);
	for (int i = 0; i < H; i++) {
		cin >> S[i];
	}
	if ((R - L) % 2 == 1) {
		cout << "No" << endl;
	}
	else {
		vector<vector<int> > d1 = distance(H, W, S, 0, 0);
		vector<vector<int> > d2 = distance(H, W, S, H - 1, W - 1);
		bool found = false;
		string answer;
		for (int i = 0; i < H && !found; i++) {
			for (int j = 0; j < W && !found; j++) {
				if (d1[i][j] <= L && d1[i][j] % 2 == L % 2 && d2[i][j] <= K - R && d2[i][j] % 2 == (K - R) % 2) {
					int bit = 0;
					if (i != 0 && i != H - 1 && S[i - 1][j] == '.' && S[i + 1][j] == '.') {
						bit |= 1;
					}
					if (j != 0 && j != W - 1 && S[i][j - 1] == '.' && S[i][j + 1] == '.') {
						bit |= 2;
					}
					if (bit != 0) {
						found = true;
						int ax = i, ay = j;
						for (int k = 0; k < L; k++) {
							int best = INF, nx = -1, ny = -1, nd = -1;
							for (int l = 0; l < 4; l++) {
								int tx = ax + dx[l], ty = ay + dy[l];
								if (0 <= tx && tx < H && 0 <= ty && ty < W && best > d1[tx][ty]) {
									best = d1[tx][ty];
									nx = tx;
									ny = ty;
									nd = l ^ 2;
								}
							}
							answer += ds[nd];
							ax = nx;
							ay = ny;
						}
						reverse(answer.begin(), answer.end());
						for (int i = L; i < R; i++) {
							answer += (bit & 1 ? (i % 2 == 0 ? 'U' : 'D') : (i % 2 == 0 ? 'L' : 'R'));
						}
						int bx = i, by = j;
						for (int k = R; k < K; k++) {
							int best = INF, nx = -1, ny = -1, nd = -1;
							for (int l = 0; l < 4; l++) {
								int tx = bx + dx[l], ty = by + dy[l];
								if (0 <= tx && tx < H && 0 <= ty && ty < W && best > d2[tx][ty]) {
									best = d2[tx][ty];
									nx = tx;
									ny = ty;
									nd = l;
								}
							}
							answer += ds[nd];
							bx = nx;
							by = ny;
						}
					}
				}
			}
		}
		if (!found) {
			cout << "No" << endl;
		}
		else {
			cout << "Yes" << endl;
			cout << answer << endl;
		}
	}
	return 0;
}
0