結果

問題 No.2411 Reverse Directions
ユーザー umimelumimel
提出日時 2023-08-12 02:10:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 8,818 bytes
コンパイル時間 2,197 ms
コンパイル使用メモリ 197,812 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2024-04-29 16:51:12
合計ジャッジ時間 5,890 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 RE -
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 7 ms
9,472 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 WA -
testcase_10 AC 15 ms
10,368 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 7 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 RE -
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 2 ms
5,376 KB
testcase_28 WA -
testcase_29 AC 21 ms
7,936 KB
testcase_30 AC 12 ms
6,272 KB
testcase_31 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60;
const int IINF = (1 << 30) - 1;

template<typename T> struct Edge{
    int to; T w;
    Edge(int to_, T w_=1){
        to = to_;
        w=w_;
    }
};
template<typename T> using Tree = vector<vector<Edge<T>>>;
template<typename T> using Graph = vector<vector<Edge<T>>>;
/* 容量&重み付きエッジ for Dinic */
template<typename T> struct REdge{
    int to;
    T cap;
    T cost;
    int rev;
    REdge(int to_, T cap_, T cost_=1){
        to = to_;
        cap = cap_;
        cost = cost_;
    }
    
    REdge(int to_, T cap_, T cost_, int rev_){
        to = to_;
        cap = cap_;
        cost = cost_;
        rev = rev_;
    }
};

