結果
| 問題 | No.2411 Reverse Directions | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-08-16 13:21:06 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 17 ms / 2,000 ms | 
| コード長 | 4,994 bytes | 
| コンパイル時間 | 2,028 ms | 
| コンパイル使用メモリ | 184,512 KB | 
| 実行使用メモリ | 6,824 KB | 
| 最終ジャッジ日時 | 2024-11-25 02:26:47 | 
| 合計ジャッジ時間 | 3,654 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 29 | 
ソースコード
#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(l - dist1[y][x] & 1) continue;
            if(y >= 1 && y + 1 < h && A[y - 1][x] != '#' && A[y + 1][x] != '#'){
                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';
}
            
            
            
        