結果

問題 No.34 砂漠の行商人
ユーザー finefine
提出日時 2017-03-08 19:30:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 5,000 ms
コード長 1,084 bytes
コンパイル時間 2,160 ms
コンパイル使用メモリ 178,904 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 19:27:09
合計ジャッジ時間 3,549 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;
typedef tuple<int, int, int, int> T;

const int INF = 1e7;
const int dh[] = {1, -1, 0, 0};
const int dw[] = {0, 0, 1, -1};

int L[100][100];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, V, sx, sy, gx, gy;
	cin >> N >> V >> sx >> sy >> gx >> gy;
	sx--;
	sy--;
	gx--;
	gy--;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> L[i][j];
		}
	}
	vector< vector<int> > memo(N, vector<int>(N, INF));
	queue<T> q;
	q.push(T(0, 0, sy, sx));
	int ans = INF;
	while (!q.empty()) {
		T t = q.front();
		q.pop();
		int d, v, h, w;
		tie(d, v, h, w) = t;
		if (memo[h][w] <= v) continue;
		memo[h][w] = v;
		if (h == gy && w == gx) {
			ans = d;
			break;
		}
		for (int i = 0; i < 4; i++) {
			int nh = h + dh[i], nw = w + dw[i];
			if (nh < 0 || nh >= N || nw < 0 || nw >= N) continue;
			int nd = d + 1, nv = v + L[nh][nw];
			if (nv >= V) continue;
			if (memo[nh][nw] <= nv) continue;
			q.push(T(nd, nv, nh, nw));
		}
	}
	cout << (ans == INF ? -1 : ans) << endl;
	return 0;
}
0