結果

問題 No.124 門松列(3)
ユーザー 古寺いろは古寺いろは
提出日時 2015-04-08 17:08:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,683 bytes
コンパイル時間 928 ms
コンパイル使用メモリ 76,892 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-17 18:44:59
合計ジャッジ時間 2,318 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//#include "bits/stdc++.h"
#include <iostream>
#include <vector>
#include <queue>
#include<algorithm>

using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define ALL(c) (c).begin(), (c).end()

int dp[100][100][10];
int INF = 99999999;

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

bool check(int a, int b, int c){
	vector<int> v;
	v.push_back(a);
	v.push_back(b);
	v.push_back(c);
	sort(v.begin(), v.end());
	if (v[0] == v[1]) return false;
	if (v[1] == v[2]) return false;
	if (v[1] == b) return false;
	return true;
}

int main() {
	int W, H;
	cin >> W >> H;
	vector<vector<int>> board(H, vector<int>(W));
	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			cin >> board[i][j];
		}
	}

	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			for (int k = 0; k < 10; k++)
			{
				dp[i][j][k] = INF;
			}
		}
	}

	queue<pair<pair<int, int>, int>> q;
	for (int k = 0; k < 10; k++)
	{
		dp[0][0][k] = 0;
		q.push(make_pair(make_pair(0, 0), k));
	}

	while (!q.empty()){
		auto pos = q.front().first;
		auto pre = q.front().second;
		q.pop();
		int now = board[pos.first][pos.second];
		int cost = dp[pos.first][pos.second][pre];
		for (int k = 0; k < 4; k++)
		{
			int ny = pos.first + dy[k];
			int nx = pos.second + dx[k];
			if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
			int next = board[ny][nx];
			if (cost != 0 && !check(pre, now, next)) continue;
			if (dp[ny][nx][now] != INF) continue;
			dp[ny][nx][now] = cost + 1;
			q.push(make_pair(make_pair(ny, nx), now));
		}
	}
	

	int ans = INF;
	for (int k = 0; k < 10; k++)
	{
		ans = min(ans, dp[H - 1][W - 1][k]);
	}
	if (ans == INF) ans = -1;

	cout << ans << endl;
}
0