結果

問題 No.2411 Reverse Directions
ユーザー i_am_noobi_am_noob
提出日時 2023-08-11 21:53:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,424 bytes
コンパイル時間 2,269 ms
コンパイル使用メモリ 208,788 KB
実行使用メモリ 8,664 KB
最終ジャッジ日時 2024-04-29 12:47:35
合計ジャッジ時間 3,941 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,632 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 3 ms
5,888 KB
testcase_03 AC 3 ms
5,632 KB
testcase_04 AC 7 ms
6,452 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,760 KB
testcase_07 AC 5 ms
7,936 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 14 ms
8,664 KB
testcase_11 AC 4 ms
5,632 KB
testcase_12 AC 18 ms
7,756 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 8 ms
7,808 KB
testcase_15 AC 5 ms
6,060 KB
testcase_16 AC 4 ms
6,272 KB
testcase_17 AC 8 ms
6,400 KB
testcase_18 AC 6 ms
6,004 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 7 ms
6,400 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 15 ms
7,716 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 17 ms
7,856 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 4 ms
5,888 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 12 ms
7,168 KB
testcase_29 AC 19 ms
7,680 KB
testcase_30 AC 14 ms
7,424 KB
testcase_31 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;

#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())

const int N=505,dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
const string mv="LRUD";

int n,m,k,l,r,dis[2][N][N],par[2][N][N];
bool vis[N][N];
string a[N];

bool valid(int i, int j){return i>=0&&i<n&&j>=0&&j<m&&a[i][j]=='.';}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> n >> m >> k >> l >> r; l--,r--;
    if((l+r)%2==0||(n+m+k)%2){
        cout << "No\n";
        return 0;
    }
    for(int i=0; i<n; ++i) cin >> a[i];
    memset(dis,0x3f,sizeof dis);
    for(int _=0; _<2; ++_){
        int x0,y0;
        if(_==0) x0=y0=0;
        else x0=n-1,y0=m-1;
        queue<pii> q;
        memset(vis,0,sizeof vis);
        q.push({x0,y0}),dis[_][x0][y0]=0,vis[x0][y0]=1,par[_][x0][y0]=-1;
        while(!q.empty()){
            auto [x,y]=q.front(); q.pop();
            for(int i=0; i<4; ++i){
                int nx=x+dx[i],ny=y+dy[i];
                if(valid(nx,ny)&&!vis[nx][ny]){
                    q.push({nx,ny}),dis[_][nx][ny]=dis[_][x][y]+1,par[_][nx][ny]=i,vis[nx][ny]=1;
                }
            }
        }
    }
    for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) if(a[i][j]!='#'&&dis[0][i][j]<=l&&dis[1][i][j]<=k-1-r&&(dis[0][i][j]+l)%2==0){
        if((valid(i,j-1)&&valid(i,j+1))||(valid(i-1,j)&&valid(i+1,j))){
            string res1,res2;
            int x=i,y=j;
            //cout << i << ' ' << j << endl;
            while(~par[0][x][y]){
                int tmp=par[0][x][y];
                res1+=mv[tmp];
                //cout << "tmp " << tmp << ' ' << x << ' ' << y << endl;
                x-=dx[tmp],y-=dy[tmp];
                //cout << x << ' ' << y << endl;
            }
            x=i,y=j;
            while(~par[1][x][y]){
                int tmp=par[1][x][y];
                res2+=mv[tmp^1];
                x-=dx[tmp],y-=dy[tmp];
            }
            reverse(all(res1));
            //cout << res1 << ' ' << res2 << endl;
            if(valid(i,j-1)&&valid(i,j+1)){
                while(sz(res1)+sz(res2)<k) res1+="LR";
            }
            else{
                while(sz(res1)+sz(res2)<k) res1+="UD";
            }
            res1+=res2;
            assert(sz(res1)==k);
            cout << "Yes\n" << res1 << "\n";
            return 0;
        }
    }
    cout << "No\n";
}
0