結果

問題 No.2411 Reverse Directions
ユーザー polylogKpolylogK
提出日時 2023-08-11 23:23:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 3,569 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 215,888 KB
実行使用メモリ 8,188 KB
最終ジャッジ日時 2024-04-29 14:33:47
合計ジャッジ時間 4,295 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 5 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
6,144 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 28 ms
8,188 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 44 ms
6,400 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 12 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 15 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 13 ms
5,376 KB
testcase_21 AC 8 ms
5,376 KB
testcase_22 AC 32 ms
5,508 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 43 ms
6,536 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 6 ms
5,376 KB
testcase_28 AC 21 ms
5,416 KB
testcase_29 AC 43 ms
6,420 KB
testcase_30 AC 26 ms
5,848 KB
testcase_31 AC 13 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

#if defined(DONLINE_JUDGE)
#define NDEBUG
#elif defined(ONLINE_JUDGE)
#define NDEBUG
#endif

template <typename T>
std::vector<T> make_v(size_t a) {
    return std::vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
    int h, w, k, l, r;
    scanf("%d%d%d%d%d", &h, &w, &k, &l, &r);
    std::vector<std::string> s(h);
    for (auto& v : s) std::cin >> v;

    --l;

    if ((r - l) % 2) {
        puts("No");
        return 0;
    }
    if (l == 0 or r == k) {
        puts("No");
        return 0;
    }

    std::vector<int> dy{0, 1, 0, -1};
    std::vector<int> dx{1, 0, -1, 0};
    std::string tos = "RDLU";
    auto g = [](char c) {
        if (c == 'R') return 0;
        if (c == 'D') return 1;
        if (c == 'L') return 2;
        if (c == 'U') return 3;
        return -1;
    };

    auto f = [&](int sy, int sx) {
        std::vector d(h, std::vector<int>(w, 1 << 30));
        d[sy][sx] = 0;

        std::queue<std::pair<int, int>> qu({{sy, sx}});
        while (!qu.empty()) {
            auto [y, x] = qu.front();
            qu.pop();

            for (int k = 0; k < 4; ++k) {
                const int ny = y + dy[k];
                const int nx = x + dx[k];
                if (nx < 0 or ny < 0 or nx >= w or ny >= h) continue;
                if (s[ny][nx] == '#') continue;
                if (d[ny][nx] != 1 << 30) continue;

                qu.push({ny, nx});
                d[ny][nx] = d[y][x] + 1;
            }
        }
        return d;
    };

    auto sakina = [&](int y, int x, int ty, int tx) {
        auto ds = f(y, x);
        auto dt = f(ty, tx);

        std::string ans = "";
        while (y != ty or x != tx) {
            for (int k = 0; k < 4; ++k) {
                const int ny = y + dy[k];
                const int nx = x + dx[k];
                if (ny < 0 or nx < 0 or ny >= h or nx >= w) continue;
                if (s[ny][nx] == '#') continue;

                if (ds[y][x] + 1 + dt[ny][nx] != ds[ty][tx]) continue;

                ans += tos[k];
                y = ny;
                x = nx;
                break;
            }
        }
        return ans;
    };

    auto d0 = f(0, 0);
    auto dn = f(h - 1, w - 1);

    if (k % 2 != dn[0][0] % 2) {
        puts("No");
        return 0;
    }

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            if (s[i][j] == '#') continue;
            if (d0[i][j] > l) continue;
            if (d0[i][j] % 2 != l % 2) continue;
            if (dn[i][j] > k - r) continue;

            int dir = -1;
            if (i + 1 < h and i - 1 >= 0 and s[i + 1][j] == '.' and
                s[i - 1][j] == '.') {
                dir = 1;
            }
            if (j + 1 < w and j - 1 >= 0 and s[i][j + 1] == '.' and
                s[i][j - 1] == '.') {
                dir = 0;
            }

            if (dir == -1) continue;

            std::string front = sakina(0, 0, i, j);
            std::string back = sakina(i, j, h - 1, w - 1);
            while (front.size() + back.size() < k) {
                front += tos[dir];
                front += tos[(dir + 2) % 4];
            }

            puts("Yes");
            std::cout << front + back << std::endl;
            return 0;
        }
    }

    puts("No");
    return 0;
}
0