結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  codershifth | 
| 提出日時 | 2015-10-17 17:14:33 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 5 ms / 5,000 ms | 
| コード長 | 3,581 bytes | 
| コンパイル時間 | 2,346 ms | 
| コンパイル使用メモリ | 190,516 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-07-22 10:18:11 | 
| 合計ジャッジ時間 | 3,492 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
#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
            
            
            
        