結果

問題 No.124 門松列(3)
ユーザー nona_cat_puyonona_cat_puyo
提出日時 2016-06-05 13:32:34
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,386 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 69,704 KB
実行使用メモリ 33,556 KB
最終ジャッジ日時 2023-07-30 02:35:10
合計ジャッジ時間 3,808 ms
ジャッジサーバーID
(参考情報)
judge5 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 40 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 WA -
testcase_17 AC 9 ms
5,076 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 17 ms
6,868 KB
testcase_20 WA -
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 48 ms
13,708 KB
testcase_23 AC 33 ms
4,380 KB
testcase_24 AC 61 ms
18,512 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 53 ms
14,268 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 < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> M[i][j];
		}
	}

	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; 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();

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

		
		for (int i = 0; i < 4; i++) {
			int nx = p.first.first + dx[i];
			int ny = p.first.second + dy[i];


			if (0 <= nx && nx < H && 0 <= ny && ny < W) {
				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[H - 1][W - 1] != -1) break;
		k++;

	}
	cout << d[H - 1][W - 1];


    return 0;
}

0