結果

問題 No.124 門松列(3)
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-20 22:42:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,834 bytes
コンパイル時間 624 ms
コンパイル使用メモリ 75,484 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 16:39:20
合計ジャッジ時間 1,544 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <tuple>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<(n);i++)
const int INF = 1e9;
//上 右 下 左 0 1 2 3
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

int h, w;
int board[110][110];
int d[110][110][4];
int main(void){
	cin >> w >> h;
	rep(i, h)rep(j, w) cin >> board[i][j];
	rep(i, h)rep(j, w)rep(k, 4) d[i][j][k] = INF;

	queue<tuple<P, P, int>> q;
	q.push(make_tuple(make_pair(0, 1), make_pair(0, 0), 1));
	q.push(make_tuple(make_pair(1, 0), make_pair(0, 0), 2));
	d[0][0][0] = 0,  
	d[0][1][1] = 1; d[1][0][2] = 1;
	while(!q.empty()){
		P p1, p2;//1つまえ 2つまえ
		int v;//どの方向から来たか
		auto t = q.front(); q.pop();
		tie(p1, p2, v) = t;
		// tie(p1, p2, v) = q.front; q.pop();
		rep(i, 4){
			int ny = p1.first + dy[i], nx = p1.second + dx[i];
			// if(ny == h - 1 && nx == w - 1)break;
			// printf("%d %d %d\n", ny, nx, i);
			if(!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue;
			// printf("get0 %d %d\n", ny, nx);
			if(d[ny][nx][i] != INF) continue;

			// printf("get %d %d\n", ny, nx);

			int len0 = board[ny][nx];
			int len1 = board[p1.first][p1.second];
			int len2 = board[p2.first][p2.second];
			// printf("%d %d %d\n", len0, len1, len2);

			if(len0 == len1 || len1 == len2 || len2 == len0) continue;
			int mid = (len0 + len1 + len2) 
				- min(len0, min(len1, len2)) - max(len0, max(len1, len2));
			if(mid == len0 || mid == len2){
				q.push(make_tuple(make_pair(ny, nx), p1, i));
				// printf("push %d %d\n", ny, nx);
				d[ny][nx][i] = d[p1.first][p1.second][v] + 1;
			}
		}
	}
	int ans = INF;
	rep(i, 4){
		ans  = min(ans, d[h - 1][w - 1][i]);
	}
	if(ans == INF) printf("-1\n");
	else printf("%d\n", ans);
	return 0;
}
0