結果

問題 No.124 門松列(3)
ユーザー koyoprokoyopro
提出日時 2015-10-03 18:43:15
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,107 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 149,236 KB
実行使用メモリ 12,220 KB
最終ジャッジ日時 2023-09-27 01:49:56
合計ジャッジ時間 13,872 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}
0