結果

問題 No.34 砂漠の行商人
コンテスト
ユーザー Unbakedbread
提出日時 2025-12-11 01:27:41
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 50 ms / 5,000 ms
コード長 907 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,263 ms
コンパイル使用メモリ 288,456 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-11 01:27:47
合計ジャッジ時間 5,069 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

constexpr int DX[4] = {0, 1, 0, -1}, DY[4] = {1, 0, -1, 0};

int main(void) {
	int N, V, sx, sy, gx, gy;
	cin >> N >> V >> sx >> sy >> gx >> gy;
	--sx, --sy, --gx, --gy;
	
	vector L(N, vector<int>(N));
	for(int i = 0; i < N; ++i) for(int j = 0; j < N; ++j) cin >> L[i][j];
	
	vector d(N, vector<int>(N, 0));
	queue<tuple<int, int, int, int>> que;
	d[sx][sy] = V;
	que.push(make_tuple(0, sx, sy, V));
	
	while(!que.empty()) {
		auto [dist, x, y, c] = que.front();
		que.pop();
		
		if(x == gx and y == gy) {
			cout << dist << "\n";
			return 0;
		}
		
		for(int dir = 0; dir < 4; ++dir) {
			int nx = x + DX[dir], ny = y + DY[dir];
			if(!(0 <= nx and nx < N and 0 <= ny and ny < N)) continue;
			int nc = c - L[ny][nx];
			if(d[nx][ny] < nc) {
				d[nx][ny] = nc;
				que.push(make_tuple(dist + 1, nx, ny, nc));
			}
		}
	}
	
	cout << -1 << "\n";
	return 0;
}
0