結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  koyopro | 
| 提出日時 | 2015-10-03 18:43:15 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,107 bytes | 
| コンパイル時間 | 1,251 ms | 
| コンパイル使用メモリ | 163,084 KB | 
| 実行使用メモリ | 8,704 KB | 
| 最終ジャッジ日時 | 2024-07-19 19:25:25 | 
| 合計ジャッジ時間 | 13,685 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | -- * 4 | 
| other | TLE * 1 -- * 25 | 
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(int i=0; i<(n); i++)
int W,H;
int M[111][111];
bool ch[111][111];
bool isKadomatsu(int a, int b, int c) {
    if (a == b) return false;
    if (a == c) return false;
    if (b == c) return false;
    int l[] = {a, b, c};
    sort(l, l+3);
    // 2番目は真ん中ではいけない
    if (l[1] == b) return false;
    return true;
}
int dfs(int x, int y, vector<int> s) {
    if (x == W-1 && y == H-1) return 0;
    int dxy[] = {0, 1, 0, -1, 0};
    int minc = 100000;
    s.push_back(M[x][y]);
    ch[x][y] = true;
    REP(i,4) {
        int nx = x + dxy[i];
        int ny = y + dxy[i+1];
        if (nx<0||W<=nx||ny<0||H<=ny) continue;
        if (ch[nx][ny]) continue;
        if (s.size()>=2 && !isKadomatsu(s[s.size()-2], s[s.size()-1], M[nx][ny])) continue;
        minc = min(minc, 1 + dfs(nx, ny, s));
    }
    ch[x][y] = false;
    return minc;
}
signed main()
{
    cin>>W>>H;
    REP(y,H)REP(x,W) cin >> M[x][y];
    vector<int> s;
    int ans = dfs(0, 0, s);
    cout << (ans<1000 ? ans : -1) << endl;
    return 0;
}
            
            
            
        