結果

問題 No.124 門松列(3)
ユーザー tubo28tubo28
提出日時 2015-01-12 00:06:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 2,545 bytes
コンパイル時間 1,543 ms
コンパイル使用メモリ 100,136 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 04:35:31
合計ジャッジ時間 2,165 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <deque>
#include <map>
#include <queue>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define each(e,c) for(auto&e:c)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<(DUMP(),__VA_ARGS__).str()<<" ["<<__LINE__<<"]"<<endl)
struct DUMP:ostringstream{template<class T>DUMP &operator,(const T&t){if(this->tellp())*this<<", ";*this<<t;return *this;}};
#else
#define dump(...)
#endif
template<class T> ostream& operator<<(ostream& os, vector<T> const& v){
    rep(i,v.size()) os << v[i] << (i+1==v.size()?"":" ");
    return os;
}

int W,H;
int M[111][111];

// d,v,pre1,pre2
typedef tuple<int,int,int,int> State;
typedef tuple<int,int,int> ns;
map<ns,int> dp;

int solve(){
    dp.clear();
    dp[mt(0,-1,-1)] = 0;
    priority_queue<State> q;
    q.emplace(0,0,-1,-1);
    while(q.size()){
        int d,v,p1,p2;
        tie(d,v,p1,p2) = q.top(); q.pop();
        //dump(d,v,p1,p2);
        d = -d;
        if(v==W*H-1) return d;
        int dx[]={0,1,-1,0}, dy[]={1,0,0,-1};
        if(dp.count(tie(v,p1,p2)) && dp[tie(v,p1,p2)] < d) continue;
        rep(i,4){
            int nx = v%W+dx[i], ny = v/W+dy[i];
            int x = v%W, y = v/W;
            int p1x = p1%W, p1y = p1/W;
            if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
            if(p1==-1){
                if(M[y][x]==M[ny][nx]){
                    continue;
                }
            } else {
                if(M[p1y][p1x] == M[ny][nx]) continue;
                if(!((M[p1y][p1x] < M[y][x] && M[y][x] > M[ny][nx]) ||
                     (M[p1y][p1x] > M[y][x] && M[y][x] < M[ny][nx]))){
                    continue;
                }
            }
            int nv = ny*W+nx;
            if(dp.count(tie(nv,v,p1))==0 || dp[tie(nv,v,p1)] > d+1){
                dp[tie(nv,v,p1)] = d+1;
                q.emplace(-(d+1),nv,v,p1);
            }
        }
    }
    return -1;
}

int main(){
    while(cin>>W>>H && W){
        rep(i,H)rep(j,W) cin >> M[i][j];
        cout << solve() << endl;
    }
}
0