結果

問題 No.2411 Reverse Directions
ユーザー PAKACHUPAKACHU
提出日時 2023-08-12 01:16:12
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,781 bytes
コンパイル時間 2,858 ms
コンパイル使用メモリ 169,088 KB
実行使用メモリ 10,744 KB
最終ジャッジ日時 2024-04-29 16:05:41
合計ジャッジ時間 5,454 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 12 ms
9,984 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 27 ms
7,964 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 WA -
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 8 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 19 ms
6,736 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 27 ms
8,192 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 14 ms
5,760 KB
testcase_29 AC 26 ms
7,936 KB
testcase_30 AC 17 ms
6,272 KB
testcase_31 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }, dy[8] = { 0, 1, 0, -1, 1, -1, 1, -1 };
const long long mod = 998244353;
const ll inf = 1LL << 60;
const int INF = 5e8;

struct dsu {
public:
    dsu()
        : _n(0)
    {
    }
    dsu(int n)
        : _n(n)
        , parent_or_size(n, -1)
    {
    }

    int merge(int a, int b)
    {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y)
            return x;
        if (-parent_or_size[x] < -parent_or_size[y])
            std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b)
    {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }
    // 頂点aの属する連結成分の代表元を返す
    int leader(int a)
    {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0)
            return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    // 頂点aの属する連結成分のサイズを返す
    int size(int a)
    {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }
    // グラフを連結成分に分け、その情報を返します。
    // 返り値は「「一つの連結成分の頂点番号のリスト」のリスト」です。
    //(内側外側限らず)vector内でどの順番で頂点が格納されているかは未定義です。
    std::vector<std::vector<int>> groups()
    {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

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 ((h + w) % 2 != k % 2 or (l - r + 1) % 2 or h - 1 + w - 1 > k) {
        cout << "No" << endl;
        return 0;
    }

    vector<vector<bool>> ok(h, vector<bool>(w));
    for (int i = 1; i < h - 1; i++) {
        for (int j = 1; j < w - 1; j++) {
            if (s[i][j] == '#')
                continue;
            if (s[i + 1][j] == '.' and s[i - 1][j] == '.')
                ok[i][j] = true;
            if (s[i][j + 1] == '.' and s[i][j - 1] == '.')
                ok[i][j] = true;
        }
    }

    vector<vector<int>> sd(h, vector<int>(w, -1)), gd(h, vector<int>(w, -1));
    vector<vector<pair<int, int>>> ps(h, vector<pair<int, int>>(w)), pd(h, vector<pair<int, int>>(w));
    queue<pair<int, int>> q;
    auto bfs = [&](int sx, int sy, vector<vector<int>>& dist, vector<vector<pair<int, int>>>& pre) -> void {
        q.emplace(sx, sy);
        dist[sx][sy] = 0;
        while (q.size()) {
            auto [x, y] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i], ny = y + dy[i];
                if (nx < 0 or ny < 0 or nx >= h or ny >= w or s[nx][ny] == '#')
                    continue;
                if (dist[nx][ny] != -1)
                    continue;
                dist[nx][ny] = dist[x][y] + 1;
                pre[nx][ny] = { x, y };
                q.emplace(nx, ny);
            }
        }
    };
    bfs(0, 0, sd, ps);
    bfs(h - 1, w - 1, gd, pd);

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (sd[i][j] == -1 or gd[i][j] == -1 or sd[i][j] + gd[i][j] >= k)
                continue;
            if (ok[i][j] and sd[i][j] <= l - 1 and (l - 1) % 2 == sd[i][j] % 2) {
                string t = "";
                int x = i, y = j;
                while (x != 0 or y != 0) {
                    auto [px, py] = ps[x][y];
                    if (x == px) {
                        t.push_back('R');
                    } else {
                        t.push_back('D');
                    }
                    x = px;
                    y = py;
                }
                reverse(t.begin(), t.end());

                if (s[i + 1][j] == '.' and s[i - 1][j] == '.') {
                    while ((int)t.size() < k - gd[i][j]) {
                        t.push_back('U');
                        t.push_back('D');
                    }
                } else {
                    while ((int)t.size() < k - gd[i][j]) {
                        t.push_back('R');
                        t.push_back('L');
                    }
                }

                x = i;
                y = j;
                while (x != h - 1 or y != w - 1) {
                    auto [px, py] = pd[x][y];
                    if (x == px) {
                        t.push_back('R');
                    } else {
                        t.push_back('D');
                    }
                    x = px;
                    y = py;
                }
                
                cout << "Yes" << endl;
                cout << t << endl;
                return 0;
                
            }
        }
    }
    cout << "No" << endl;
}
0