結果
問題 | No.2411 Reverse Directions |
ユーザー |
![]() |
提出日時 | 2023-08-11 23:29:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 22 ms / 2,000 ms |
コード長 | 3,233 bytes |
コンパイル時間 | 2,231 ms |
コンパイル使用メモリ | 206,684 KB |
最終ジャッジ日時 | 2025-02-16 02:10:24 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#define rep(i,n) for(int i=0;i<(int)(n);i++)#define ALL(v) v.begin(),v.end()typedef long long ll;#include<bits/stdc++.h>using namespace std;int dh[]={1,0,-1,0};int dw[]={0,1,0,-1};int dist[505][505];int dist1[505][505];const int INF=1e9;pair<int,int> pre[505][505];pair<int,int> pre1[505][505];int main(){ios::sync_with_stdio(false);std::cin.tie(nullptr);rep(i,505) rep(j,505) dist[i][j]=INF;rep(i,505) rep(j,505) dist1[i][j]=INF;int h,w,k,l,r;cin>>h>>w>>k>>l>>r;vector<string> S(h);rep(i,h) cin>>S[i];if((r-l)%2==0 || h+w-2>k || (h+w)%2!=k%2){cout<<"No"<<endl;return 0;}queue<pair<int,int>> que;que.push({0,0});dist[0][0]=0;pre[0][0]={-1,-1};while(que.size()){auto t=que.front(); que.pop();int i=t.first,j=t.second;rep(e,4){int ni=i+dh[e],nj=j+dw[e];if(ni<0 || nj<0 || ni>=h || nj>=w) continue;if(S[ni][nj]=='#') continue;if(dist[i][j]+1<dist[ni][nj]){dist[ni][nj]=dist[i][j]+1;que.push({ni,nj});pre[ni][nj]={i,j};}}}if(dist[h-1][w-1]==INF){cout<<"No"<<endl;return 0;}que.push({h-1,w-1});dist1[h-1][w-1]=0;pre1[h-1][w-1]={-1,-1};while(que.size()){auto t=que.front(); que.pop();int i=t.first,j=t.second;rep(e,4){int ni=i+dh[e],nj=j+dw[e];if(ni<0 || nj<0 || ni>=h || nj>=w) continue;if(S[ni][nj]=='#') continue;if(dist1[i][j]+1<dist1[ni][nj]){dist1[ni][nj]=dist1[i][j]+1;que.push({ni,nj});pre1[ni][nj]={i,j};}}}rep(i,h) rep(j,w){if((i+j)%2==l%2) continue;if(dist[i][j]>l) continue;if(dist1[i][j]>k-r) continue;if(j>0 && j<w-1 && dist[i][j-1]!=INF && dist[i][j+1]!=INF){cout<<"Yes"<<endl;int d=k-dist[i][j]-dist1[i][j];string a,b,c;int ni=i,nj=j;while(1){int p=pre[ni][nj].first,q=pre[ni][nj].second;if(p==-1) break;if(ni<p) a+="U";else if(ni>p) a+="D";else if(nj<q) a+="L";else a+="R";ni=p,nj=q;}reverse(ALL(a));rep(_,d/2) b+="RL";ni=i,nj=j;while(1){int p=pre1[ni][nj].first,q=pre1[ni][nj].second;if(p==-1) break;if(ni<p) c+="D";else if(ni>p) c+="U";else if(nj<q) c+="R";else c+="L";ni=p,nj=q;}cout<<a<<b<<c<<endl;return 0;}if(i>0 && i<h-1 && dist[i-1][j]!=INF && dist[i+1][j]!=INF){cout<<"Yes"<<endl;int d=k-dist[i][j]-dist1[i][j];string a,b,c;int ni=i,nj=j;while(1){int p=pre[ni][nj].first,q=pre[ni][nj].second;if(p==-1) break;if(ni<p) a+="U";else if(ni>p) a+="D";else if(nj<q) a+="L";else a+="R";ni=p,nj=q;}reverse(ALL(a));rep(_,d/2) b+="UD";ni=i,nj=j;while(1){int p=pre1[ni][nj].first,q=pre1[ni][nj].second;if(p==-1) break;if(ni<p) c+="D";else if(ni>p) c+="U";else if(nj<q) c+="R";else c+="L";ni=p,nj=q;}cout<<a<<b<<c<<endl;return 0;}}cout<<"No"<<endl;return 0;}