結果

問題 No.124 門松列(3)
ユーザー pekempeypekempey
提出日時 2015-08-20 15:28:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,799 bytes
コンパイル時間 2,382 ms
コンパイル使用メモリ 157,380 KB
実行使用メモリ 9,424 KB
最終ジャッジ日時 2023-09-25 13:52:13
合計ジャッジ時間 3,587 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
9,316 KB
testcase_01 AC 6 ms
9,152 KB
testcase_02 AC 6 ms
9,152 KB
testcase_03 AC 8 ms
9,224 KB
testcase_04 AC 7 ms
9,300 KB
testcase_05 AC 7 ms
9,176 KB
testcase_06 AC 5 ms
9,184 KB
testcase_07 AC 6 ms
9,204 KB
testcase_08 AC 6 ms
9,156 KB
testcase_09 AC 6 ms
9,148 KB
testcase_10 AC 6 ms
9,184 KB
testcase_11 AC 6 ms
9,284 KB
testcase_12 AC 6 ms
9,152 KB
testcase_13 AC 6 ms
9,156 KB
testcase_14 AC 6 ms
9,232 KB
testcase_15 AC 6 ms
9,164 KB
testcase_16 AC 6 ms
9,176 KB
testcase_17 AC 6 ms
9,380 KB
testcase_18 AC 6 ms
9,144 KB
testcase_19 AC 6 ms
9,232 KB
testcase_20 AC 6 ms
9,236 KB
testcase_21 AC 6 ms
9,156 KB
testcase_22 AC 6 ms
9,236 KB
testcase_23 AC 6 ms
9,176 KB
testcase_24 AC 6 ms
9,424 KB
testcase_25 AC 6 ms
9,424 KB
testcase_26 AC 6 ms
9,168 KB
testcase_27 AC 6 ms
9,236 KB
testcase_28 AC 7 ms
9,268 KB
testcase_29 AC 7 ms
9,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

int W, H;
int M[111][111];

int dp[111][111][11][11]; // y, x, curr, prev
typedef tuple<int, int, int, int, int> P; // dist, y, x, curr, prev

int main() {
	cin >> W >> H;
	rep (i, H) rep (j, W) cin >> M[i][j];

	rep (i, 111) rep (j, 111) rep (k, 11) rep (l, 11) dp[i][j][k][l] = inf;

	priority_queue<P, vector<P>, greater<P>> q;
	q.emplace(0, 0, 0, M[0][0], 0);
	dp[0][0][M[0][0]][0] = 0;

	while (!q.empty()) {
		P p = q.top(); q.pop();
		int y = get<1>(p);
		int x = get<2>(p);
		int curr = get<3>(p);
		int prev = get<4>(p);

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

		rep (k, 4) {
			int ny = y + dy[k];
			int nx = x + dx[k];

			if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;

			int next = M[ny][nx];

			if (prev == 0) {
				if (curr == next) continue;
				if (dp[ny][nx][next][curr] > dp[y][x][curr][prev] + 1) {
					dp[ny][nx][next][curr] = dp[y][x][curr][prev] + 1;
					q.emplace(dp[ny][nx][next][curr], ny, nx, next, curr);
				}
			} else {
				if (prev == curr || prev == next || curr == next) continue;

				if (prev > curr && curr < next || prev < curr && curr > next) {
					if (dp[ny][nx][next][curr] > dp[y][x][curr][prev] + 1) {
						dp[ny][nx][next][curr] = dp[y][x][curr][prev] + 1;
						q.emplace(dp[ny][nx][next][curr], ny, nx, next, curr);
					}
				}
			}
		}
	}

	int ans = inf;
	rep (k, 10) rep (l, 10) {
		ans = min(ans, dp[H - 1][W - 1][k][l]);
	}
	if (ans == inf) ans = -1;
	cout << ans << endl;

	return 0;
}
0