結果

問題 No.124 門松列(3)
ユーザー なおなお
提出日時 2015-01-12 01:46:00
言語 C++11
(gcc 11.4.0)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,871 bytes
コンパイル時間 2,365 ms
コンパイル使用メモリ 149,084 KB
実行使用メモリ 538,180 KB
最終ジャッジ日時 2023-09-04 05:00:11
合計ジャッジ時間 13,800 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 AC 2 ms
7,480 KB
testcase_02 AC 2 ms
7,632 KB
testcase_03 MLE -
testcase_04 MLE -
testcase_05 MLE -
testcase_06 AC 3 ms
7,476 KB
testcase_07 AC 2 ms
5,432 KB
testcase_08 AC 3 ms
9,572 KB
testcase_09 AC 3 ms
7,600 KB
testcase_10 AC 3 ms
7,504 KB
testcase_11 AC 2 ms
5,716 KB
testcase_12 AC 2 ms
5,592 KB
testcase_13 AC 3 ms
7,424 KB
testcase_14 AC 6 ms
26,020 KB
testcase_15 AC 6 ms
26,024 KB
testcase_16 AC 30 ms
126,712 KB
testcase_17 AC 11 ms
46,484 KB
testcase_18 AC 6 ms
28,188 KB
testcase_19 AC 9 ms
40,368 KB
testcase_20 AC 13 ms
58,860 KB
testcase_21 AC 7 ms
28,060 KB
testcase_22 AC 9 ms
40,392 KB
testcase_23 AC 188 ms
324,448 KB
testcase_24 AC 351 ms
457,964 KB
testcase_25 AC 332 ms
371,512 KB
testcase_26 AC 51 ms
153,752 KB
testcase_27 AC 31 ms
116,600 KB
testcase_28 MLE -
testcase_29 MLE -
権限があれば一括ダウンロードができます

ソースコード

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[111][111][111][111];
const int dir[][2] = {{1,0},{0,1},{-1,0},{0,-1}};

struct V {
    int x1,y1,x2,y2;
    V(int x1,int y1,int x2,int y2) :
        x1(x1),y1(y1),x2(x2),y2(y2){;}
};

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(y2,H) REP(x2,W)
        step[y2][x2][y][x] = INF;

    deque<V> q;
    q.push_back(V(-1,-1,0,0));

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

            if(step[v.y2][v.x2][my][mx] <= c) continue;
            if(v.x1 >= 0  && v.y1 >= 0 &&
                !isKadomatsuSequence(m[v.y1][v.x1], m[v.y2][v.x2], m[my][mx])) continue;
            step[v.y2][v.x2][my][mx] = c;
            q.push_back(V(v.x2,v.y2,mx,my));
        }
    }
    cout << -1 << endl;
}
0