結果

問題 No.124 門松列(3)
ユーザー koyopro
提出日時 2015-10-03 18:42:27
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
実行時間 -
コード長 1,103 bytes
コンパイル時間 1,365 ms
コンパイル使用メモリ 164,664 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-19 19:25:11
合計ジャッジ時間 3,647 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 13 WA * 6 RE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

#define REP(i, n) for(int i=0; i<(n); i++)

int W,H;
int M[11][11];
bool ch[11][11];
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;
}
0