結果

問題 No.34 砂漠の行商人
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-09-18 14:04:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,688 ms / 5,000 ms
コード長 1,722 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 88,292 KB
実行使用メモリ 408,472 KB
最終ジャッジ日時 2023-09-10 19:30:12
合計ジャッジ時間 9,405 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 12 ms
5,368 KB
testcase_05 AC 12 ms
5,692 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 18 ms
7,044 KB
testcase_08 AC 26 ms
8,860 KB
testcase_09 AC 514 ms
237,828 KB
testcase_10 AC 391 ms
407,496 KB
testcase_11 AC 1,688 ms
408,472 KB
testcase_12 AC 9 ms
5,308 KB
testcase_13 AC 1,522 ms
106,196 KB
testcase_14 AC 1,042 ms
102,300 KB
testcase_15 AC 36 ms
44,448 KB
testcase_16 AC 103 ms
84,260 KB
testcase_17 AC 24 ms
32,024 KB
testcase_18 AC 9 ms
9,924 KB
testcase_19 AC 207 ms
25,368 KB
testcase_20 AC 407 ms
41,084 KB
testcase_21 AC 7 ms
6,372 KB
testcase_22 AC 34 ms
40,508 KB
testcase_23 AC 95 ms
119,612 KB
testcase_24 AC 876 ms
214,088 KB
testcase_25 AC 27 ms
10,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0