結果

問題 No.124 門松列(3)
ユーザー hotpepsihotpepsi
提出日時 2015-01-12 01:04:08
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,638 bytes
コンパイル時間 769 ms
コンパイル使用メモリ 78,320 KB
実行使用メモリ 814,688 KB
最終ジャッジ日時 2023-09-04 04:56:08
合計ジャッジ時間 7,277 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 vector< vector<II> > 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) {
		Queue q;
		q.push_back(vector<II>(1, II(0, 0)));
		while (q.size() > 0) {
			Queue next;
			for (int i = 0; i < (int)q.size(); ++i) {
				const vector<II> &h = q[i];
				if (h.back() == II(W - 1, H - 1)) {
					return h.size() - 1;
				}
				int len = (int)h.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 x = h.back().first + dx[d], y = h.back().second + dy[d];
					if (x >= 0 && x < W && y >= 0 && y < H && find(h.begin(), h.end(), II(x, y)) == h.end()) {
						int p = -1;
						if (len >= 2) {
							p = b[h[len - 2].second][h[len - 2].first];
						}
						int q = b[h[len - 1].second][h[len - 1].first];
						int r = b[y][x];
						if (p < 0 || (p < q && q > r) || (p > q && q < r)) {
							vector<II> b = h;
							b.push_back(II(x, y));
							next.push_back(b);
						}
					}
				}
			}
			q = next;
		}
		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