結果

問題 No.124 門松列(3)
ユーザー fiordfiord
提出日時 2015-08-17 23:32:51
言語 C++11
(gcc 13.3.0)
結果
MLE  
実行時間 -
コード長 1,362 bytes
コンパイル時間 1,362 ms
コンパイル使用メモリ 166,648 KB
実行使用メモリ 814,660 KB
最終ジャッジ日時 2024-07-18 10:04:42
合計ジャッジ時間 9,670 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other MLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)	for(int i=0;i<n;i++)
int bfs[2][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,h)	rep(j,w)	bfs[0][i][j]=bfs[1][i][j]=1<<30;
	bfs[0][0][0]=bfs[1][0][0]=0;
	if(m[0][0]<m[0][1])	bfs[0][0][1]=1;
	else if(m[0][0]>m[0][1])	bfs[1][0][1]=1;
	if(m[0][0]<m[1][0])	bfs[0][1][0]=1;
	else if(m[0][0]>m[1][0])	bfs[1][1][0]=1;
	int mx[]={1,-1,0,0};
	int my[]={0,0,1,-1};
	queue<tuple<int,int,int,int>> q;
	q.push(make_tuple(0,1,m[0][0],(bfs[0][0][1]==1?0:1)));
	q.push(make_tuple(1,0,m[0][0],(bfs[0][1][0]==1?0:1)));
	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 next=get<3>(crt);
		if(bfs[next][y][x]>w*h)	break;
		for(int i=0;i<4;i++){
			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[next^1][y+my[i]][x+mx[i]]=bfs[next][y][x]+1;
				q.push(make_tuple(y+my[i],x+mx[i],m[y][x],next^1));
			}
		}
		if(bfs[next][h-1][w-1]!=1<<30)	break;
	}
	if(min(bfs[0][h-1][w-1],bfs[1][h-1][w-1])==(1<<30))	cout<<-1<<endl;
	else 	cout<<min(bfs[0][h-1][w-1],bfs[1][h-1][w-1])<<endl;
	return 0;
}
0