結果

問題 No.124 門松列(3)
ユーザー hotpepsihotpepsi
提出日時 2015-01-12 01:29:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,626 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 70,560 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 04:57:43
合計ジャッジ時間 1,946 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <sstream>
#include <vector>
#include <cstring>

using namespace std;

struct History {
	int a;
	int b;
	int x;
	int y;
	int dir;
};

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

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

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

	int solve(void) {
		Queue q;
		q.push_back({ -1, b[0][0], 0, 0, 0 });
		int ans = 0;
		while (q.size() > 0) {
			Queue next;
			for (int i = 0; i < (int)q.size(); ++i) {
				const History &h = q[i];
				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.x + dx[d], y = h.y + dy[d];
					if (x >= 0 && x < W && y >= 0 && y < H && !(x == 0 && y == 0) && !vis[h.dir][d][y][x]) {
						vis[h.dir][d][y][x] = 1;
						if (h.a < 0 || h.a != b[y][x] && (h.a < h.b && h.b > b[y][x] || h.a > h.b && h.b < b[y][x])) {
							if (x == W - 1 && y == H - 1) {
								return ans + 1;
							}
							next.push_back({ h.b, b[y][x], x, y, d });
						}
					}
				}
			}
			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