Problem p01539 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define chmin(a, b) (a) = min((a), (b));
#define chmax(a, b) (a) = max((a), (b));
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(v)                                                              \\
  cerr << (#v) << ":";                                                         \\
  for (auto(x) : (v)) {                                                        \\
    cerr << " " << (x);                                                        \\
  }                                                                            \\
  cerr << endl;
#define printVS(vs)                                                            \\
  cerr << (#vs) << ":" << endl;                                                \\
  for (auto(s) : (vs)) {                                                       \\
    cerr << (s) << endl;                                                       \\
  }
#define printVV(vv)                                                            \\
  cerr << (#vv) << ":" << endl;                                                \\
  for (auto(v) : (vv)) {                                                       \\
    for (auto(x) : (v)) {                                                      \\
      cerr << " " << (x);                                                      \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define printP(p) cerr << (#p) << (p).first << " " << (p).second << endl;
#define printVP(vp)                                                            \\
  cerr << (#vp) << ":" << endl;                                                \\
  for (auto(p) : (vp)) {                                                       \\
    cerr << (p).first << " " << (p).second << endl;                            \\
  }

inline void output() { cerr << endl; }
template <typename First, typename... Rest>
inline void output(const First &first, const Rest &...rest) {
  cerr << first << " ";
  output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

vi DY(int x, int y) {
  return (abs(x) % 2) ? vi{1, 1, 0, -1, 0, 1, 0} : vi{1, 0, -1, -1, -1, 0, 0};
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  int n;
  cin >> n;
  set<Pii> st_forbidden;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    st_forbidden.emplace(x, y);
  }
  int lx, ly;
  cin >> lx >> ly;

  using State = tuple<int, int, int, int>; // cost, x, y, t % 6
  const int m = 6;
  map<TUPLE, int> d; // d[(x, y, t)] = c
  priority_queue<State, vector<State>, greater<State>> pq;
  d[TUPLE(sx, sy, 0)] = 0;
  pq.push(State(0, sx, sy, 0));

  while (!pq.empty()) {
    int c, x, y, t;
    tie(c, x, y, t) = pq.top();
    pq.pop();
    // output(c, x, y, t);
    // assert(abs(x) <= lx && abs(y) <= ly);
    if (x == gx && y == gy) {
      cout << c << endl;
      return 0;
    }
    if (d[TUPLE(x, y, t)] < c)
      continue;
    vi dx{0, 1, 1, 0, -1, -1, 0};
    vi dy = DY(x, y);
    rep(k, m + 1) {
      int nx = x + dx[k], ny = y + dy[k], nt = (t + 1) % m;
      if (abs(nx) <= lx && abs(ny) <= ly && !st_forbidden.count(Pii(nx, ny))) {
        int rem = abs(x) * abs(y) * t % m;
        // output(rem, k);
        int nc = c + (rem != k);
        if (!d.count(TUPLE(nx, ny, nt)) || d[TUPLE(nx, ny, nt)] > nc) {
          d[TUPLE(nx, ny, nt)] = nc;
          pq.emplace(nc, nx, ny, nt);
        }
      }
    }
  }

  cout << -1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define chmin(a, b) (a) = min((a), (b));
#define chmax(a, b) (a) = max((a), (b));
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(v)                                                              \\
  cerr << (#v) << ":";                                                         \\
  for (auto(x) : (v)) {                                                        \\
    cerr << " " << (x);                                                        \\
  }                                                                            \\
  cerr << endl;
#define printVS(vs)                                                            \\
  cerr << (#vs) << ":" << endl;                                                \\
  for (auto(s) : (vs)) {                                                       \\
    cerr << (s) << endl;                                                       \\
  }
#define printVV(vv)                                                            \\
  cerr << (#vv) << ":" << endl;                                                \\
  for (auto(v) : (vv)) {                                                       \\
    for (auto(x) : (v)) {                                                      \\
      cerr << " " << (x);                                                      \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define printP(p) cerr << (#p) << (p).first << " " << (p).second << endl;
#define printVP(vp)                                                            \\
  cerr << (#vp) << ":" << endl;                                                \\
  for (auto(p) : (vp)) {                                                       \\
    cerr << (p).first << " " << (p).second << endl;                            \\
  }

inline void output() { cerr << endl; }
template <typename First, typename... Rest>
inline void output(const First &first, const Rest &...rest) {
  cerr << first << " ";
  output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

vi DY(int x, int y) {
  return (abs(x) % 2) ? vi{1, 1, 0, -1, 0, 1} : vi{1, 0, -1, -1, -1, 0};
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  int n;
  cin >> n;
  set<Pii> st_forbidden;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    st_forbidden.emplace(x, y);
  }
  int lx, ly;
  cin >> lx >> ly;

  using State = tuple<int, int, int, int>; // x, y, t % 6, cost
  const int m = 6;
  set<TUPLE> st_visited; // x, y, t % 6
  deque<State> deq;
  deq.emplace_back(sx, sy, 0, 0);
  st_visited.emplace(sx, sy, 0);
  while (!deq.empty()) {
    int x, y, t, c;
    tie(x, y, t, c) = deq.front();
    deq.pop_front();
    if (x == gx && y == gy) {
      cout << c << endl;
      return 0;
    }
    vi dx{0, 1, 1, 0, -1, -1};
    vi dy = DY(x, y);
    rep(k, m) {
      int nx = x + dx[k], ny = y + dy[k], nt = (t + 1) % m;
      if (abs(nx) <= lx && abs(ny) <= ly && !st_forbidden.count(Pii(nx, ny)) &&
          !st_visited.count(TUPLE(nx, ny, nt))) {
        if (((abs(x) % m) * (abs(y) % m) * (abs(t) % m)) % m == k) {
          deq.emplace_front(nx, ny, nt, c);
        } else {
          deq.emplace_back(nx, ny, nt, c + 1);
        }
      }
    }
  }

  cout << -1 << endl;
}