結果

問題 No.124 門松列(3)
ユーザー codershifthcodershifth
提出日時 2015-10-17 17:14:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 3,581 bytes
コンパイル時間 2,581 ms
コンパイル使用メモリ 176,468 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 16:12:07
合計ジャッジ時間 4,256 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

namespace multi_dimentional_vector {
template<typename T> void resize(std::vector<T> &vec, size_t sz) { vec.resize(sz); }
template<typename T, typename S>
void resize(std::vector<T> &vec, size_t sz, const S &init) { vec.resize(sz, static_cast<T>(init)); }
template<typename T, typename... S>
void resize(std::vector<T> &vec, size_t sz, const S&... follows) {
        vec.resize(sz);
        for (auto &v : vec) resize(v, follows...);
}
template <typename T>
struct has_iterator {
    template <typename U> static char test(typename U::iterator* x);
    template <typename U> static long test(U* x);
    static const bool value = sizeof(test<T>(0)) == 1;
};
template <typename T, typename S>
typename std::enable_if<!has_iterator<T>::value, void>::type
fill(std::vector<T>& vec, const S &value) { std::fill(vec.begin(), vec.end(), static_cast<T>(value)); }

template <typename T, typename S>
typename std::enable_if<has_iterator<T>::value, void>::type
fill(std::vector<T>& vec, const S &value) { for (auto &v : vec) fill(v, value); }

template<typename T, typename... S>
std::vector<T> define(size_t sz, const S&... follows) {
        std::vector<T> vec;
        return std::move(vec);
}
};
namespace mdv = multi_dimentional_vector;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class PineDecorationSequence3 {
public:
    void solve(void) {
            int W,H;
            cin>>W>>H;

            vector<vector<int>> M;
            mdv::resize(M,H,W);

            REP(y,H)
            REP(x,W)
                cin>>M[y][x];

            //   1
            // 0 * 2
            //   3
            vector<vector<vector<bool>>> vis;
            mdv::resize(vis,H,W,4,false);

            // 幅優先探索
            queue<tuple<int,int,int,int>> que;
            // (0,0) から次の移動場所を push する
            que.emplace(0,1,1,1);
            que.emplace(1,0,0,1);

            while ( !que.empty() )
            {
                int x,y,p,t;
                tie(x,y,p,t) = que.front();
                que.pop();

                if ( x == W-1 && y == H-1 )
                {
                    cout<<t<<endl;
                    return;
                }

                int dx[] = {-1,0,1,0};
                int dy[] = {0,-1,0,1};
                int px = x+dx[p];
                int py = y+dy[p];

                REP(i,4)
                {
                    int nx = x+dx[i];
                    int ny = y+dy[i];
                    int np = (i+2)%4;

                    if ( nx < 0 || W <= nx || ny < 0 || H <= ny )
                        continue;

                    // 門松列になっているか確認
                    vector<int> a3 = {M[py][px], M[y][x], M[ny][nx]};
                    sort(RANGE(a3));

                    if ( M[ny][nx] != a3[1] && M[py][px] != a3[1] )
                        continue;

                    if ( a3[0] == a3[1] || a3[1] == a3[2] || a3[2] == a3[0])
                        continue;

                    if ( vis[ny][nx][np] )
                        continue;

                    que.emplace(nx,ny,np,t+1);
                    vis[ny][nx][np] = true;
                }
            }
            cout<<-1<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new PineDecorationSequence3();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0