結果
問題 |
No.34 砂漠の行商人
|
ユーザー |
|
提出日時 | 2017-09-18 14:04:05 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 1,750 ms / 5,000 ms |
コード長 | 1,722 bytes |
コンパイル時間 | 884 ms |
コンパイル使用メモリ | 86,456 KB |
実行使用メモリ | 408,576 KB |
最終ジャッジ日時 | 2024-06-28 10:37:43 |
合計ジャッジ時間 | 8,786 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:15:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 15 | int N, V, sr, sc, gr, gc; scanf("%d%d%d%d%d%d", &N, &V, &sc, &sr, &gc, &gr); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:20:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 20 | scanf("%d", &G[r][c]); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <iostream> #include <algorithm> #include <queue> #include <tuple> using namespace std; using i64 = long long; constexpr int inf = 987654321; // N E S W constexpr array<int, 4> dr = {-1, 0, 1, 0}, dc = { 0, 1, 0,-1}; constexpr int dr_size = dr.size(); int main(void) { int N, V, sr, sc, gr, gc; scanf("%d%d%d%d%d%d", &N, &V, &sc, &sr, &gc, &gr); --sr, --sc, --gr, --gc; vector<vector<int>> G(N, vector<int>(N, 0)); // G[N][N] for(int r=0; r<N; ++r) { for(int c=0; c<N; ++c) { scanf("%d", &G[r][c]); } } queue<tuple<int, int, int>> que; // 座標, 残りHP que.emplace(sr, sc, V); // cost[座標][残りHP] := 時間 vector<vector<vector<int>>> cost(N, vector<vector<int>>(N, vector<int>(V+1, inf))); // cost[N][N][V+1] cost[sr][sc][V] = 0; vector<vector<vector<bool>>> inque(N, vector<vector<bool>>(N, vector<bool>(V+1, false))); // inque[N][N][V+1] inque[sr][sc][V] = true; while(!que.empty()) { int r, c, v; tie(r, c, v) = que.front(); que.pop(); inque[r][c][v] = false; if(r == gr && c == gc) { break; } for(int i=0; i<dr_size; ++i) { int nr = r + dr[i], nc = c + dc[i]; if(!(0 <= nr && nr < N && 0 <= nc && nc < N)) { continue; } int nv = v - G[nr][nc]; if(nv <= 0) { continue; } if(cost[nr][nc][nv] > cost[r][c][v] + 1) { cost[nr][nc][nv] = cost[r][c][v] + 1; if(!inque[nr][nc][nv]) { inque[nr][nc][nv] = true; que.emplace(nr, nc, nv); } } } } int res = inf; for(int v=1; v<=V; ++v) { res = min(res, cost[gr][gc][v]); } if(res == inf) { res = -1; } printf("%d\n", res); return 0; }