結果

問題 No.124 門松列(3)
ユーザー goodbatongoodbaton
提出日時 2015-09-16 21:56:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,779 bytes
コンパイル時間 1,129 ms
コンパイル使用メモリ 88,992 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-26 12:18:44
合計ジャッジ時間 2,515 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 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

bool visit[100][100][12];
int mo[] = {0,1,0,-1,0};
int h,w,m[100][100];
queue<pair<pair<int,int>,int> > q1;

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

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]);
        }
    }
    
    q1.push({{0,0},-1});
    
    for(int i=0;q1.size()>0;i++){
        queue<pair<pair<int,int>,int> > q2;

        while(q1.size()){
            pair<pair<int,int>,int> p = q1.front();
            q1.pop();
            int x = p.first.second;
            int y = p.first.first;
            
            if(x==w-1 && y==h-1){
                printf("%d\n",i);
                return 0;
            }
            
            if(visit[y][x][p.second]) continue;
            visit[y][x][p.second] = true;
            
            for(int i=0;i<4;i++){
                
                if(isKadomatsuSequence(p.second,m[y][x],m[y+mo[i]][x+mo[i+1]])){
                    if(0<=x+mo[i+1] && x+mo[i+1]<w && 0<=y+mo[i] && y+mo[i]<h)
                        q2.push({{y+mo[i],x+mo[i+1]},m[y][x]});
                }
            }
        }
        
        q1 = q2;
        
    }
    
    puts("-1");
    
    return 0;
}
0