結果

問題 No.124 門松列(3)
ユーザー IL_mstaIL_msta
提出日時 2015-07-22 19:59:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,746 bytes
コンパイル時間 764 ms
コンパイル使用メモリ 98,268 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 21:19:49
合計ジャッジ時間 1,961 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

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