結果

問題 No.34 砂漠の行商人
ユーザー kk
提出日時 2021-02-25 17:23:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 567 ms / 5,000 ms
コード長 1,203 bytes
コンパイル時間 2,399 ms
コンパイル使用メモリ 226,356 KB
実行使用メモリ 83,200 KB
最終ジャッジ日時 2024-04-08 15:11:24
合計ジャッジ時間 6,293 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 10 ms
6,548 KB
testcase_05 AC 18 ms
7,424 KB
testcase_06 AC 4 ms
6,548 KB
testcase_07 AC 33 ms
10,240 KB
testcase_08 AC 52 ms
14,208 KB
testcase_09 AC 185 ms
34,304 KB
testcase_10 AC 92 ms
21,632 KB
testcase_11 AC 45 ms
15,744 KB
testcase_12 AC 15 ms
6,548 KB
testcase_13 AC 567 ms
83,200 KB
testcase_14 AC 500 ms
72,832 KB
testcase_15 AC 36 ms
10,368 KB
testcase_16 AC 37 ms
10,496 KB
testcase_17 AC 75 ms
17,792 KB
testcase_18 AC 4 ms
6,548 KB
testcase_19 AC 193 ms
36,096 KB
testcase_20 AC 271 ms
46,464 KB
testcase_21 AC 3 ms
6,548 KB
testcase_22 AC 140 ms
28,032 KB
testcase_23 AC 57 ms
14,336 KB
testcase_24 AC 285 ms
50,304 KB
testcase_25 AC 65 ms
15,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1<<30;

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, v, sx, sy, gx, gy;
  cin >> n >> v >> sx >> sy >> gx >> gy;
  --sx, --sy;
  --gx, --gy;

  vector<vector<int> > bd(n, vector<int>(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> bd[i][j];

  vector<vector<map<int, int> > > dist(n, vector<map<int, int> >(n));
  queue<tuple<int, int, int, int> > q;
  q.emplace(sy, sx, v, 0);
  dist[sy][sx][v] = 0;
  
  while (!q.empty()) {
    int y, x, w, d;
    tie(y, x, w, d) = q.front();
    q.pop();

    for (int k = 0; k < 4; k++) {
      int y2 = y + dy[k];
      int x2 = x + dx[k];
      if (y2 < 0 || y2 >= n) continue;
      if (x2 < 0 || x2 >= n) continue;
      int w2 = w - bd[y2][x2];
      if (w2 <= 0) continue;
      if (dist[y2][x2].size() && dist[y2][x2].rbegin()->first >= w2) continue;
      dist[y2][x2][w2] = d + 1;
      q.emplace(y2, x2, w2, d + 1);
    }
  }

  int ret = INF;
  for (auto &[k, v]: dist[gy][gx]) {
    ret = min(ret, v);
  }

  if (ret == INF)
    ret = -1;
  cout << ret << endl;
  
  return 0;
}
0