結果
| 問題 | No.34 砂漠の行商人 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-10-21 17:08:28 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 4,844 ms / 5,000 ms |
| + 618µs | |
| コード長 | 1,222 bytes |
| 記録 | |
| コンパイル時間 | 6,931 ms |
| コンパイル使用メモリ | 212,728 KB |
| 実行使用メモリ | 398,208 KB |
| 最終ジャッジ日時 | 2026-10-21 17:09:01 |
| 合計ジャッジ時間 | 25,188 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge5_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
int main() {
using namespace std;
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 (auto &l : L) {
for (auto &e : l) { cin >> e; }
}
constexpr int INF = 1e9;
vector dist(n, vector(n, vector<int>(v + 1, INF)));
dist[sy][sx][v] = 0;
queue<tuple<int, int, int>> que;
que.emplace(sy, sx, v);
constexpr int dx[] = {-1, 0, 1, 0};
constexpr int dy[] = {0, -1, 0, 1};
while (!que.empty()) {
auto [x, y, l] = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) { continue; }
int nl = l - L[nx][ny];
if (nl <= 0) { continue; }
if (dist[nx][ny][nl] > dist[x][y][l] + 1) {
dist[nx][ny][nl] = dist[x][y][l] + 1;
que.emplace(nx, ny, nl);
}
}
}
int ans = INF;
for (int l = 0; l <= v; l++) { ans = min(ans, dist[gy][gx][l]); }
cout << (ans < INF ? ans : -1) << endl;
}