結果

問題 No.2411 Reverse Directions
ユーザー milanis48663220milanis48663220
提出日時 2023-08-11 22:34:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,061 bytes
コンパイル時間 1,795 ms
コンパイル使用メモリ 141,296 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 13:37:35
合計ジャッジ時間 5,306 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using P = pair<int, int>;
using T = tuple<int, int, int>;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w, k, l, r; cin >> h >> w >> k >> l >> r; l--;
    vector<string> s(h);
    for(int i = 0; i < h; i++) cin >> s[i];
    auto fx = [&](int i, int j){
        if(i-1 >= 0 && i+1 < h){
            if(s[i-1][j] == '.' && s[i][j] == '.' && s[i+1][j] == '.') return true;
        }
        return false;
    };
    auto fy = [&](int i, int j){
        if(j-1 >= 0 && j+1 < w){
            if(s[i][j-1] == '.' && s[i][j] == '.' && s[i][j+1] == '.') return true;
        }
        return false;
    };
    auto f = [&](int i, int j){
        return fx(i, j) || fy(i, j);
    };
    if(k < h+w-2){
        cout << "No" << endl;
        return 0;
    }
    
    if(r%2 != l%2){
        cout << "No" << endl;
        return 0;
    }
    if((h+w-2) != k-(r-l)){
        cout << "No" << endl;
        return 0;
    }
    auto pre = vec3d(h, w, 2, T(-1, -1, -1));
    auto ok = vec3d(h, w, 2, false);
    ok[0][0][0] = true;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            for(int k = 0; k < 2; k++){
                if(!ok[i][j][k]) continue;
                if(i+1 < h && s[i+1][j] == '.'){
                    int kk = k;
                    if(f(i+1, j) && i+j+1 == l) kk = 1;
                    ok[i+1][j][kk] = true;
                    pre[i+1][j][kk] = {i, j, k};
                }
                if(j+1 < w){
                    int kk = k;
                    if(f(i, j+1) && i+j+1 == l) kk = 1;
                    ok[i][j+1][kk] = true;
                    pre[i][j+1][kk] = {i, j, k};
                }
            }
        }
    }
    if(!ok[h-1][w-1][1]){
        cout << "No" << endl;
        return 0;
    }
    int extra = k-(h-1)-(w-1);
    int x = h-1, y = w-1, p = 1;
    if(!ok[h-1][w-1][1]) p = 0;
    vector<P> path = {P(x, y)};
    while(true){
        auto [xx, yy, pp] = pre[x][y][p];
        x = xx;
        y = yy;
        p = pp;
        path.push_back({x, y});
        if(x == 0 && y == 0) break;
    }
    reverse(path.begin(), path.end());
    int len = path.size();
    string ans;
    for(int i = 0; i < len-1; i++){
        auto [x, y] = path[i];
        auto [xx, yy] = path[i+1];
        if(xx == x+1){
            ans += 'D';
        }
        if(yy == y+1){
            ans += 'R';
        }
        if(ans.size() == l){
            if(fx(xx, yy)){
                while(extra){
                    ans += "UD";
                    extra -= 2;
                }
            }else{
                assert(fy(xx, yy));
                while(extra){
                    ans += "LR";
                    extra -= 2;
                }
            }
        }
    }
    cout << ans << endl;
    return 1;
}
0