結果
| 問題 | No.2411 Reverse Directions | 
| コンテスト | |
| ユーザー |  planes | 
| 提出日時 | 2023-11-14 17:23:51 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 21 ms / 2,000 ms | 
| コード長 | 3,587 bytes | 
| コンパイル時間 | 2,246 ms | 
| コンパイル使用メモリ | 184,664 KB | 
| 実行使用メモリ | 7,936 KB | 
| 最終ジャッジ日時 | 2024-09-26 03:37:29 | 
| 合計ジャッジ時間 | 4,012 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 29 | 
ソースコード
  #include <bits/stdc++.h> 
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
 #define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
ll INF=2e18;
vector<ll> A={1,0,-1,0};
vector<ll> B={0,1,0,-1};
string t="ULDR";
string T="DRUL";
int main() {
    ios::sync_with_stdio(false);
  cin.tie(0);
  
  ll H,W,K,L,R;cin>>H>>W>>K>>L>>R;
  
  vector<string> S(H);
  for(ll i=0;i<H;i++) cin>>S[i];
  if((R-L+1)%2!=0) {
    cout<<"No"<<endl;
    return 0;
  }
  if((H+W-2)%2!=K%2) {
    cout<<"No"<<endl;
    return 0;
  }
vector<vector<ll>> d(H,vector<ll> (W,INF));
queue<pair<ll,ll>> Q;
Q.push(make_pair(0,0));
d[0][0]=0;
while(!Q.empty()) {
  auto q=Q.front();
  Q.pop();
for(ll i=0;i<4;i++) {
  ll x=q.first+A[i];
  ll y=q.second+B[i];
  if(x<0||y<0||x>=H||y>=W) continue;
  if(S[x][y]=='#') continue;
  if(d[x][y]==INF) {
    d[x][y]=d[q.first][q.second]+1;
    Q.push(make_pair(x,y));
  }
}
}
vector<vector<ll>> rd(H,vector<ll> (W,INF));
rd[H-1][W-1]=0;
Q.push(make_pair(H-1,W-1));
while(!Q.empty()) {
auto q=Q.front();
  Q.pop();
for(ll i=0;i<4;i++) {
  ll x=q.first+A[i];
  ll y=q.second+B[i];
  if(x<0||y<0||x>=H||y>=W) continue;
  if(S[x][y]=='#') continue;
  if(rd[x][y]==INF) {
    rd[x][y]=rd[q.first][q.second]+1;
    Q.push(make_pair(x,y));
  }
}
}
for(ll i=0;i<H;i++) {
  for(ll j=0;j<W;j++) {
    //横行けるか
  if(j>0&&j<W-1&&S[i][j-1]=='.'&&S[i][j+1]=='.') {
    if(d[i][j]<=L-1&&d[i][j]%2==(L-1)%2&&rd[i][j]<=K-R&&rd[i][j]%2==(K-R)%2) {
    vector<char> ans(K,'n');
    ll now=d[i][j];
    pair<ll,ll> p=make_pair(i,j);
    while(now>0) {
      for(ll h=0;h<4;h++) {
        ll x=p.first+A[h];
        ll y=p.second+B[h];
        if(x<0||x>=H||y<0||y>=W) continue;
        if(d[x][y]==now-1) {
       now--;
       p=make_pair(x,y);
       ans[now]=t[h];
       break;
        }
      }
    }
    now=rd[i][j];
    p=make_pair(i,j);
    while(now>0) {
      for(ll h=0;h<4;h++) {
        ll x=p.first+A[h];
        ll y=p.second+B[h];
        if(x<0||x>=H||y<0||y>=W) continue;
        if(rd[x][y]==now-1) {
          now--;
          p=make_pair(x,y);
          ans[K-1-now]=T[h];
          break;
        }
      }
    }
    ll count=0;
    for(ll h=0;h<K;h++) {
      if(ans[h]=='n') {
        if(count%2==0) ans[h]='L';
        else ans[h]='R';
        count++;
      }
    }
    cout<<"Yes"<<endl;
    for(auto x:ans) cout<<x;
    cout<<endl;
    return 0;
    }
  }
  if(i>0&&i<H-1&&S[i-1][j]=='.'&&S[i+1][j]=='.') {
    if(d[i][j]<=L-1&&d[i][j]%2==(L-1)%2&&rd[i][j]<=K-R&&rd[i][j]%2==(K-R)%2) {
    vector<char> ans(K,'n');
    ll now=d[i][j];
    pair<ll,ll> p=make_pair(i,j);
    while(now>0) {
      for(ll h=0;h<4;h++) {
        ll x=p.first+A[h];
        ll y=p.second+B[h];
        if(x<0||x>=H||y<0||y>=W) continue;
        if(d[x][y]==now-1) {
       now--;
       p=make_pair(x,y);
       ans[now]=t[h];
       break;
        }
      }
    }
    now=rd[i][j];
    p=make_pair(i,j);
    while(now>0) {
      for(ll h=0;h<4;h++) {
        ll x=p.first+A[h];
        ll y=p.second+B[h];
        if(x<0||x>=H||y<0||y>=W) continue;
        if(rd[x][y]==now-1) {
          now--;
          p=make_pair(x,y);
          ans[K-1-now]=T[h];
          break;
        }
      }
    }
    ll count=0;
    for(ll h=0;h<K;h++) {
      if(ans[h]=='n') {
        if(count%2==0) ans[h]='U';
        else ans[h]='D';
        count++;
      }
    }
    cout<<"Yes"<<endl;
    for(auto x:ans) cout<<x;
    cout<<endl;
    return 0;
    }
  }
  }
}
cout<<"No"<<endl;
}
    
          
    
            
            
            
        