結果

問題 No.124 門松列(3)
ユーザー fiordfiord
提出日時 2015-08-17 23:47:15
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,088 bytes
コンパイル時間 1,408 ms
コンパイル使用メモリ 153,316 KB
実行使用メモリ 814,632 KB
最終ジャッジ日時 2023-09-25 12:41:30
合計ジャッジ時間 9,787 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)	for(int i=0;i<n;i++)
int bfs[4][100][100];
bool ok(int a,int b,int c){
	if(a==b||b==c||c==a)	return false;
	if(a<b&&c<b)	return true;
	if(a>b&&c>b)	return true;
	return false;
}
int main(){
	int w,h;	cin>>w>>h;
	int m[h][w];
	rep(i,h)	rep(j,w)	cin>>m[i][j];
	rep(i,4)	rep(j,h)	rep(k,w)	bfs[i][j][k]=1<<30;
	bfs[2][0][1]=1;
	bfs[0][1][0]=1;
	int mx[]={1,0,0,-1};
	int my[]={0,-1,1,0};
	queue<tuple<int,int,int,int>> q;
	q.push(make_tuple(0,1,m[0][0],2));
	q.push(make_tuple(1,0,m[0][0],0));
	while(!q.empty()){
		auto crt=q.front();	q.pop();
		int y=get<0>(crt);
		int x=get<1>(crt);
		int before=get<2>(crt);
		int from=get<3>(crt);
		rep(i,4){
			if((from^3)==i)	continue;
			if(0>y+my[i]||y+my[i]>=h||0>x+mx[i]||x+mx[i]>=w)	continue;
			if(ok(before,m[y][x],m[y+my[i]][x+mx[i]])){
				bfs[i][y+my[i]][x+mx[i]]=bfs[from][y][x]+1;
				q.push(make_tuple(y+my[i],x+mx[i],m[y][x],i));
			}
		}
		rep(i,4){
			if(bfs[i][h-1][w-1]!=1<<30){
				cout<<bfs[i][h-1][w-1]<<endl;
				return 0;
			}
		}
	}
	cout<<-1<<endl;
	return 0;
}
0