結果

問題 No.124 門松列(3)
ユーザー hotpepsihotpepsi
提出日時 2015-01-12 00:49:41
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,720 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 77,668 KB
実行使用メモリ 814,388 KB
最終ジャッジ日時 2023-09-04 04:52:54
合計ジャッジ時間 5,850 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
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 <algorithm>
#include <sstream>
#include <vector>
#include <cstring>

using namespace std;

typedef pair<int, int> II;
typedef pair<pair<int, int>, vector<int> > History;
typedef vector<History> Queue;

struct Solver {
	int H;
	int W;
	int b[100][100];

public:
	Solver(int h, int w, const int (&bb)[100][100]): H(h), W(w) {
		memcpy(b, bb, sizeof(b));
	}

	int solve(void) {
		vector<int> a;
		a.push_back(b[0][0]);
		Queue q;
		q.push_back(make_pair(II(0, 0), a));
		int ans = 0;
		while (q.size() > 0 && ans < W*H) {
			Queue next;
			for (int i = 0; i < (int)q.size(); ++i) {
				const History &h = q[i];
				if (h.first.first == W - 1 && h.first.second == H - 1) {
					return ans;
				}
				int len = (int)h.second.size();
				const int dx[4] = { -1, 0, 1, 0 };
				const int dy[4] = { 0, -1, 0, 1 };
				for (int d = 0; d < 4; ++d) {
					int xx = h.first.first + dx[d], yy = h.first.second + dy[d];
					if (xx >= 0 && xx < W && yy >= 0 && yy < H) {
						int n = b[yy][xx];
						if (len <= 1 ||
								(h.second[len - 2] < h.second[len - 1] && h.second[len-1] > n) ||
								(h.second[len - 2] > h.second[len - 1] && h.second[len - 1] < n)) {
							vector<int> b = h.second;
							b.push_back(n);
							next.push_back(make_pair(II(xx, yy), b));
						}
					}
				}
			}
			q = next;
			++ans;
		}
		return -1;
	}
};

int main(int argc, char *argv[])
{
	int W, H;
	string s;
	{
		getline(cin, s);
		stringstream ss(s);
		ss >> W >> H;
	}
	int b[100][100] = {};
	for (int i = 0; i < H; ++i) {
		getline(cin, s);
		stringstream ss(s);
		for (int j = 0; j < W; ++j) {
			ss >> b[i][j];
		}
	}
	Solver solver(H, W, b);
	int ans = solver.solve();
	cout << ans << endl;
	return 0;
}
0