結果

問題 No.2411 Reverse Directions
ユーザー yutake2000yutake2000
提出日時 2023-08-11 23:27:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,755 bytes
コンパイル時間 2,600 ms
コンパイル使用メモリ 222,212 KB
実行使用メモリ 15,512 KB
最終ジャッジ日時 2024-11-18 18:55:59
合計ジャッジ時間 33,075 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,640 KB
testcase_01 AC 2 ms
12,704 KB
testcase_02 AC 2 ms
13,636 KB
testcase_03 AC 2 ms
11,296 KB
testcase_04 AC 7 ms
13,632 KB
testcase_05 AC 2 ms
11,424 KB
testcase_06 AC 2 ms
13,636 KB
testcase_07 AC 10 ms
12,800 KB
testcase_08 AC 2 ms
13,636 KB
testcase_09 AC 2 ms
13,220 KB
testcase_10 AC 24 ms
15,512 KB
testcase_11 AC 1 ms
12,580 KB
testcase_12 TLE -
testcase_13 AC 3 ms
12,064 KB
testcase_14 TLE -
testcase_15 AC 9 ms
13,636 KB
testcase_16 AC 700 ms
13,636 KB
testcase_17 TLE -
testcase_18 AC 83 ms
6,816 KB
testcase_19 AC 2 ms
6,820 KB
testcase_20 TLE -
testcase_21 AC 4 ms
6,816 KB
testcase_22 TLE -
testcase_23 AC 3 ms
6,816 KB
testcase_24 TLE -
testcase_25 AC 3 ms
6,816 KB
testcase_26 AC 317 ms
6,820 KB
testcase_27 AC 3 ms
6,820 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 AC 5 ms
12,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pdd;
template<typename T>
using MaxHeap = priority_queue<T>;
template<typename T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define INF (ll)1e17
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define cauto const auto&
#define cdebug if (debug_mode) cerr
#define dump(x) if (debug_mode) cerr << #x << " = " << (x) << "\t";
#define SP << " " <<
#define TB << "\t" <<

#ifdef _LOCAL
    bool debug_mode = true;
#else
    bool debug_mode = false;
#endif

int main()
{

    ll H, W, K, L, R;
    cin >> H >> W >> K >> L >> R;

    vector<string> board(H);
    REP(i, H) cin >> board[i];

    if ((R - L) % 2 == 0 || (H + W - K) % 2 != 0 || (K - (R-L+1)) < (H+W-2)) {
        cout << "No" << endl;
        return 0;
    }

    vector<vector<ll>> dist(H, vector<ll>(W, INF));
    set<Pll> ps;
    deque<Pll> Q;
    Q.push_back({0, 0});
    dist[0][0] = 0;
    ll dy[] = {0, 1, 0, -1};
    ll dx[] = {1, 0, -1, 0};
    while (!Q.empty()) {
        Pll p = Q.front(); Q.pop_front();
        ll y = p.first, x = p.second;
        REP(k, 4) {
            ll ny = y + dy[k], nx = x + dx[k];
            if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
            if (board[ny][nx] == '#') continue;
            if (dist[ny][nx] <= dist[y][x] + 1) continue;
            dist[ny][nx] = dist[y][x] + 1;
            Q.push_front({ny, nx});

            if ((dist[y][x] - L) % 2 == 0 || dist[y][x] > L) continue;
            ll ny2 = y - dy[k], nx2 = x - dx[k];
            if (ny2 < 0 || ny2 >= H || nx2 < 0 || nx2 >= W) continue;
            if (board[ny2][nx2] == '#') continue;
            ps.insert({y, x});
        }
    }

    vector<vector<ll>> dist2(H, vector<ll>(W, INF));

    deque<Pll> Q2;
    Q2.push_back({H-1, W-1});
    dist2[H-1][W-1] = 0;
    while (!Q2.empty()) {
        Pll p = Q2.front(); Q2.pop_front();
        ll y = p.first, x = p.second;
        REP(k, 4) {
            ll ny = y + dy[k], nx = x + dx[k];
            if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
            if (board[ny][nx] == '#') continue;
            if (dist2[ny][nx] <= dist2[y][x] + 1) continue;
            dist2[ny][nx] = dist2[y][x] + 1;
            Q2.push_front({ny, nx});
        }
    }

    for (auto p : ps) {
        ll y = p.first, x = p.second;
        cdebug << y SP x << endl;
        if (dist2[y][x] <= K - R) {
            
            string ans = "";
            ll d = dist2[y][x];
            // 経路復元
            while (d > 0) {
                REP(k, 4) {
                    ll ny = y + dy[k], nx = x + dx[k];
                    if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
                    if (board[ny][nx] == '#') continue;
                    if (dist2[ny][nx] != d - 1) continue;
                    ans += "RDLU"[k];
                    y = ny, x = nx;
                    d--;
                    break;
                }
            }
            reverse(ALL(ans));

            y = p.first;
            x = p.second;

            REP(k, 2) {
                ll ny = y + dy[k], nx = x + dx[k];
                ll ny2 = y - dy[k], nx2 = x - dx[k];
                cdebug << ny SP nx SP ny2 SP nx2 << endl;
                if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
                if (ny2 < 0 || ny2 >= H || nx2 < 0 || nx2 >= W) continue;
                if (board[ny][nx] == '#') continue;
                if (board[ny2][nx2] == '#') continue;

                REP(i, (K-dist[y][x]-dist2[y][x])/2) {
                    ans += "RDLU"[k];
                    ans += "RDLU"[k+2];
                }
                break;
            }

            d = dist[y][x];
            while (d > 0) {
                REP(k, 4) {
                    ll ny = y + dy[k], nx = x + dx[k];
                    if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
                    if (board[ny][nx] == '#') continue;
                    if (dist[ny][nx] != d - 1) continue;
                    ans += "LURD"[k];
                    y = ny, x = nx;
                    d--;
                    break;
                }
            }

            reverse(ALL(ans));

            cout << "Yes" << endl;
            cout << ans << endl;
            return 0;

        }
    }

    cdebug << "unreachable" << endl;
    cout << "No" << endl;

    return 0;
}
0