結果

問題 No.2411 Reverse Directions
ユーザー kwm_tkwm_t
提出日時 2023-08-12 00:10:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 3,894 bytes
コンパイル時間 2,282 ms
コンパイル使用メモリ 217,360 KB
実行使用メモリ 7,076 KB
最終ジャッジ日時 2024-04-29 15:23:38
合計ジャッジ時間 3,776 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
const int dx[] = { 1,0,-1,0 };
const int dy[] = { 0,-1,0,1 };
char cr[] = { 'U','R','D','L' };
char cr2[] = { 'D','L','U','R' };
string f(string s, int l, int r) {
	rep(i, s.size()) {
		if (l - 1 <= i && i < r) {
			if ('U' == s[i])s[i] = 'D';
			else if ('D' == s[i])s[i] = 'U';
			else if ('R' == s[i])s[i] = 'L';
			else if ('L' == s[i])s[i] = 'R';
		}
	}
	return s;
}
void check(bool mode = true) {
	int h, w, k, l, r; cin >> h >> w >> k >> l >> r;
	vector<string>s(h);
	rep(i, h)cin >> s[i];
	string str; cin >> str;
	str = f(str, l, r);
	cout << str << endl;
	int x = 0, y = 0;
	rep(i, str.size()) {
		if ('U' == str[i])x--;
		if ('D' == str[i])x++;
		if ('R' == str[i])y++;
		if ('L' == str[i])y--;
		if (s[x][y] == '#') {
			cout << "!!" << endl;
			return;
		}
	}
	cout << "ok" << endl;
}
int main() {
	//check();
	//return 0;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int h, w, k, l, r; cin >> h >> w >> k >> l >> r;
	vector<string>s(h);
	rep(i, h)cin >> s[i];
	if (0 == (r - l) % 2) {
		cout << "No" << endl;
		return 0;
	}
	vector<vector<int>> dp(h, vector<int>(w, INF));
	vector<vector<int>> dpR(h, vector<int>(w, INF));
	rep(_, 2) {
		queue<pair<int, int>> q;
		if (0 == _) {
			q.emplace(0, 0);
			dp[0][0] = 0;
		}
		else {
			q.emplace(h - 1, w - 1);
			dp[h - 1][w - 1] = 0;
		}
		while (!q.empty()) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			rep(i, 4) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				if ((0 <= nx) && (h > nx) && (0 <= ny) && (w > ny) && ('#' != s[nx][ny])) {
					if (chmin(dp[nx][ny], dp[x][y] + 1)) q.emplace(nx, ny);
				}
			}
		}
		swap(dp, dpR);
	}
	if (0 != abs(dp[h - 1][w - 1] - k) % 2) {
		cout << "No" << endl;
		return 0;
	}
	auto f = [&](int x, int y)->string {
		string ret;
		while (0 != dp[x][y]) {
			rep(i, 4) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				if (nx < 0)continue;
				if (ny < 0)continue;
				if (nx >= h)continue;
				if (ny >= w)continue;
				char c = cr[i];
				if (dp[nx][ny] + 1 == dp[x][y]) {
					x = nx;
					y = ny;
					ret += c;
					break;
				}
			}
		}
		return ret;
	};
	rep(i, h)rep(j, w) {
		if (INF == dp[i][j])continue;
		int he = dp[i][j];
		int ta = dpR[i][j];
		int bo = k - he - ta;
		if (he >= l)continue;
		if (bo < 0)continue;
		if (he + bo < r)continue;
		if (0 != (r - he) % 2)continue;
		if ((0 != j) && ('.' == s[i][j - 1]) && (w != j + 1) && ('.' == s[i][j + 1])) {
			string ans0 = f(i, j);
			reverse(all(ans0));
			swap(dp, dpR);
			swap(cr, cr2);
			string ans1;
			rep(i, bo / 2)ans1 += "LR";
			string ans2 = f(i, j);
			cout << "Yes" << endl;
			string ans = ans0 + ans1 + ans2;
			cout << ans << endl;
			return 0;
		}
		if ((0 != i) && ('.' == s[i - 1][j]) && (h != (i + 1)) && ('.' == s[i + 1][j])) {
			string ans0 = f(i, j);
			reverse(all(ans0));
			swap(dp, dpR);
			swap(cr, cr2);
			string ans1;
			rep(i, bo / 2)ans1 += "DU";
			string ans2 = f(i, j);
			cout << "Yes" << endl;
			string ans = ans0 + ans1 + ans2;
			cout << ans << endl;
			return 0;
		}
	}
	cout << "No" << endl;
	return 0;
}
0