結果

問題 No.34 砂漠の行商人
ユーザー furon
提出日時 2023-06-16 21:13:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,312 ms / 5,000 ms
コード長 1,754 bytes
コンパイル時間 1,294 ms
コンパイル使用メモリ 132,252 KB
最終ジャッジ日時 2025-02-14 03:54:56
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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