結果

問題 No.1572 XI
ユーザー mugen_1337mugen_1337
提出日時 2022-12-27 22:36:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 670 ms / 2,000 ms
コード長 4,313 bytes
コンパイル時間 2,610 ms
コンパイル使用メモリ 218,720 KB
実行使用メモリ 59,392 KB
最終ジャッジ日時 2024-05-02 00:50:29
合計ジャッジ時間 16,460 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 132 ms
15,104 KB
testcase_05 AC 310 ms
30,336 KB
testcase_06 AC 371 ms
35,968 KB
testcase_07 AC 10 ms
5,376 KB
testcase_08 AC 311 ms
31,360 KB
testcase_09 AC 145 ms
16,512 KB
testcase_10 AC 245 ms
25,216 KB
testcase_11 AC 20 ms
5,376 KB
testcase_12 AC 106 ms
12,800 KB
testcase_13 AC 25 ms
5,632 KB
testcase_14 AC 108 ms
13,312 KB
testcase_15 AC 25 ms
5,632 KB
testcase_16 AC 87 ms
11,264 KB
testcase_17 AC 9 ms
5,376 KB
testcase_18 AC 131 ms
15,104 KB
testcase_19 AC 331 ms
32,512 KB
testcase_20 AC 210 ms
21,888 KB
testcase_21 AC 391 ms
36,992 KB
testcase_22 AC 301 ms
29,568 KB
testcase_23 AC 7 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 593 ms
54,144 KB
testcase_35 AC 589 ms
53,376 KB
testcase_36 AC 597 ms
54,656 KB
testcase_37 AC 625 ms
56,576 KB
testcase_38 AC 607 ms
55,808 KB
testcase_39 AC 624 ms
56,320 KB
testcase_40 AC 582 ms
52,224 KB
testcase_41 AC 614 ms
54,784 KB
testcase_42 AC 611 ms
55,424 KB
testcase_43 AC 604 ms
54,400 KB
testcase_44 AC 670 ms
59,392 KB
testcase_45 AC 659 ms
59,136 KB
testcase_46 AC 644 ms
59,136 KB
testcase_47 AC 93 ms
59,136 KB
testcase_48 AC 647 ms
59,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define ALL(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mod 1000000007
using ll = long long;
const int INF = 1000000000;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

struct IOSetup {
    IOSetup() {
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout << fixed << setprecision(12);
    }
} iosetup;

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (T &x : v) is >> x;
    return is;
}

template<typename T>
struct Dice{
    //left, right, front, back, down, up
    T l,r,f,b,d,u;

    void RollF(){ // y-- (next, d <- f)
        int tmp=d; d=f; f=u; u=b; b=tmp;
    }

    void RollB(){ // y++ (next, d <- b)
        int tmp=d; d=b; b=u; u=f; f=tmp;
    }

    void RollL(){ // x-- (next, d <- l)
        int tmp=d; d=l; l=u; u=r; r=tmp;
    }

    void RollR(){ // x++ (next, d <- r)
        int tmp=d; d=r; r=u; u=l; l=tmp;
    }
};

template<typename T>
bool same_dice(Dice<T> a,Dice<T> b){
    return (a.b==b.b and a.f==b.f and a.l==b.l and a.r==b.r and a.u==b.u and a.d==b.d);
}

signed main() {
    int H, W; cin >> H >> W;
    int si, sj, gi, gj; cin >> si >> sj >> gi >> gj;
    si--, sj--, gi--, gj--;
    vector<string> G(H); cin >> G;

    vector<vector<vector<int>>> dp(H, vector<vector<int>>(W, vector<int>(6, INF)));

    // 0u, 1d, 2l, 3r, 4f, 5b
    using Tp = tuple<int,int,int>;
    queue<Tp> que;
    dp[si][sj][0] = 0;
    que.emplace(si, sj, 0);
    
    Dice<int> Ud = {0, 0, 0, 0, 0, 1}, 
              Dd = {0, 0, 0, 0, 1, 0},
              Bd = {0, 0, 0, 1, 0, 0},
              Fd = {0, 0, 1, 0, 0, 0},
              Rd = {0, 1, 0, 0, 0, 0},
              Ld = {1, 0, 0, 0, 0, 0};
    
    auto diceIdx = [&](Dice<int> x){
        if(same_dice(x, Ud)) return 0;
        if(same_dice(x, Dd)) return 1;
        if(same_dice(x, Bd)) return 2;
        if(same_dice(x, Fd)) return 3;
        if(same_dice(x, Rd)) return 4;
        if(same_dice(x, Ld)) return 5;
        return -1;
    };
    auto makeDice = [&](int state){
        if(state == 0) return Ud;
        if(state == 1) return Dd;
        if(state == 2) return Bd;
        if(state == 3) return Fd;
        if(state == 4) return Rd;
        if(state == 5) return Ld;
        return Ud;
    };
    
    while(not que.empty()){
        auto [ci, cj, dir] = que.front(); que.pop();

        {// R
            int ni = ci + 0, nj = cj + 1;
            Dice<int> d = makeDice(dir); d.RollR();
            int ndir = diceIdx(d);
            if(0<= ni and ni < H and 0 <= nj and nj < W and G[ni][nj] == '.')
                if(chmin(dp[ni][nj][ndir], dp[ci][cj][dir] + 1)) que.emplace(ni, nj, ndir);
        }
        {// L
            int ni = ci + 0, nj = cj - 1;
            Dice<int> d = makeDice(dir); d.RollL();
            int ndir = diceIdx(d);
            if(0<= ni and ni < H and 0 <= nj and nj < W and G[ni][nj] == '.')
                if(chmin(dp[ni][nj][ndir], dp[ci][cj][dir] + 1)) que.emplace(ni, nj, ndir);
        }
        {// F
            int ni = ci + 1, nj = cj;
            Dice<int> d = makeDice(dir); d.RollF();
            int ndir = diceIdx(d);
            if(0<= ni and ni < H and 0 <= nj and nj < W and G[ni][nj] == '.')
                if(chmin(dp[ni][nj][ndir], dp[ci][cj][dir] + 1)) que.emplace(ni, nj, ndir);
        }
        {// B
            int ni = ci - 1, nj = cj;
            Dice<int> d = makeDice(dir); d.RollB();
            int ndir = diceIdx(d);
            if(0<= ni and ni < H and 0 <= nj and nj < W and G[ni][nj] == '.')
                if(chmin(dp[ni][nj][ndir], dp[ci][cj][dir] + 1)) que.emplace(ni, nj, ndir);
        }
    }
    if(dp[gi][gj][0] < INF) cout << dp[gi][gj][0] << endl;
    else cout << -1 << endl;
    return 0;
}
0