結果

問題 No.34 砂漠の行商人
ユーザー furonfuron
提出日時 2023-06-16 21:13:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,335 ms / 5,000 ms
コード長 1,754 bytes
コンパイル時間 1,386 ms
コンパイル使用メモリ 135,144 KB
実行使用メモリ 398,216 KB
最終ジャッジ日時 2023-09-06 18:17:47
合計ジャッジ時間 7,738 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 10 ms
4,948 KB
testcase_05 AC 10 ms
5,252 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 16 ms
6,228 KB
testcase_08 AC 21 ms
8,308 KB
testcase_09 AC 411 ms
231,856 KB
testcase_10 AC 319 ms
398,004 KB
testcase_11 AC 1,335 ms
398,216 KB
testcase_12 AC 7 ms
4,924 KB
testcase_13 AC 866 ms
102,456 KB
testcase_14 AC 683 ms
98,544 KB
testcase_15 AC 33 ms
43,248 KB
testcase_16 AC 92 ms
82,208 KB
testcase_17 AC 22 ms
30,556 KB
testcase_18 AC 8 ms
9,084 KB
testcase_19 AC 156 ms
24,048 KB
testcase_20 AC 305 ms
39,440 KB
testcase_21 AC 6 ms
5,388 KB
testcase_22 AC 31 ms
38,684 KB
testcase_23 AC 90 ms
117,244 KB
testcase_24 AC 618 ms
207,692 KB
testcase_25 AC 23 ms
9,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <random>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
const int inf = (1 << 30) - 1;
const ll INF = 1LL << 60;
const int MOD = 1000000007;

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

int main() {
	int n, V, sx, sy, gx, gy;
	cin >> n >> V >> sx >> sy >> gx >> gy;
	sx--; sy--; gx--; gy--;
	vvi g(n, vi(n));
	int h = n, w = n;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> g[y][x];
		}
	}
	vector<vvi> dist(n, vvi(n, vi(V + 1, inf)));
	dist[sy][sx][0] = 0;
	queue<pair<pii, int>> que;
	que.push({ {sy, sx} , 0 });
	while (!que.empty()) {
		pair<pii, int> p = que.front();
		que.pop();
		auto [y, x] = p.first;
		int v = p.second;
		int d = dist[y][x][v];
		if (y == gy && x == gx) {
			cout << d << endl;
			return 0;
		}

		for (int i = 0; i < 4; i++) {
			int y2 = y + dy[i];
			int x2 = x + dx[i];
			if (y2 < 0 || y2 > h - 1 || x2 < 0 || x2 > w - 1) continue;

			int c = g[y2][x2];
			if (v + c >= V) continue;
			if (dist[y2][x2][v + c] <= d + 1) continue;
			dist[y2][x2][v + c] = d + 1;
			que.push({ {y2, x2}, v + c });

		}

	}
	cout << -1 << endl;

	return 0;
}
0