結果

問題 No.2411 Reverse Directions
ユーザー kotatsugamekotatsugame
提出日時 2023-08-11 21:48:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 917 ms
コンパイル使用メモリ 81,568 KB
実行使用メモリ 5,912 KB
最終ジャッジ日時 2024-04-29 12:40:56
合計ジャッジ時間 2,510 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 8 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 2 ms
5,376 KB
testcase_10 AC 15 ms
5,912 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 19 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 8 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 7 ms
5,376 KB
testcase_22 AC 14 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 17 ms
5,632 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 6 ms
5,376 KB
testcase_28 AC 13 ms
5,376 KB
testcase_29 AC 20 ms
5,376 KB
testcase_30 AC 15 ms
5,376 KB
testcase_31 AC 13 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<queue>
#include<cassert>
using namespace std;
int H,W,K,L,R;
string S[500];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
string dir="RDLU";
int dist[2][500][500];
string f(int t,int i,int j)
{
	string ret="";
	while(dist[t][i][j]>0)
	{
		for(int r=0;r<4;r++)
		{
			if(t==0)
			{
				int x=i-dx[r],y=j-dy[r];
				if(x>=0&&y>=0&&x<H&&y<W&&dist[t][x][y]==dist[t][i][j]-1)
				{
					i=x,j=y;
					ret+=dir[r];
					break;
				}
			}
			else
			{
				int x=i+dx[r],y=j+dy[r];
				if(x>=0&&y>=0&&x<H&&y<W&&dist[t][x][y]==dist[t][i][j]-1)
				{
					i=x,j=y;
					ret+=dir[r];
					break;
				}
			}
		}
	}
	return ret;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>H>>W>>K>>L>>R;L--;
	for(int i=0;i<H;i++)cin>>S[i];
	if((R-L)%2==1)
	{
		cout<<"No"<<endl;
		return 0;
	}
	for(int t=0;t<2;t++)
	{
		for(int i=0;i<H;i++)for(int j=0;j<W;j++)dist[t][i][j]=1e9;
		queue<pair<int,int> >Q;
		if(t==0)
		{
			dist[t][0][0]=0;
			Q.push(make_pair(0,0));
		}
		else
		{
			dist[t][H-1][W-1]=0;
			Q.push(make_pair(H-1,W-1));
		}
		while(!Q.empty())
		{
			int x=Q.front().first,y=Q.front().second;
			Q.pop();
			for(int r=0;r<4;r++)
			{
				int tx=x+dx[r],ty=y+dy[r];
				if(tx<0||ty<0||tx>=H||ty>=W||S[tx][ty]=='#'||dist[t][tx][ty]<=dist[t][x][y]+1)continue;
				dist[t][tx][ty]=dist[t][x][y]+1;
				Q.push(make_pair(tx,ty));
			}
		}
	}
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)if(S[i][j]=='.')
	{
		bool X=false,Y=false;
		if(i>0&&i+1<H&&S[i-1][j]=='.'&&S[i+1][j]=='.')X=true;
		if(j>0&&j+1<W&&S[i][j-1]=='.'&&S[i][j+1]=='.')Y=true;
		if(!X&&!Y)continue;
		int b=L-dist[0][i][j],a=K-R-dist[1][i][j];
		if(b<0||b%2==1||a<0||a%2==1)continue;
		cout<<"Yes\n";
		string B=f(0,i,j);
		reverse(B.begin(),B.end());
		cout<<B;
		int c=a+b+R-L;
		for(int t=0;t<c;t+=2)
		{
			cout<<(X?"DU":"LR");
		}
		string A=f(1,i,j);
		cout<<A<<endl;
		return 0;
	}
	cout<<"No"<<endl;
}
0