結果

問題 No.2411 Reverse Directions
ユーザー keisuke6keisuke6
提出日時 2023-08-11 22:59:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 3,632 bytes
コンパイル時間 2,794 ms
コンパイル使用メモリ 220,252 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-04-29 14:10:26
合計ジャッジ時間 4,981 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 23 ms
13,312 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 46 ms
20,480 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 69 ms
18,560 KB
testcase_13 AC 17 ms
7,040 KB
testcase_14 AC 16 ms
7,168 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 22 ms
8,064 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 21 ms
7,424 KB
testcase_21 AC 28 ms
10,112 KB
testcase_22 AC 53 ms
13,952 KB
testcase_23 AC 31 ms
9,472 KB
testcase_24 AC 77 ms
18,304 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 14 ms
6,272 KB
testcase_28 AC 36 ms
10,728 KB
testcase_29 AC 69 ms
18,272 KB
testcase_30 AC 42 ms
12,284 KB
testcase_31 AC 44 ms
12,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	int H,W,K,L,R;
	cin>>H>>W>>K>>L>>R;
	vector<vector<char>> S(H,vector<char>(W));
	for(int i=0;i<H;i++)for(int j=0;j<W;j++) cin>>S[i][j];
	vector<vector<int>> G(H*W);
	for(int i=0;i<H;i++)for(int j=0;j<W;j++){
		if(S[i][j] == '#') continue;
		if(i != 0 && S[i-1][j] == '.') G[i*W+j].push_back(i*W+j-W);
		if(i != H-1 && S[i+1][j] == '.') G[i*W+j].push_back(i*W+j+W);
		if(j != 0 && S[i][j-1] == '.') G[i*W+j].push_back(i*W+j-1);
		if(j != W-1 && S[i][j+1] == '.') G[i*W+j].push_back(i*W+j+1);
	}
	vector<int> dist(H*W,1e18);
	vector<int> seen(H*W,1e18);
	seen[H*W-1] = 0;
	dist[0] = 0;
	queue<int> q;
	q.push(0);
	while(!q.empty()){
		int pos = q.front();
		q.pop();
		for(int x:G[pos]){
			if(dist[x] != 1e18) continue;
			dist[x] = dist[pos] + 1;
			q.push(x);
		}
	}
	q.push(H*W-1);
	while(!q.empty()){
		int pos = q.front();
		q.pop();
		for(int x:G[pos]){
			if(seen[x] != 1e18) continue;
			seen[x] = seen[pos] + 1;
			q.push(x);
		}
	}
	if(dist[H*W-1] == 1e18 || dist[H*W-1]%2 != K%2 || (R-L)%2 == 0){
		cout<<"No"<<endl;
		return 0;
	}
	for(int i=0;i<H;i++)for(int j=0;j<W;j++){
		if(S[i][j] == '#' || dist[i*W+j] >= L || seen[i*W+j] > K-R || dist[i*W+j]%2 == L%2 || seen[i*W+j]%2 != (K-R)%2 || dist[i*W+j] == 1e18 || seen[i*W+j] == 1e18) continue;
		bool a = (i != 0 && i != H-1 && S[i-1][j] == '.' && S[i+1][j] == '.');
		bool b = (j != 0 && j != W-1 && S[i][j-1] == '.' && S[i][j+1] == '.');
		if(a){
			cout<<"Yes"<<endl;
			string s = "", t = "", ans = "";
			int now = i*W+j;
			while(now != 0){
				for(int x:G[now]){
					if(dist[x]+1 == dist[now]){
						if(x == now-1) s += 'R';
						if(x == now+1) s += 'L';
						if(x == now-W) s += 'D';
						if(x == now+W) s += 'U';
						now = x;
						break;
					}
				}
			}
			now = H*W-1;
			vector<int> distt(H*W,1e18);
			distt[i*W+j] = 0;
			q.push(i*W+j);
			while(!q.empty()){
				int pos = q.front();
				q.pop();
				for(int x:G[pos]){
					if(distt[x] != 1e18) continue;
					distt[x] = distt[pos] + 1;
					q.push(x);
				}
			}
			while(now != i*W+j){
				for(int x:G[now]){
					if(distt[x]+1 == distt[now]){
						if(x == now-1) t += 'R';
						if(x == now+1) t += 'L';
						if(x == now-W) t += 'D';
						if(x == now+W) t += 'U';
						now = x;
						break;
					}
				}
			}
			reverse(s.begin(),s.end());
			reverse(t.begin(),t.end());
			for(int k=dist[i*W+j]+1;k<K-(distt[H*W-1]-distt[i*W+j]);k+=2){
				s += 'U';
				s += 'D';
			}
			ans = s+t;
			cout<<ans<<endl;
			return 0;
		}
		if(b){
			cout<<"Yes"<<endl;
			string s = "", t = "", ans = "";
			int now = i*W+j;
			while(now != 0){
				for(int x:G[now]){
					if(dist[x]+1 == dist[now]){
						if(x == now-1) s += 'R';
						if(x == now+1) s += 'L';
						if(x == now-W) s += 'D';
						if(x == now+W) s += 'U';
						now = x;
						break;
					}
				}
			}
			now = H*W-1;
			vector<int> distt(H*W,1e18);
			distt[i*W+j] = 0;
			q.push(i*W+j);
			while(!q.empty()){
				int pos = q.front();
				q.pop();
				for(int x:G[pos]){
					if(distt[x] != 1e18) continue;
					distt[x] = distt[pos] + 1;
					q.push(x);
				}
			}
			while(now != i*W+j){
				for(int x:G[now]){
					if(distt[x]+1 == distt[now]){
						if(x == now-1) t += 'R';
						if(x == now+1) t += 'L';
						if(x == now-W) t += 'D';
						if(x == now+W) t += 'U';
						now = x;
						break;
					}
				}
			}
			reverse(s.begin(),s.end());
			reverse(t.begin(),t.end());
			for(int k=dist[i*W+j]+1;k<K-(distt[H*W-1]-distt[i*W+j]);k+=2){
				s += 'L';
				s += 'R';
			}
			ans = s+t;
			cout<<ans<<endl;
			return 0;
		}
	}
	cout<<"No"<<endl;
}
0