結果

問題 No.34 砂漠の行商人
ユーザー furonfuron
提出日時 2023-06-16 21:05:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,801 bytes
コンパイル時間 2,990 ms
コンパイル使用メモリ 135,536 KB
実行使用メモリ 402,556 KB
最終ジャッジ日時 2023-09-06 18:12:57
合計ジャッジ時間 13,870 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 10 ms
4,912 KB
testcase_05 AC 10 ms
5,220 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 15 ms
6,204 KB
testcase_08 AC 21 ms
8,452 KB
testcase_09 AC 4,545 ms
231,936 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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];

		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 });

		}

	}
	int ans = inf;
	for (int i = 0; i <= V; i++) {
		ans = min(ans, dist[gy][gx][i]);
	}
	if (ans == inf) ans = -1;
	cout << ans << endl;

	return 0;
}
0