結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  siman | 
| 提出日時 | 2021-05-25 16:19:45 | 
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 86 ms / 5,000 ms | 
| コード長 | 1,609 bytes | 
| コンパイル時間 | 4,116 ms | 
| コンパイル使用メモリ | 141,800 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-14 02:44:05 | 
| 合計ジャッジ時間 | 3,203 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
#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;
}
            
            
            
        