結果

問題 No.124 門松列(3)
ユーザー なおなお
提出日時 2015-01-12 01:43:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,818 bytes
コンパイル時間 1,424 ms
コンパイル使用メモリ 148,772 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 04:58:10
合計ジャッジ時間 2,518 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,384 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 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 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 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 #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))
#define RREP(i, n)          for(int(i)=(n)-1;(i)>=0;--(i))
const int MOD = (int)1e9+7;

const int INF = (1<<29);
int m[111][111];
int step[10][111][111];
bool f[10][111][111];
const int dir[][2] = {{1,0},{0,1},{-1,0},{0,-1}};

struct V {
    int x,y,v;
    V(int x,int y,int v) :
        x(x),y(y),v(v){;}
};

bool isKadomatsuSequence(int a, int b, int c){
    if(a == b || a == c || b == c) return false;
    if(b == max(max(a,c),b)) return true;
    if(b == min(min(a,c),b)) return true;
    return false;
}

int main(){
    int W,H; cin >> W >> H;
    REP(y,H) REP(x,W){
        cin >> m[y][x];
    }
    REP(y,H) REP(x,W) REP(v,10)
        step[v][y][x] = INF;

    deque<V> q;
    q.push_back(V(0,0,0));
    step[0][0][0] = 0;

    while(!q.empty()){
        auto v = q.front(); q.pop_front();
        if(v.y == H-1 && v.x == W-1){
            cout << step[v.v][v.y][v.x] << endl;
            return 0;
        }
        int c = step[v.v][v.y][v.x] + 1;
        f[v.v][v.y][v.x] = false;
        REP(d,4){
            int mx = v.x + dir[d][0];
            int my = v.y + dir[d][1];
            if(mx < 0 || my < 0 || mx >= W || my >= H) continue;

            int nv = m[v.y][v.x];
            if(step[nv][my][mx] < c) continue;
            if(v.v > 0 && !isKadomatsuSequence(v.v, nv, m[my][mx])) continue;
            step[nv][my][mx] = c;
            if(!f[nv][my][mx]){
                q.push_back(V(mx,my,nv));
                f[nv][my][mx] = true;
            }
        }
    }
    cout << -1 << endl;
}
0