結果

問題 No.124 門松列(3)
ユーザー kurenaifkurenaif
提出日時 2016-09-20 22:28:43
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,805 bytes
コンパイル時間 1,083 ms
コンパイル使用メモリ 108,476 KB
実行使用メモリ 422,004 KB
最終ジャッジ日時 2024-04-28 16:39:14
合計ジャッジ時間 7,537 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <stack>
#include <limits>
#include <cassert>
#include <fstream>
#include <cstring>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <ciso646>
#include <set>
#include <array>

using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

#define inf 0x3f3f3f3f
#define INF INT_MAX/3
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define pii pair<int,int>
#define pcc pair<char,char>
#define pic pair<int,char>
#define pci pair<char,int>
#define VS vector<string>
#define VI vector<int>
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define pi 2*acos(0.0)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE()freopen("out0.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define FST first
#define SEC second
#define SETUP cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15)

namespace {
	struct input_returnner {
		int N; input_returnner(int N_ = 0) :N(N_) {}
		template<typename T> operator vector<T>() const { vector<T> res(N); for (auto &a : res) cin >> a; return std::move(res); }
		template<typename T> operator T() const { T res; cin >> res; return res; }
		template<typename T> T operator - (T right) { return T(input_returnner()) - right; }
		template<typename T> T operator + (T right) { return T(input_returnner()) + right; }
		template<typename T> T operator * (T right) { return T(input_returnner()) * right; }
		template<typename T> T operator / (T right) { return T(input_returnner()) / right; }
		template<typename T> T operator << (T right) { return T(input_returnner()) << right; }
		template<typename T> T operator >> (T right) { return T(input_returnner()) >> right; }
	};
	template<typename T> input_returnner in() { return in<T>(); }
	input_returnner in() { return input_returnner(); }
	input_returnner in(int N) { return std::move(input_returnner(N)); }
}

void solve();
/// ---template---
signed main(void) {
	SETUP;
	solve();
	return 0;
}

struct Point {
	int x;
	int y;
	Point(int x_, int y_):x(x_),y(y_){}
	Point() {}
};

bool operator < (list<Point> left, list<Point> right) {
	return left.size() < right.size();
}

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

bool isKadomatsu(const list<Point>& points, const vector<vector<int> >& M, const Point& np) {
	if (points.size() < 2) return true;
	array<int, 3> cost;
	cost[0] = M[np.y][np.x];
	auto it = points.rbegin();
	cost[1] = M[it->y][it->x];
	++it;
	cost[2] = M[it->y][it->x];
	REP(i, 3) if (cost[i] == cost[(i + 1) % 3]) return false;
	if(*max_element(ALL(cost)) == cost[1] or *min_element(ALL(cost)) == cost[1]) return true;
	return false;
}

void solve() {
	int W, H; cin >> W >> H;
	vector< vector<int> > M(H, vector<int>(W));
	REP(h, H) REP(w, W) cin >> M[h][w];

	priority_queue<pair<int, list<Point>>> que;
	que.push({ 0,{Point(0,0)} });

	while (not que.empty()) {
		auto a = que.top(); que.pop();
		if (a.second.size() > 3) a.second.pop_front();
		Point p = *a.second.rbegin();
		if (p.x == W - 1 and p.y == H - 1) {
			cout << a.first << endl;
			return;
		}
		REP(i, 4) {
			int nx = p.x + dx[i], ny = p.y + dy[i];
			if (0 <= nx and nx < W and 0 <= ny and ny < H) {
				if (isKadomatsu(a.second, M, Point(nx, ny))) {
					a.second.push_back(Point(nx, ny));
					que.push({ a.first + 1, a.second });
					a.second.pop_back();
				}
			}
		}
	}
	cout << -1 << endl;
}
0