結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  IL_msta | 
| 提出日時 | 2015-07-22 19:59:52 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3 ms / 5,000 ms | 
| コード長 | 1,746 bytes | 
| コンパイル時間 | 836 ms | 
| コンパイル使用メモリ | 100,124 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-08 11:54:23 | 
| 合計ジャッジ時間 | 1,656 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
#define _USE_MATH_DEFINES
 
#include <iostream>
#include <iomanip>
 
#include <algorithm>
#include <cmath>
 
#include <string>
//#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
 
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
 
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////
int f[100][100];
bool kado(int a,int b,int c,int d){
	if(d<2){
		return true;
	}
	if( (c < a && a < b) || (b<a&&a<c)||(a<c&&c<b)||(b<c&&c<a) ){
		return true;
	}
	return false;
}
int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	int W,H;
	cin>>W>>H;
	rep(i,H)rep(j,W){
		cin>>f[i][j];//[y][x]
	}
	queue< vector<int> > q;
	set < vector<int> > s;
	vector<int> now(5),next(5),now4(4);
	now[0] = 0;
	now[1] = 0;
	now[2] = -1;
	now[3] = -1;
	now[4] = 0;
	rep(i,4){now4[i] = now[i];}
	int dx[4] = {1,0,-1,0};
	int dy[4] = {0,1,0,-1};
	q.push( now );
	s.insert( now4 );
	while( !q.empty()){
		now = q.front();q.pop();
		next[2] = f[now[1]][now[0]];
		next[3] = now[2];
		next[4] = now[4] + 1;
		rep(i,4){
			next[0] = now[0] + dx[i];
			next[1] = now[1] + dy[i];
			if( 0 <= next[0] && next[0] < W &&
				0 <= next[1] && next[1] < H){
				if( true == kado(f[next[1]][next[0]],next[2],next[3],next[4]) ){
					if( next[0] == W-1 && next[1] == H-1){
						P(next[4]);
						return 0;
					}
					if(next[4] <= W*H){
						rep(i,4){now4[i] = next[i];}
						if( true == s.insert( now4 ).second ){
							q.push(next);
						}
					}
				}
			}
		}
	}
	P(-1);
	return 0;
}
            
            
            
        