結果

問題 No.124 門松列(3)
ユーザー sugim48sugim48
提出日時 2015-01-11 23:37:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 144 ms / 5,000 ms
コード長 2,378 bytes
コンパイル時間 796 ms
コンパイル使用メモリ 88,384 KB
実行使用メモリ 72,444 KB
最終ジャッジ日時 2023-09-03 23:40:16
合計ジャッジ時間 3,415 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
42,628 KB
testcase_01 AC 14 ms
34,824 KB
testcase_02 AC 13 ms
34,816 KB
testcase_03 AC 107 ms
60,604 KB
testcase_04 AC 144 ms
72,444 KB
testcase_05 AC 144 ms
72,324 KB
testcase_06 AC 14 ms
34,728 KB
testcase_07 AC 14 ms
35,028 KB
testcase_08 AC 14 ms
34,744 KB
testcase_09 AC 14 ms
34,824 KB
testcase_10 AC 14 ms
34,704 KB
testcase_11 AC 14 ms
34,720 KB
testcase_12 AC 14 ms
34,720 KB
testcase_13 AC 14 ms
34,944 KB
testcase_14 AC 16 ms
35,032 KB
testcase_15 AC 16 ms
35,060 KB
testcase_16 AC 24 ms
37,528 KB
testcase_17 AC 17 ms
35,576 KB
testcase_18 AC 15 ms
35,144 KB
testcase_19 AC 17 ms
35,476 KB
testcase_20 AC 19 ms
36,140 KB
testcase_21 AC 15 ms
35,080 KB
testcase_22 AC 18 ms
35,884 KB
testcase_23 AC 61 ms
48,196 KB
testcase_24 AC 83 ms
54,788 KB
testcase_25 AC 83 ms
54,660 KB
testcase_26 AC 37 ms
41,540 KB
testcase_27 AC 29 ms
38,964 KB
testcase_28 AC 144 ms
72,372 KB
testcase_29 AC 144 ms
72,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v, w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 2;

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

vector<edge> G[1000000];
ll d[1000000];

void dijkstra_deque(int n, vector<edge> G[], int s, ll d[]) {
	vector<bool> vis(n, false);
	fill(d, d + n, LLONG_MAX); d[s] = 0;
	deque<ll> deq; deq.push_back(s);
	while (!deq.empty()) {
		int u = deq.front(); deq.pop_front();
		if (vis[u]) continue;
		vis[u] = true;
		for (int i = 0; i < G[u].size(); i++) {
			edge e = G[u][i];
			if (d[e.v] > d[u] + e.w) {
				d[e.v] = d[u] + e.w;
				if (e.w == 0) deq.push_front(e.v);
				else deq.push_back(e.v);
			}
		}
	}
}

void add_edge(vector<edge> G[], int u, int v) {
	edge e = {u, v, 1};
	G[u].push_back(e);
}

bool dr_kadomatsu(int x, int y, int z) {
	if (!x) return true;
	if (x == y || y == z || z == x) return false;
	return (x - y) * (z - y) > 0;
}

int W, H;

int encode(int y, int x, int i, int j) {
	return ((y * W + x) * 10 + i) * 10 + j;
}

int main() {
	cin >> W >> H;
	vector< vector<int> > M(H, vector<int>(W));
	for (int y = 0; y < H; y++)
		for (int x = 0; x < W; x++)
			cin >> M[y][x];
	for (int y = 0; y < H; y++)
		for (int x = 0; x < W; x++)
			for (int i = 0; i < 10; i++)
				for (int j = 0; j < 10; j++) {
					for (int k = 0; k < 4; k++) {
						int _x = x + dx[k], _y = y + dy[k];
						if (_x < 0 || _x >= W || _y < 0 || _y >= H) continue;
						int h1 = j, h2 = M[y][x], h3 = M[_y][_x];
						if (!dr_kadomatsu(h1, h2, h3)) continue;
						add_edge(G, encode(y, x, i, j), encode(_y, _x, h1, h2));
					}
				}
	dijkstra_deque(1000000, G, encode(0, 0, 0, 0), d);
	ll mini = LLONG_MAX;
	for (int i = 0; i < 10; i++)
		for (int j = 0; j < 10; j++)
			mini = min(mini, d[encode(H - 1, W - 1, i, j)]);
	cout << (mini < LLONG_MAX ? mini : -1) << endl;
}
0