結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  ninoinui | 
| 提出日時 | 2021-04-06 20:15:20 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 8 ms / 5,000 ms | 
| コード長 | 4,235 bytes | 
| コンパイル時間 | 3,471 ms | 
| コンパイル使用メモリ | 206,212 KB | 
| 最終ジャッジ日時 | 2025-01-20 12:29:24 | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
#ifndef _DEBUG
#define _DEBUG
#include __FILE__
signed main() {
  int N, V;
  cin >> N >> V;
  int H, W;
  H = N, W = N;
  int sh = 0, sw = 0, gh = H - 1, gw = W - 1;
  cin >> sw >> sh >> gw >> gh;
  sh--, sw--, gh--, gw--;
  vector<vector<int>> S(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> S.at(i).at(j);
    }
  }
  vector<vector<int>> cost(H, vector<int>(W, INT_MAX));
  cost.at(sh).at(sw) = 0;
  vector<int> mh{-1, 0, 1, 0}, mw{0, 1, 0, -1};
  queue<tuple<int, int, int>> Q;
  Q.push({sh, sw, 0});
  while (Q.size()) {
    auto [h, w, cnt] = Q.front();
    Q.pop();
    if (h == gh && w == gw) return cout << cnt << "\n", 0;
    for (int i = 0; i < 4; i++) {
      int nh = h + mh.at(i);
      int nw = w + mw.at(i);
      if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
      if (cost.at(h).at(w) + S.at(nh).at(nw) >= V) continue;
      if (cmin(cost.at(nh).at(nw), cost.at(h).at(w) + S.at(nh).at(nw))) Q.push({nh, nw, cnt + 1});
    }
  }
  cout << -1 << "\n";
}
#else
// #undef _DEBUG
#pragma GCC diagnostic warning "-Wextra"
#pragma GCC diagnostic warning "-Wshadow"
#include <bits/stdc++.h>
using namespace std;
struct io_setup {
  io_setup() { cin.tie(nullptr)->sync_with_stdio(false); }
} io_setup;
template <class A, class B> bool cmin(A& a, const B& b) {
  if (a > b) return a = b, true;
  return false;
}
template <class A, class B> bool cmax(A& a, const B& b) {
  if (a < b) return a = b, true;
  return false;
}
template <class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p);
template <class A, class B, class C> ostream& operator<<(ostream& os, const tuple<A, B, C>& t);
template <class A, class B, class C, class D> ostream& operator<<(ostream& os, const tuple<A, B, C, D>& t);
#define foreach(it, a) for (auto it = a.begin(); it != a.end(); it++)
ostream& operator<<(ostream& os, const vector<char>& vec) {
  os << "{";
  foreach (it, vec) { os << (it == vec.begin() ? "" : " ") << *it; }
  return os << "}";
}
template <class A> ostream& operator<<(ostream& os, const vector<A>& vec) {
  os << "{";
  foreach (it, vec) { os << (it == vec.begin() ? "" : ", ") << *it; }
  return os << "}";
}
template <class A> ostream& operator<<(ostream& os, const vector<vector<A>>& vec) {
  os << "{\n";
  foreach (it, vec) { os << (it == vec.begin() ? " " : "\n ") << *it; }
  return os << "\n}";
}
template <class A> ostream& operator<<(ostream& os, const vector<vector<vector<A>>>& vec) {
  os << "{\n";
  foreach (it, vec) { os << (it == vec.begin() ? "" : "\n") << it - vec.begin() << ": " << *it; }
  return os << "\n}";
}
template <class A> ostream& operator<<(ostream& os, const set<A>& se) {
  os << "{";
  foreach (it, se) { os << (it == se.begin() ? "" : ", ") << *it; }
  return os << "}";
}
template <class A> ostream& operator<<(ostream& os, const multiset<A>& ms) {
  os << "{";
  foreach (it, ms) { os << (it == ms.begin() ? "" : ", ") << *it; }
  return os << "}";
}
template <class A, class B> ostream& operator<<(ostream& os, const map<A, B>& ma) {
  os << "{";
  foreach (it, ma) { os << (it == ma.begin() ? "" : ", ") << *it; }
  return os << "}";
}
template <class A> ostream& operator<<(ostream& os, priority_queue<A> pq) {
  os << "{";
  while (!pq.empty()) {
    os << pq.top(), pq.pop();
    if (!pq.empty()) os << ", ";
  }
  return os << "}";
}
template <class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p) {
  const auto& [a, b] = p;
  return os << "(" << a << ", " << b << ")";
}
template <class A, class B, class C> ostream& operator<<(ostream& os, const tuple<A, B, C>& t) {
  const auto& [a, b, c] = t;
  return os << "(" << a << ", " << b << ", " << c << ")";
}
template <class A, class B, class C, class D> ostream& operator<<(ostream& os, const tuple<A, B, C, D>& t) {
  const auto& [a, b, c, d] = t;
  return os << "(" << a << ", " << b << ", " << c << ", " << d << ")";
}
void dump_func() {}
template <class A, class... B> void dump_func(const A& a, const B&... b) {
  cout << a << (sizeof...(b) ? ", " : "\n");
  dump_func(b...);
}
#ifdef _DEBUG
#define dump(...) cout << "[" << (#__VA_ARGS__) << "]: ", dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif
#endif
            
            
            
        