結果

問題 No.124 門松列(3)
ユーザー kurenai3110kurenai3110
提出日時 2016-09-20 23:20:33
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,390 bytes
コンパイル時間 726 ms
コンパイル使用メモリ 78,316 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 16:39:26
合計ジャッジ時間 1,827 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;

int W, H;
int DP[101][101][4];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
vector<vector<int> >M;

void bfs() {
	for (int i = 0; i < 101; i++)for (int j = 0; j < 101; j++)for (int k = 0; k < 4; k++)DP[i][j][k] = INT32_MAX;
	DP[0][1][0] = 1;
	DP[1][0][3] = 1;
	queue<pair<int,int> > que;
	que.push(make_pair(W,0));
	que.push(make_pair(1,3));
	while (!que.empty()) {
		pair<int,int> v = que.front(); que.pop();
		int y = v.first / W;
		int x = v.first % W;
		int p_y = y + dy[v.second];
		int p_x = x + dx[v.second];

		for (int i = 0; i < 4; i++) {
			int n_y = y + dy[i];
			int n_x = x + dx[i];
			if (n_x<0 || n_y<0 || n_x>=W || n_y>=H)continue;
			if (DP[n_x][n_y][(i + 2) % 4] <= DP[x][y][v.second] + 1)continue;
			if((M[p_y][p_x]<=M[y][x]&&M[y][x]<=M[n_y][n_x])|| (M[p_y][p_x] >= M[y][x] && M[y][x] >= M[n_y][n_x]))continue;

			DP[n_x][n_y][(i + 2) % 4] = DP[x][y][v.second] + 1;
			que.push(make_pair(W*n_y + n_x, (i + 2) % 4));
		}
	}
}

int main()
{
	cin >> W >> H;
	M.resize(W);
	for (int i = 0; i < W; i++) {
		M[i].resize(H);
		for (int j = 0; j < H; j++) {
			cin >> M[i][j];
		}
	}

	bfs();

	int ans = min(DP[W - 1][H - 1][0], DP[W - 1][H - 1][3]);
	if (ans == INT32_MAX)cout << -1 << endl;
	else cout << ans << endl;

    return 0;
}
0