結果

問題 No.2411 Reverse Directions
ユーザー milanis48663220milanis48663220
提出日時 2023-08-11 23:35:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 5,644 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 138,084 KB
実行使用メモリ 12,928 KB
最終ジャッジ日時 2024-04-29 14:47:51
合計ジャッジ時間 4,416 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 11 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 9 ms
12,672 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 22 ms
12,928 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 8 ms
5,376 KB
testcase_18 AC 6 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 RE -
testcase_23 AC 2 ms
5,376 KB
testcase_24 RE -
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 RE -
testcase_29 AC 25 ms
9,600 KB
testcase_30 AC 18 ms
7,680 KB
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};

pair<vector<vector<int>>, vector<vector<P>>> calc_dist(vector<string> s, int sx, int sy){
    int h = s.size(), w = s[0].size();
    queue<P> que;
    auto dist = vec2d(h, w, -1);
    auto pre = vec2d(h, w, P(-1, -1));
    auto set_dist = [&](int i, int j, int d){
        if(dist[i][j] == -1){
            que.push({i, j});
            dist[i][j] = d;
            return true;
        }
        return false;
    };
    auto in_field = [&](int x, int y){
        return 0 <= x && x < h && 0 <= y && y < w;
    };
    set_dist(sx, sy, 0);
    while(!que.empty()){
        auto [x, y] = que.front(); que.pop();
        for(int k = 0; k < 4; k++) {
            int xx = x+dx[k];
            int yy = y+dy[k];
            if(!in_field(xx, yy)) continue;
            if(s[xx][yy] != '.') continue;
            if(set_dist(xx, yy, dist[x][y]+1)){
                pre[xx][yy] = {x, y};
            }
        }
    }
    return make_pair(dist, pre);
}

string inv(string s){
    string ans;
    for(char c: s){
        if(c == 'L') ans += 'R';
        if(c == 'R') ans += 'L';
        if(c == 'D') ans += 'U';
        if(c == 'U') ans += 'D';
    }
    return ans;
}

string make_path(int x, int y, vector<vector<P>> pre){
    string ans;
    while(true){
        auto [xx, yy] = pre[x][y];
        if(xx == -1){
            assert(yy == -1);
            break;
        }
        if(x == xx+1){
            ans += 'D';
        }else if(x == xx-1){
            ans += 'U';
        }else if(y == yy+1){
            ans += 'R';
        }else{
            assert(y == yy-1);
            ans += 'L';
        }
        x = xx;
        y = yy;
    }
    return ans;
}

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(r%2 != l%2){
        cout << "No" << endl;
        return 0;
    }
    if((h+w-2)%2 != (k-(r-l))%2){
        cout << "No" << endl;
        return 0;
    }
    
    if((h+w-2) > k-(r-l)){
        cout << "No" << endl;
        return 0;
    }
    auto in_field = [&](int x, int y){
        return 0 <= x && x < h && 0 <= y && y < w;
    };
    auto verify = [&](string t, int xt, int yt){
        int x = 0, y = 0;
        for(char c: t){
            if(c == 'D') x++;
            if(c == 'U') x--;
            if(c == 'L') y--;
            if(c == 'R') y++;
            assert(in_field(x, y));
            // assert(s[x][y] == '.');
        }
    };
    auto [ds, pre_s] = calc_dist(s, 0, 0);
    auto [dt, pre_t] = calc_dist(s, h-1, w-1);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(ds[i][j] == -1 || dt[i][j] == -1){
                continue;
            }
            if(!f(i, j)) continue;
            if((i+j)%2 != l%2) continue;
            if(ds[i][j] > l) continue;
            if(dt[i][j] > k-r) continue;
            int extra = k-(ds[i][j]+dt[i][j]);
            string ans = make_path(i, j, pre_s);
            auto ans_inv = ans;
            verify(ans, i, j);
            // cout << i << ' ' << j << endl;
            if(fx(i, j)){
                while(extra){
                    ans += "DU";
                    ans_inv += "UD";
                    extra-=2;
                }
            }else{
                assert(fy(i, j));
                while(extra){
                    ans += "LR";
                    ans_inv += "RL";
                    extra-=2;
                }
            }
            assert(ans.size() >= r);
            auto tail = inv(make_path(i, j, pre_t));
            ans += tail;
            ans_inv += tail;
            cout << "Yes" << endl;
            cout << ans << endl;
            assert(ans.size() == k);
            // verify(ans_inv);
            return 0;
        }
    }
    cout << "No" << endl;
    return 0;
}
0