結果
問題 | No.34 砂漠の行商人 |
ユーザー |
|
提出日時 | 2017-03-08 19:30:28 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 72 ms / 5,000 ms |
コード長 | 1,084 bytes |
コンパイル時間 | 1,719 ms |
コンパイル使用メモリ | 181,816 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-28 10:34:55 |
合計ジャッジ時間 | 2,796 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef tuple<int, int, int, int> T; const int INF = 1e7; const int dh[] = {1, -1, 0, 0}; const int dw[] = {0, 0, 1, -1}; int L[100][100]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, V, sx, sy, gx, gy; cin >> N >> V >> sx >> sy >> gx >> gy; sx--; sy--; gx--; gy--; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> L[i][j]; } } vector< vector<int> > memo(N, vector<int>(N, INF)); queue<T> q; q.push(T(0, 0, sy, sx)); int ans = INF; while (!q.empty()) { T t = q.front(); q.pop(); int d, v, h, w; tie(d, v, h, w) = t; if (memo[h][w] <= v) continue; memo[h][w] = v; if (h == gy && w == gx) { ans = d; break; } for (int i = 0; i < 4; i++) { int nh = h + dh[i], nw = w + dw[i]; if (nh < 0 || nh >= N || nw < 0 || nw >= N) continue; int nd = d + 1, nv = v + L[nh][nw]; if (nv >= V) continue; if (memo[nh][nw] <= nv) continue; q.push(T(nd, nv, nh, nw)); } } cout << (ans == INF ? -1 : ans) << endl; return 0; }