結果

問題 No.124 門松列(3)
ユーザー Yang33Yang33
提出日時 2018-04-27 01:50:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,431 bytes
コンパイル時間 2,143 ms
コンパイル使用メモリ 178,868 KB
実行使用メモリ 7,672 KB
最終ジャッジ日時 2023-09-10 05:58:54
合計ジャッジ時間 3,265 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,504 KB
testcase_01 AC 4 ms
7,432 KB
testcase_02 AC 4 ms
7,432 KB
testcase_03 AC 4 ms
7,480 KB
testcase_04 AC 4 ms
7,496 KB
testcase_05 AC 4 ms
7,464 KB
testcase_06 AC 4 ms
7,420 KB
testcase_07 AC 3 ms
7,532 KB
testcase_08 AC 3 ms
7,404 KB
testcase_09 AC 3 ms
7,520 KB
testcase_10 AC 4 ms
7,436 KB
testcase_11 AC 4 ms
7,588 KB
testcase_12 AC 4 ms
7,444 KB
testcase_13 AC 4 ms
7,476 KB
testcase_14 AC 4 ms
7,524 KB
testcase_15 AC 4 ms
7,424 KB
testcase_16 AC 4 ms
7,396 KB
testcase_17 AC 4 ms
7,592 KB
testcase_18 AC 3 ms
7,592 KB
testcase_19 AC 3 ms
7,436 KB
testcase_20 AC 4 ms
7,440 KB
testcase_21 AC 4 ms
7,452 KB
testcase_22 AC 4 ms
7,672 KB
testcase_23 AC 4 ms
7,416 KB
testcase_24 AC 4 ms
7,432 KB
testcase_25 AC 4 ms
7,416 KB
testcase_26 AC 3 ms
7,416 KB
testcase_27 AC 3 ms
7,408 KB
testcase_28 AC 4 ms
7,536 KB
testcase_29 AC 4 ms
7,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/04/26  Problem: yukicoder 124  / Link: http://yukicoder.me/problems/no/124  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

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

	int W, H; cin >> W >> H;
	VVI  a(H, VI(W));
	FOR(i, 0, H) {
		FOR(j, 0, W) {
			cin >> a[i][j];
		}
	}

	int dist[102][102][10][10];
	fill(***dist, ***dist + 102 * 102 * 10 * 10, INF);
	using tp = tuple<int, int, int, int, int>;
	queue<tp>pq;
	dist[0][0][0][a[0][0]] = 0;
	pq.push(tp(0, 0, 0, 0, a[0][0])); // dist, y, x, prepre, pre
	auto f = [&](int c, int b, int a) {// 
		bool ok = 0;
		if (a<b&&b>c&&a != c)ok = 1;
		if (a > b&&b < c&&a != c)ok = 1;
		if (a == 0 || b == 0)ok = 1;
		return ok;

	};
	while (!pq.empty()) {
		int d, y, x, a_2, a_1;
		tie(d, y, x, a_2, a_1) = pq.front(); pq.pop();
		if (dist[y][x][a_2][a_1] < d)continue;
		FOR(k, 0, 4) {
			int ny = y + DY[k], nx = x + DX[k];
			if (0 <= ny&&ny < H && 0 <= nx&&nx < W) {
				if (f(a[ny][nx], a_1, a_2)) {
					//cout << a_2 << "," << a_1 << "," << a[ny][nx]<< endl;
					if (dist[ny][nx][a_1][a[ny][nx]] > dist[y][x][a_2][a_1] + 1) {
						dist[ny][nx][a_1][a[ny][nx]] = dist[y][x][a_2][a_1] + 1;
						pq.push(tp(d, ny, nx, a_1, a[ny][nx]));
					}
				}
			}
		}
	}

	int ans = INF;
	FOR(i, 1, 10) {
		FOR(j, 1, 10) {
			ans = min(ans, dist[H - 1][W - 1][i][j]);
		}
	}
	cout << (ans < INF ? ans : -1) << "\n";

	return 0;
}
0