結果

問題 No.2411 Reverse Directions
ユーザー polylogK
提出日時 2023-08-11 22:46:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,017 bytes
コンパイル時間 2,323 ms
コンパイル使用メモリ 210,264 KB
最終ジャッジ日時 2025-02-16 01:53:39
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21 WA * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:26:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   26 |     scanf("%d%d%d%d%d", &h, &w, &k, &l, &r);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

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;
    }

    std::vector<int> dy{0, 1, 0, -1};
    std::vector<int> dx{1, 0, -1, 0};
    std::string tos = "RDLU";
    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 d0 = f(0, 0);
    auto dn = f(h - 1, w - 1);

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

    std::string ans = "";
    {
        int y = 0, x = 0;
        while (y != h - 1 or x != w - 1) {
            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 (d0[y][x] + 1 + dn[ny][nx] != dn[0][0]) continue;

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

    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;
    };

    if (ans.size() <= l) {
        int dir = (g(ans.back()) + 2) % 4;
        while (ans.size() < k) {
            ans += tos[dir];
            ans += tos[(dir + 2) % 4];
        }
    } else {
        auto front = ans.substr(0, l);
        auto back = ans.substr(l, ans.size() - l);

        int dir = g(back.front());
        while (front.size() + back.size() < k) {
            front += tos[dir];
            front += tos[(dir + 2) % 4];
        }
        ans = front + back;
    }

    puts("Yes");
    std::cout << ans << std::endl;
    return 0;
}
0