結果

問題 No.124 門松列(3)
ユーザー tkzw_21tkzw_21
提出日時 2015-01-12 00:03:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,155 bytes
コンパイル時間 1,401 ms
コンパイル使用メモリ 144,824 KB
実行使用メモリ 8,644 KB
最終ジャッジ日時 2023-09-04 04:34:00
合計ジャッジ時間 2,906 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,256 KB
testcase_01 WA -
testcase_02 AC 5 ms
8,328 KB
testcase_03 AC 7 ms
8,644 KB
testcase_04 AC 6 ms
8,320 KB
testcase_05 AC 6 ms
8,204 KB
testcase_06 AC 4 ms
8,216 KB
testcase_07 AC 4 ms
8,128 KB
testcase_08 AC 4 ms
8,328 KB
testcase_09 AC 5 ms
8,288 KB
testcase_10 AC 4 ms
8,204 KB
testcase_11 AC 4 ms
8,288 KB
testcase_12 AC 4 ms
8,332 KB
testcase_13 AC 4 ms
8,212 KB
testcase_14 AC 4 ms
8,284 KB
testcase_15 AC 4 ms
8,224 KB
testcase_16 AC 4 ms
8,172 KB
testcase_17 AC 5 ms
8,212 KB
testcase_18 AC 4 ms
8,128 KB
testcase_19 AC 4 ms
8,220 KB
testcase_20 AC 5 ms
8,152 KB
testcase_21 WA -
testcase_22 AC 4 ms
8,128 KB
testcase_23 AC 5 ms
8,300 KB
testcase_24 AC 5 ms
8,232 KB
testcase_25 AC 6 ms
8,232 KB
testcase_26 AC 5 ms
8,236 KB
testcase_27 AC 5 ms
8,136 KB
testcase_28 AC 6 ms
8,180 KB
testcase_29 AC 6 ms
8,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define INF INT_MAX / 2
#define MOD 1000000007

using namespace std;

typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIPII;
typedef long long ll;

bool isKadomatsuSequence(int a2,int a1,int a0){
	if((a1 < a0 && a1 < a2 || a1 > a2 && a1 > a0) && a2 != a0)return true;
	return false;
}

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

int m[101][101];
bool used[101][101];
int memo[101][101][11][11];

int w,h;
int dfs(int y,int x,int prev2,int prev1){
	if(y<0||y>=h||x<0||x>=w|| prev2 != -1 && !isKadomatsuSequence(prev2,prev1,m[y][x]) || used[y][x])return INF;
	if(y == h-1 && x == w-1)return 0;
	if(memo[y][x][prev2+1][prev1+1] != -1)return memo[y][x][prev2+1][prev1+1];
	//cout << y << " " << x << endl;
	int ret = INF;
	for(int i=0;i<4;i++){
		used[y][x] = true;
		ret = min(ret,dfs(y+dy[i],x+dx[i],prev1,m[y][x]));
		used[y][x] = false;
	}
	return memo[y][x][prev2+1][prev1+1] = ret+1;
}

int main(void) {
	cin >> w >> h;
	memset(memo,-1,sizeof(memo));
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin >> m[i][j];
		}
	}
	int ans = dfs(0,0,-1,-1);
	if(ans>=INF)cout << -1 << endl;
	else cout << ans << endl;
}
0