結果

問題 No.124 門松列(3)
ユーザー furafura
提出日時 2020-06-08 00:36:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 980 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 207,388 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-25 14:31:30
合計ジャッジ時間 3,679 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

bool is_kadomatsu(int a,int b,int c){
	if(a==b || a==c || b==c) return false;
	if(a<b && b>c) return true;
	if(a>b && b<c) return true;
	return false;
}
int main(){
	int h,w; scanf("%d%d",&w,&h);
	int a[100][100];
	rep(i,h) rep(j,w) scanf("%d",&a[i][j]);

	int d[100][100][4];
	rep(i,h) rep(j,w) rep(k,4) d[i][j][k]=-1;
	d[0][1][0]=1;
	d[1][0][3]=1;
	queue<tuple<int,int,int>> Q;
	Q.emplace(0,1,0);
	Q.emplace(1,0,3);
	while(!Q.empty()){
		int y,x,k; tie(y,x,k)=Q.front(); Q.pop();

		if(y==h-1 && x==w-1) return printf("%d\n",d[y][x][k]),0;

		int a0=a[y-dy[k]][x-dx[k]],a1=a[y][x];
		rep(kk,4){
			int yy=y+dy[kk],xx=x+dx[kk];
			if(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx][kk]==-1){
				int a2=a[yy][xx];
				if(is_kadomatsu(a0,a1,a2)){
					d[yy][xx][kk]=d[y][x][k]+1;
					Q.emplace(yy,xx,kk);
				}
			}
		}
	}
	puts("-1");

	return 0;
}
0