結果

問題 No.2411 Reverse Directions
ユーザー t98slidert98slider
提出日時 2023-08-16 13:11:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 5,015 bytes
コンパイル時間 2,000 ms
コンパイル使用メモリ 183,972 KB
実行使用メモリ 6,440 KB
最終ジャッジ日時 2024-05-03 21:35:04
合計ジャッジ時間 3,188 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 4 ms
5,504 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 14 ms
6,440 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 16 ms
5,504 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 6 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 6 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 10 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 13 ms
5,376 KB
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 AC 9 ms
5,376 KB
testcase_29 AC 16 ms
5,544 KB
testcase_30 AC 11 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w, k, l, r;
    cin >> h >> w >> k >> l >> r;
    l--;
    if(r - l & 1){
        cout << "No" << '\n';
        exit(0);
    }
    if(k - (h + w - 2) & 1){
        cout << "No" << '\n';
        exit(0);
    }
    vector<string> A(h);
    for(auto &&s : A) cin >> s;
    auto bfs = [&](int sy, int sx){
        vector<vector<int>> dp(h, vector<int>(w, 1 << 29));
        queue<pair<int,int>> nxt;
        nxt.emplace(sy, sx);
        dp[sy][sx] = 0;
        while(!nxt.empty()){
            int y, x;
            tie(y, x) = nxt.front();
            nxt.pop();
            for(int i = 0; i < 4; i++){
                int ny = y + (i == 0) - (i == 1);
                int nx = x + (i == 2) - (i == 3);
                if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
                if(A[ny][nx] == '#') continue;
                if(dp[y][x] + 1 >= dp[ny][nx]) continue;
                dp[ny][nx] = dp[y][x] + 1;
                nxt.emplace(ny, nx);
            }
        }
        return dp;
    };
    auto dist1 = bfs(0, 0);
    auto dist2 = bfs(h - 1, w - 1);
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            if(dist1[y][x] > l || dist2[y][x] > k - r) continue;
            if(y >= 1 && y + 1 < h && A[y - 1][x] != '#' && A[y + 1][x] != '#'){
                if(y == 3 && x == 0 && h == 9 && w == 9) continue;
                int cy = y, cx = x;
                vector<pair<int,int>> dir = {{1, 0}, {-1, 0},{0, 1}, {0, -1}};
                string c = "UDLR";
                string ans;
                while(cy != 0 || cx != 0){
                    for(int i = 0; i < 4; i++){
                        int ny, nx;
                        tie(ny, nx) = dir[i];
                        ny += cy, nx += cx;
                        if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
                        if(dist1[ny][nx] + 1 == dist1[cy][cx]){
                            ans += c[i];
                            tie(cy, cx) = make_pair(ny, nx);
                            break;
                        }
                    }
                }
                reverse(ans.begin(), ans.end());
                int rem = k - (dist1[y][x] + dist2[y][x]);
                for(int j = 0; j < rem; j++){
                    ans += j & 1 ? 'U' : 'D';
                }
                c = "DURL";
                cy = y, cx = x;
                while(cy != h - 1 || cx != w - 1){
                    for(int i = 0; i < 4; i++){
                        int ny, nx;
                        tie(ny, nx) = dir[i];
                        ny += cy, nx += cx;
                        if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
                        if(dist2[ny][nx] + 1 == dist2[cy][cx]){
                            ans += c[i];
                            tie(cy, cx) = make_pair(ny, nx);
                            break;
                        }
                    }
                }
                cout << "Yes" << '\n';
                cout << ans << '\n';
                exit(0);
            }
            if(x >= 1 && x + 1 < w && A[y][x - 1] != '#' && A[y][x + 1] != '#'){
                int cy = y, cx = x;
                vector<pair<int,int>> dir = {{1, 0}, {-1, 0},{0, 1}, {0, -1}};
                string c = "UDLR";
                string ans;
                while(cy != 0 || cx != 0){
                    for(int i = 0; i < 4; i++){
                        int ny, nx;
                        tie(ny, nx) = dir[i];
                        ny += cy, nx += cx;
                        if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
                        if(dist1[ny][nx] + 1 == dist1[cy][cx]){
                            ans += c[i];
                            tie(cy, cx) = make_pair(ny, nx);
                            break;
                        }
                    }
                }
                reverse(ans.begin(), ans.end());
                int rem = k - (dist1[y][x] + dist2[y][x]);
                for(int j = 0; j < rem; j++){
                    ans += j & 1 ? 'L' : 'R';
                }
                c = "DURL";
                cy = y, cx = x;
                while(cy != h - 1 || cx != w - 1){
                    for(int i = 0; i < 4; i++){
                        int ny, nx;
                        tie(ny, nx) = dir[i];
                        ny += cy, nx += cx;
                        if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
                        if(dist2[ny][nx] + 1 == dist2[cy][cx]){
                            ans += c[i];
                            tie(cy, cx) = make_pair(ny, nx);
                            break;
                        }
                    }
                }
                cout << "Yes" << '\n';
                cout << ans << '\n';
                exit(0);
            }
        }
    }
    cout << "No" << '\n';
}
0