結果

問題 No.2411 Reverse Directions
ユーザー planesplanes
提出日時 2023-11-14 17:23:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 3,587 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 185,628 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2023-11-14 17:23:56
合計ジャッジ時間 5,011 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

  #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;
}


    
          


    

0