結果

問題 No.1315 渦巻洞穴
ユーザー tokusakurai
提出日時 2020-12-12 12:23:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,666 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 199,144 KB
最終ジャッジ日時 2025-01-16 22:54:59
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 79
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

pii pos(int x){
    if(x == 1) {return pii(0, 0);}

    int r = ceil(sqrt(x)-0.000001);
    if(r%2 == 0) ++r;
    int k = x-(r-2)*(r-2)-1;
    int a = (r-1)/2, b = k%(r-1);

    if(k/(r-1) == 0){
        return pii(a, -a+1+b);
    }
    if(k/(r-1) == 1){
        return pii(a-1-b, a);
    }
    if(k/(r-1) == 2){
        return pii(-a, a-1-b);
    }
    if(k/(r-1) == 3){
        return pii(-a+1+b, -a);
    }
    return pii(0, 0);
}

char inv(char x){
    if(x == 'R') return 'L';
    if(x == 'L') return 'R';
    if(x == 'U') return 'D';
    if(x == 'D') return 'U';
    return '.';
}

string move(int sx, int sy, int tx, int ty){
    assert((abs(sx-tx)+abs(sy-ty))%2 == 0);
    string tmp, ret;
    if(sx < tx){
        rep(i, tx-sx) tmp += 'R';
    }
    else{
        rep(i, sx-tx) tmp += 'L';
    }
    if(sy < ty){
        rep(i, ty-sy) tmp += 'U';
    }
    else{
        rep(i, sy-ty) tmp += 'D';
    }
    rep(i, sz(tmp)/2){
        ret += tmp[2*i], ret += tmp[2*i+1];
        ret += inv(tmp[2*i+1]);
        ret += tmp[2*i+1];
    }
    return ret;
}

int main(){
    int S, T;
    cin >> S >> T;
    int sx, sy, tx, ty;
    tie(sx, sy) = pos(S), tie(tx, ty) = pos(T);
    //cout << sx << ' ' << sy << ' ' << tx << ' ' << ty << '\n';

    string ans;
    ans += "RLR";
    sx++;
    int a = abs(sx)+abs(sy), b = abs(tx)+abs(ty);
    a &= 1, b &= 1;

    if(a == b){
        ans += move(sx, sy, tx, ty);
    }
    else if(a == 0){
        ans += move(sx, sy, 0, 0);
        ans += "RLRUD";
        ans += move(1, 0, tx, ty);
    }
    else{
        ans += move(sx, sy, -1, 0);
        ans += "URD";
        ans += move(0, 0, tx, ty);
    }

    cout << 0 << '\n' << sz(ans) << '\n' << ans << '\n';
}
0