結果

問題 No.34 砂漠の行商人
ユーザー simansiman
提出日時 2021-05-25 16:19:45
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 5,000 ms
コード長 1,609 bytes
コンパイル時間 3,682 ms
コンパイル使用メモリ 141,024 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 03:50:17
合計ジャッジ時間 5,059 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 11 ms
5,376 KB
testcase_08 AC 13 ms
5,376 KB
testcase_09 AC 31 ms
5,376 KB
testcase_10 AC 20 ms
5,376 KB
testcase_11 AC 9 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 89 ms
5,376 KB
testcase_14 AC 68 ms
5,376 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 9 ms
5,376 KB
testcase_17 AC 17 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 36 ms
5,376 KB
testcase_20 AC 44 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 26 ms
5,376 KB
testcase_23 AC 13 ms
5,376 KB
testcase_24 AC 47 ms
5,376 KB
testcase_25 AC 13 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};

struct Node {
  int y;
  int x;
  int h;
  int dist;

  Node(int y = -1, int x = -1, int h = -1, int dist = 0) {
    this->y = y;
    this->x = x;
    this->h = h;
    this->dist = dist;
  }

  bool operator>(const Node &n) const {
    return dist > n.dist;
  }
};

int main() {
  int N, V, SX, SY, GX, GY;
  cin >> N >> V >> SX >> SY >> GX >> GY;
  --SY;
  --SX;
  --GY;
  --GX;
  int L[N][N];

  for (int y = 0; y < N; ++y) {
    for (int x = 0; x < N; ++x) {
      cin >> L[y][x];
    }
  }


  queue<Node> que;
  que.push(Node(SY, SX, V));
  int visited[N][N];
  memset(visited, 0, sizeof(visited));
  int ans = INT_MAX;

  while (not que.empty()) {
    Node node = que.front();
    que.pop();

    if (node.y == GY && node.x == GX) {
      ans = min(ans, node.dist);
      continue;
    }

    if (visited[node.y][node.x] >= node.h) continue;
    visited[node.y][node.x] = node.h;

    for (int direct = 0; direct < 4; ++direct) {
      int ny = node.y + DY[direct];
      int nx = node.x + DX[direct];
      if (ny < 0 || N <= ny || nx < 0 || N <= nx) continue;
      int nh = node.h - L[ny][nx];
      if (nh <= 0) continue;

      que.push(Node(ny, nx, nh, node.dist + 1));
    }
  }

  if (ans == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}
0