結果

問題 No.124 門松列(3)
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-20 23:14:33
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,477 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 71,268 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-28 16:39:22
合計ジャッジ時間 1,560 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1 ms
5,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 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;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

int h, w;
int board[110][110];
//d[i][j] := (x, y)へいく最短距離
int d[110][110];
int main(void){
	cin >> h >> w;
	rep(i, h)rep(j, w) cin >> board[i][j];
	rep(i, h)rep(j, w) d[i][j] = INF;

	queue<pair<P, P> > q;
	q.push(make_pair(make_pair(0, 1), make_pair(0, 0)));
	q.push(make_pair(make_pair(1, 0), make_pair(0, 0)));
	d[0][0] = 0; d[0][1] = 1; d[1][0] = 1;
	while(!q.empty()){
		auto p1 = q.front().first;//1つ前
		auto p2 = q.front().second;//2つ前
		q.pop();
		rep(i, 4){
			int ny = p1.first + dy[i], nx = p1.second + dx[i];
			if(ny == h - 1 && nx == w - 1) break;
			if(!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue;
			if(d[ny][nx] != INF) continue;

			int len0 = board[ny][nx];
			int len1 = board[p1.first][p1.second];
			int len2 = board[p2.first][p2.second];
			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_pair(make_pair(ny, nx), p1));
				d[ny][nx] = d[p1.first][p1.second] + 1;
			}
		}
	}
	int ans = d[h - 1][w - 1];
	if(ans == INF) printf("-1\n");
	else printf("%d\n", d[h - 1][w - 1]);
	return 0;
}
0