結果

問題 No.2411 Reverse Directions
ユーザー umimelumimel
提出日時 2023-08-12 01:45:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 6,736 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 195,828 KB
実行使用メモリ 820,644 KB
最終ジャッジ日時 2024-11-18 21:48:01
合計ジャッジ時間 36,595 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,632 KB
testcase_01 AC 1 ms
46,584 KB
testcase_02 AC 2 ms
13,636 KB
testcase_03 AC 2 ms
125,156 KB
testcase_04 AC 6 ms
13,640 KB
testcase_05 AC 2 ms
80,184 KB
testcase_06 AC 2 ms
13,636 KB
testcase_07 AC 385 ms
82,964 KB
testcase_08 AC 2 ms
13,636 KB
testcase_09 AC 2 ms
13,636 KB
testcase_10 MLE -
testcase_11 WA -
testcase_12 WA -
testcase_13 TLE -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 593 ms
8,832 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
6,688 KB
testcase_20 WA -
testcase_21 AC 2 ms
6,692 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 WA -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 2 ms
6,688 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 AC 2 ms
88,964 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<string>> sdir(H, vector<string>(W, ""));
    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;
            sdir[nxt_i][nxt_j] = sdir[now_i][now_j]+ddir[k];
            Q.push({nxt_i, nxt_j});
        }
    }

    vector<vector<int>> tdist(H, vector<int>(W, -1));
    vector<vector<string>> tdir(H, vector<string>(W, ""));
    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;
            tdir[nxt_i][nxt_j] = rdir[k] + tdir[now_i][now_j];
            Q.push({nxt_i, nxt_j});
        }
    }

    //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;

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

        //横側を確認
        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デス
                    //L~R回目までの移動
                    rep(k, (R-L+1)/2){
                        ans+="LR";
                    }

                    //R+1~K回目までの行動
                    ans+=tdir[si][sj];
                    x = alloc[tdir[si][sj].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デス
                    //L~R回目までの移動
                    rep(k, (R-L+1)/2){
                        ans+="LR";
                    }
                    ans+="L";

                    //R+1~K回目までの行動
                    ans+=tdir[si][sj-1];
                    x = alloc[tdir[si][sj-1].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デス
                    //L~R回目までの移動
                    rep(k, (R-L+1)/2){
                        ans+="UD";
                    }
                    //R+1~K回目までの行動
                    ans+=tdir[si][sj];
                    x = alloc[tdir[si][sj].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デス
                    //L~R回目までの移動
                    rep(k, (R-L+1)/2){
                        ans+="UD";
                    }
                    ans+="U";
                    //R+1~K回目までの行動
                    ans+=tdir[si-1][sj];
                    x = alloc[tdir[si-1][sj].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