結果

問題 No.34 砂漠の行商人
ユーザー masamasa
提出日時 2015-01-30 16:29:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 1,037 bytes
コンパイル時間 712 ms
コンパイル使用メモリ 72,296 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 18:55:07
合計ジャッジ時間 1,807 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>

using namespace std;

struct state {
	int x, y, t, v;
};

int main() {
	int n, v, sx, sy, gx, gy;

	cin >> n >> v >> sx >> sy >> gx >> gy;
	vector< vector<int> > land(n+2, vector<int>(n+2, 1e8));
	vector< vector<int> > maxv(n+2, vector<int>(n+2, 0));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> land[i][j];
		}
	}

	const int dx[4] = {1, 0, -1, 0};
	const int dy[4] = {0, 1, 0, -1};
	int nx, ny, nv;

	int ans = -1;
	queue<state> que;
	state st = {sx, sy, 0, v};
	que.push(st);
	maxv[sy][sx] = v;
	while(!que.empty()) {
		st = que.front();
		que.pop();
		if (st.x == gx && st.y == gy) {
			ans = st.t;
			break;
		}

		for (int i = 0; i < 4;  i++) {
			nx = st.x + dx[i];
			ny = st.y + dy[i];
			nv = st.v - land[ny][nx];
			if (land[ny][nx] < st.v && maxv[ny][nx] < nv) {
				maxv[ny][nx] = nv;
				que.push((state) {nx, ny, st.t + 1, nv} );
			}
		}
	}

	cout << ans << endl;
	return 0;
}
0