結果

問題 No.124 門松列(3)
ユーザー goodbatongoodbaton
提出日時 2015-09-15 22:01:09
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,544 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 72,108 KB
実行使用メモリ 10,964 KB
最終ジャッジ日時 2023-09-26 12:06:47
合計ジャッジ時間 2,207 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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

int 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==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;
}

int dfs(int y,int x,int a,int b){
    int ret = INF;
    
    if(y<0 || h<=y || x<0 || w<=x) return INF;
    
    if(w-1==x && h-1==y && isKadomatsuSequence(a,b,m[y][x])) return 0;
    
    if(visit[y][x][a+1][b+1]) return INF;
    
    if(a>=0 && !isKadomatsuSequence(a,b,m[y][x])) return INF;
    
    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]);
        }
    }
    
    int ans = dfs(0,0,-1,-1);
    
    if(ans==INF){
        puts("-1");
    }else{
        printf("%d\n",ans);
    }
    
    return 0;
}
0