結果

問題 No.124 門松列(3)
ユーザー fiordfiord
提出日時 2015-08-18 00:01:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,105 bytes
コンパイル時間 1,291 ms
コンパイル使用メモリ 152,560 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 12:41:56
合計ジャッジ時間 2,494 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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[0][0][1]=1;
	bfs[2][1][0]=1;
	int mx[]={1,0,0,-1};
	int my[]={0,-1,1,0};
	queue<tuple<int,int,int>> q;
	q.push(make_tuple(0,1,0));
	q.push(make_tuple(1,0,2));
	while(!q.empty()){
		auto crt=q.front();	q.pop();
		int y=get<0>(crt);
		int x=get<1>(crt);
		int from=get<2>(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(m[y-my[from]][x-mx[from]],m[y][x],m[y+my[i]][x+mx[i]])){
				if(bfs[i][y+my[i]][x+mx[i]]!=(1<<30))	continue;
				bfs[i][y+my[i]][x+mx[i]]=bfs[from][y][x]+1;
				q.push(make_tuple(y+my[i],x+mx[i],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