結果

問題 No.124 門松列(3)
ユーザー nona_cat_puyonona_cat_puyo
提出日時 2016-06-04 16:54:02
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,381 bytes
コンパイル時間 538 ms
コンパイル使用メモリ 69,532 KB
実行使用メモリ 24,576 KB
最終ジャッジ日時 2023-07-29 21:55:18
合計ジャッジ時間 2,255 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

typedef pair<P, P> PP;

int W, H;

int M[100][100];

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

int d[100][100] = { 0 };

bool isKadomatsu(int a, int b, int c) {
	if (a == 0 || b == 0) return true;

	if ((a == b) || (b == c) || (a == c)) {
		return false;
	}
	else {
		return !(((a < b) && (b < c)) || ((a > b) && (b > c)));
	}
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> W >> H;

	int k = 0;

	for (int i = 0; i < W; i++) {
		for (int j = 0; j < H; j++) {
			cin >> M[i][j];
		}
	}
	for (int i = 0; i < W; i++) {
		for (int j = 0; j < H; j++) {
			d[i][j] = -1;
		}
	}
	d[0][0] = 0;

	queue<PP> que;
	que.push(PP(P(0, 0), P(0, 0)));

	while (que.size() && k <= 1000000) {
		PP p = que.front(); que.pop();
		
		for (int i = 0; i < 4; i++) {
			int nx = p.first.first + dx[i];
			int ny = p.first.second + dy[i];

			int b = M[p.first.first][p.first.second];
			int bb = p.second.second;

			if (0 <= nx && nx < W && 0 <= ny && ny < H) {
				if (isKadomatsu(bb, b, M[nx][ny])) {
					que.push(PP(P(nx, ny), P(bb, b)));
					d[nx][ny] = d[p.first.first][p.first.second] + 1;
				}
			}
		}
		if (d[W - 1][H - 1] != -1) break;
		k++;
	}
	cout << d[W - 1][H - 1];

    return 0;
}

0