結果

問題 No.124 門松列(3)
コンテスト
ユーザー goodbaton
提出日時 2015-09-15 22:02:47
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,569 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 644 ms
コンパイル使用メモリ 98,364 KB
実行使用メモリ 106,332 KB
最終ジャッジ日時 2026-04-02 14:52:13
合計ジャッジ時間 1,513 ms
ジャッジサーバーID
(参考情報)
judge5_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 13 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstring>

typedef long long ll;
using namespace std;

#define mod 1000000007
#define INF 1000000000
#define LLINF 2000000000000000000LL
#define PI 3.1415926536

#define SIZE 1000

ll dp[100][100][12][12];
bool visit[100][100][12][12];
int mo[] = {0,1,0,-1,0};
int h,w,m[100][100];

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

ll dfs(int y,int x,int a,int b){
    ll ret = LLINF;
    
    if(y<0 || h<=y || x<0 || w<=x) return LLINF;
    
    if(w-1==x && h-1==y && isKadomatsuSequence(a,b,m[y][x])) return 0;
    
    if(visit[y][x][a+1][b+1]) return LLINF;
    
    if(!isKadomatsuSequence(a,b,m[y][x])) return LLINF;
    
    if(dp[y][x][a+1][b+1]>0) return dp[y][x][a+1][b+1];
    
    visit[y][x][a+1][b+1] = true;
    
    for(int i=0;i<4;i++){
        ret = min(ret,dfs(y+mo[i],x+mo[i+1],b,m[y][x])+1);
    }
    
    visit[y][x][a+1][b+1] = false;
    
    return dp[y][x][a+1][b+1] = ret;
}

int main(){
    scanf("%d%d",&w,&h);
    
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            scanf("%d",&m[i][j]);
        }
    }
    
    ll ans = dfs(0,0,-1,-1);
    
    if(ans==INF){
        puts("-1");
    }else{
        printf("%lld\n",ans);
    }
    
    return 0;
}
0