/* 残余グラフ for Dinic */
template<typename T> using RGraph = vector<vector<REdge<T>>>;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int H, W, K, L, R;
    cin >> H >> W >> K >> L >> R;
    vector<string> S(H); rep(i, H) cin >> S[i];

    if((H+W-2)%2 != K%2){
        cout << "No" << endl;
        return 0;
    }

    int di[4] = {0, 0, 1, -1};
    int dj[4] = {1, -1, 0, 0};
    char ddir[4] = {'R', 'L', 'D', 'U'};
    char rdir[4] = {'L', 'R', 'U', 'D'};
    map<char, int> alloc;
    alloc['R'] = 0;
    alloc['L'] = 1;
    alloc['D'] = 2;
    alloc['U'] = 3;
    queue<pll> Q;

    vector<vector<int>> sdist(H, vector<int>(W, -1));
    vector<vector<pair<int, int>>> spre(H, vector<pair<int, int>>(W, {-1, -1}));
    sdist[0][0] = 0;
    Q.push({0, 0});
    while(!Q.empty()){
        int now_i = Q.front().fi;
        int now_j = Q.front().se;
        Q.pop();
        for(int k=0; k<4; k++){
            int nxt_i = now_i + di[k];
            int nxt_j = now_j + dj[k];
            if(nxt_i<0||nxt_j<0||H<=nxt_i||W<=nxt_j) continue;
            if(S[nxt_i][nxt_j]=='#') continue;
            if(sdist[nxt_i][nxt_j]!=-1) continue;
            sdist[nxt_i][nxt_j] = sdist[now_i][now_j]+1;
            spre[nxt_i][nxt_j] = {now_i, now_j};
            Q.push({nxt_i, nxt_j});
        }
    }

    vector<vector<int>> tdist(H, vector<int>(W, -1));
    vector<vector<pair<int, int>>> tpost(H, vector<pair<int, int>>(W, {-1, -1}));
    tdist[H-1][W-1] = 0;
    Q.push({H-1, W-1});
    while(!Q.empty()){
        int now_i = Q.front().fi;
        int now_j = Q.front().se;
        Q.pop();
        for(int k=0; k<4; k++){
            int nxt_i = now_i + di[k];
            int nxt_j = now_j + dj[k];
            if(nxt_i<0||nxt_j<0||H<=nxt_i||W<=nxt_j) continue;
            if(S[nxt_i][nxt_j]=='#') continue;
            if(tdist[nxt_i][nxt_j]!=-1) continue;
            tdist[nxt_i][nxt_j] = tdist[now_i][now_j]+1;
            tpost[nxt_i][nxt_j] = {now_i, now_j};
            Q.push({nxt_i, nxt_j});
        }
    }

    function<string(int, int)> spath = [&](int si, int sj){
        string ret = "";
        int now_i=si, now_j=sj;
        while(!(now_i==0&&now_j==0)){
            int pre_i = spre[now_i][now_j].fi;
            int pre_j = spre[now_i][now_j].se;
            rep(k, 4){
                if(pre_i+di[k]==now_i && pre_j+dj[k]==now_j){
                    ret+=ddir[k];
                }
            }
            now_i = pre_i;
            now_j = pre_j;
        }

        return ret;
    };

    function<string(int, int)> tpath = [&](int si, int sj){
        string ret = "";
        int now_i=si, now_j=sj;
        while(!(now_i==H-1&&now_j==W-1)){
            int post_i = tpost[now_i][now_j].fi;
            int post_j = tpost[now_i][now_j].se;
            rep(k, 4){
                if(now_i+di[k]==post_i && now_j+dj[k]==post_j){
                    ret+=ddir[k];
                }
            }
            now_i = post_i;
            now_j = post_j;
        }

        return ret;
    };

    //L-1回目の行動後の到達頂点
    for(int si=0; si<H; si++) for(int sj=0; sj<W; sj++){
        if((si+sj)%2 != (L-1)%2) continue;
        if(sdist[si][sj]>L-1) continue;
        if(S[si][sj]=='#') continue;

        //横側を確認
        if(0<=sj-1 && sj+1<W){
            if(S[si][sj-1]=='.' && S[si][sj+1]=='.'){
                if((R-L+1)%2==0 && tdist[si][sj]<=K-R){
                    //okデス
            
                    string ans = "";
                    //1~L-1回目までの移動
                    ans += spath(si, sj);
                    int x = alloc[ans.back()];
                    rep(k, (L-1-sdist[si][sj])/2){
                        ans += rdir[x];
                        ans += ddir[x];
                    }

                    //L~R回目までの移動
                    rep(k, (R-L+1)/2){
                        ans+="LR";
                    }

                    //R+1~K回目までの行動
                    ans+=tpath(si, sj);
                    x = alloc[ans.back()];
                    int tmp = K-(int)ans.size();
                    rep(k, tmp/2){
                        ans+=rdir[x];
                        ans+=ddir[x];
                    }

                    cout << "Yes" << endl;
                    cout << ans << endl;
                    return 0;
                }else if((R-L+1)%2==1 && tdist[si][sj-1]<=K-R){
                    //okデス

                    string ans = "";
                    //1~L-1回目までの移動
                    ans += spath(si, sj);
                    int x = alloc[ans.back()];
                    rep(k, (L-1-sdist[si][sj])/2){
                        ans += rdir[x];
                        ans += ddir[x];
                    }

                    //L~R回目までの移動
                    rep(k, (R-L+1)/2){
                        ans+="LR";
                    }
                    ans+="L";

                    //R+1~K回目までの行動
                    ans+=tpath(si, sj-1);
                    x = alloc[ans.back()];
                    int tmp = K-(int)ans.size();
                    rep(k, tmp/2){
                        ans+=rdir[x];
                        ans+=ddir[x];
                    }

                    cout << "Yes" << endl;
                    cout << ans << endl;
                    return 0;
                }
            }
        }

        //縦側を確認
        if(0<=si-1 && si+1<H){
            if(S[si-1][sj]=='.' && S[si+1][sj]=='.'){
                if((R-L+1)%2==0 && tdist[si][sj]<=K-R){
                    //okデス

                    string ans = "";
                    //1~L-1回目までの移動
                    ans += spath(si, sj);
                    int x = alloc[ans.back()];
                    rep(k, (L-1-sdist[si][sj])/2){
                        ans += rdir[x];
                        ans += ddir[x];
                    }

                    //L~R回目までの移動
                    rep(k, (R-L+1)/2){
                        ans+="UD";
                    }
                    //R+1~K回目までの行動
                    ans+=tpath(si, sj);
                    x = alloc[ans.back()];
                    int tmp = K-(int)ans.size();
                    rep(k, tmp/2){
                        ans+=rdir[x];
                        ans+=ddir[x];
                    }

                    cout << "Yes" << endl;
                    cout << ans << endl;
                    return 0;
                }else if((R-L+1)%2==1 && tdist[si-1][sj]<=K-R){
                    //okデス

                    string ans = "";
                    //1~L-1回目までの移動
                    ans += spath(si, sj);
                    int x = alloc[ans.back()];
                    rep(k, (L-1-sdist[si][sj])/2){
                        ans += rdir[x];
                        ans += ddir[x];
                    }

                    //L~R回目までの移動
                    rep(k, (R-L+1)/2){
                        ans+="UD";
                    }
                    ans+="U";
                    //R+1~K回目までの行動
                    ans+=tpath(si-1, sj);
                    x = alloc[ans.back()];
                    int tmp = K-(int)ans.size();
                    rep(k, tmp/2){
                        ans+=rdir[x];
                        ans+=ddir[x];
                    }

                    cout << "Yes" << endl;
                    cout << ans << endl;
                    return 0;
                }
            }
        }
    }

    cout << "No" << endl;
}
0