結果

問題 No.2411 Reverse Directions
ユーザー 👑 AngrySadEightAngrySadEight
提出日時 2023-07-15 14:19:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 6,624 bytes
コンパイル時間 1,329 ms
コンパイル使用メモリ 96,456 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2024-10-09 18:41:32
合計ジャッジ時間 3,711 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 6 ms
6,820 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 11 ms
6,912 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 AC 23 ms
7,976 KB
testcase_11 AC 2 ms
6,816 KB
testcase_12 AC 25 ms
6,820 KB
testcase_13 AC 3 ms
6,816 KB
testcase_14 AC 8 ms
6,816 KB
testcase_15 AC 4 ms
6,816 KB
testcase_16 AC 3 ms
6,816 KB
testcase_17 AC 9 ms
6,820 KB
testcase_18 AC 5 ms
6,816 KB
testcase_19 AC 2 ms
6,816 KB
testcase_20 AC 8 ms
6,816 KB
testcase_21 AC 4 ms
6,820 KB
testcase_22 AC 18 ms
6,816 KB
testcase_23 AC 3 ms
6,816 KB
testcase_24 AC 23 ms
6,820 KB
testcase_25 AC 2 ms
6,816 KB
testcase_26 AC 3 ms
6,820 KB
testcase_27 AC 3 ms
6,820 KB
testcase_28 AC 13 ms
6,820 KB
testcase_29 AC 26 ms
6,816 KB
testcase_30 AC 17 ms
6,816 KB
testcase_31 AC 5 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using pii = pair<int, int>;

int main() {
    int H, W, K, L, R;
    cin >> H >> W >> K >> L >> R;
    vector<string> S(H);
    for (int i = 0; i < H; i++) {
        cin >> S[i];
    }

    if ((R - L + 1) % 2 == 1) {
        cout << "No" << endl;
        return 0;
    }
    if ((H + W - 2) % 2 != K % 2) {
        cout << "No" << endl;
        return 0;
    }
    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};
    vector<char> c = {'D', 'R', 'U', 'L'};

    vector<vector<int>> dist_s(H, vector<int>(W, 10000002));
    vector<vector<int>> dist_t(H, vector<int>(W, 10000002));
    queue<pii> que;
    dist_s[0][0] = 0;
    que.push(pii(0, 0));

    while (que.size()) {
        pii p = que.front();
        que.pop();
        for (int i = 0; i < 4; i++) {
            if (p.first + dx[i] >= 0 && p.first + dx[i] < H &&
                p.second + dy[i] >= 0 && p.second + dy[i] < W) {
                if (S[p.first + dx[i]][p.second + dy[i]] == '#') {
                    continue;
                }
                if (dist_s[p.first + dx[i]][p.second + dy[i]] > H * W) {
                    dist_s[p.first + dx[i]][p.second + dy[i]] =
                        dist_s[p.first][p.second] + 1;
                    que.push(pii(p.first + dx[i], p.second + dy[i]));
                }
            }
        }
    }

    dist_t[H - 1][W - 1] = 0;
    que.push(pii(H - 1, W - 1));

    while (que.size()) {
        pii p = que.front();
        que.pop();
        for (int i = 0; i < 4; i++) {
            if (p.first + dx[i] >= 0 && p.first + dx[i] < H &&
                p.second + dy[i] >= 0 && p.second + dy[i] < W) {
                if (S[p.first + dx[i]][p.second + dy[i]] == '#') {
                    continue;
                }
                if (dist_t[p.first + dx[i]][p.second + dy[i]] > H * W) {
                    dist_t[p.first + dx[i]][p.second + dy[i]] =
                        dist_t[p.first][p.second] + 1;
                    que.push(pii(p.first + dx[i], p.second + dy[i]));
                }
            }
        }
    }
    /*for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cout << dist_s[i][j] << " ";
        }
        cout << endl;
    }
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cout << dist_t[i][j] << " ";
        }
        cout << endl;
    }*/
    vector<vector<int>> dir(H, vector<int>(W, -1));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (i > 0 && i < H - 1) {
                if (S[i][j] == '.' && S[i - 1][j] == '.' &&
                    S[i + 1][j] == '.') {
                    dir[i][j] = 1;
                }
            }
            if (j > 0 && j < W - 1) {
                if (S[i][j] == '.' && S[i][j - 1] == '.' &&
                    S[i][j + 1] == '.') {
                    dir[i][j] = 2;
                }
            }
        }
    }
    int idx_x = -1;
    int idx_y = -1;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (dir[i][j] >= 1) {
                if (dist_s[i][j] > L - 1) {
                    continue;
                }
                if (abs(dist_s[i][j] - (L - 1)) % 2 == 1) {
                    continue;
                }
                if (dist_t[i][j] > K - R) {
                    continue;
                }
                if (abs(dist_t[i][j] - (K - R)) % 2 == 1) {
                    continue;
                }
                idx_x = i;
                idx_y = j;
            }
        }
    }
    if (idx_x == -1) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
        string ans_str = "";

        pii now_idx = pii(idx_x, idx_y);
        string ss = "";
        while (true) {
            if (now_idx.first == 0 && now_idx.second == 0) {
                break;
            }
            for (int i = 0; i < 4; i++) {
                if (now_idx.first + dx[i] >= 0 && now_idx.first + dx[i] < H &&
                    now_idx.second + dy[i] >= 0 && now_idx.second + dy[i] < W) {
                    if (dist_s[now_idx.first + dx[i]][now_idx.second + dy[i]] ==
                        dist_s[now_idx.first][now_idx.second] - 1) {
                        now_idx.first += dx[i];
                        now_idx.second += dy[i];
                        ss += c[(i + 2) % 4];
                    }
                }
            }
        }
        while (true) {
            if (ss.size() == L - 1) {
                break;
            }
            if (ss[ss.size() - 1] == 'L') {
                ss += "RL";
            }
            if (ss[ss.size() - 1] == 'R') {
                ss += "LR";
            }
            if (ss[ss.size() - 1] == 'U') {
                ss += "DU";
            }
            if (ss[ss.size() - 1] == 'D') {
                ss += "UD";
            }
        }
        reverse(ss.begin(), ss.end());
        ans_str += ss;

        if (dir[idx_x][idx_y] == 1) {
            for (int i = 0; i < (R - L + 1) / 2; i++) {
                ans_str += "UD";
            }
        } else {
            for (int i = 0; i < (R - L + 1) / 2; i++) {
                ans_str += "LR";
            }
        }

        now_idx.first = idx_x;
        now_idx.second = idx_y;
        string st = "";
        while (true) {
            if (now_idx.first == H - 1 && now_idx.second == W - 1) {
                break;
            }
            for (int i = 0; i < 4; i++) {
                if (now_idx.first + dx[i] >= 0 && now_idx.first + dx[i] < H &&
                    now_idx.second + dy[i] >= 0 && now_idx.second + dy[i] < W) {
                    if (dist_t[now_idx.first + dx[i]][now_idx.second + dy[i]] ==
                        dist_t[now_idx.first][now_idx.second] - 1) {
                        now_idx.first += dx[i];
                        now_idx.second += dy[i];
                        st += c[i];
                    }
                }
            }
        }
        while (true) {
            if (st.size() == K - R) {
                break;
            }
            if (st[st.size() - 1] == 'L') {
                st += "RL";
            }
            if (st[st.size() - 1] == 'R') {
                st += "LR";
            }
            if (st[st.size() - 1] == 'U') {
                st += "DU";
            }
            if (st[st.size() - 1] == 'D') {
                st += "UD";
            }
        }
        ans_str += st;
        cout << ans_str << endl;
    }
}
0