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

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int dy[] = {1, 0, -1, -1, -1, 0, 0}, dx[] = {0, 1, 1, 0, -1, -1, 0};
int sx, sy, gx, gy, n, lx, ly;
bool ob[300][300], v[300][300][6];

int main() {
  cin >> sx >> sy >> gx >> gy >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    ob[x + 150][y + 150] = 1;
  }
  cin >> lx >> ly;

  priority_queue<pair<pi, pi>> q;
  q.push(mp(mp(0, 0), mp(sx, sy)));
  while (!q.empty()) {
    int x = q.top().second.first, y = q.top().second.second;
    int t = -q.top().first.second, co = q.top().first.first;
    assert(abs(x) <= lx && abs(y) <= ly);
    q.pop();
    if (v[x + 150][y + 150][t % 6])
      continue;
    v[x + 150][y + 150][t % 6] = 1;

    if (x == gx && y == gy) {
      cout << -co << endl;
      return 0;
    }
    int dd = abs(x * y * t) % 6;
    rep(d, 7) {
      int nx = x + dx[d], ny = y + dy[d];
      if (dx[d] && x % 2)
        ny++;
      if (lx < abs(nx) || ly < abs(ny) || ob[nx + 150][ny + 150])
        continue;
      q.push(mp(mp(co - (d != dd), -t - 1), mp(nx, ny)));
    }
  }
  cout << -1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int dy[] = {1, 0, -1, -1, -1, 0, 0}, dx[] = {0, 1, 1, 0, -1, -1, 0};
int sx, sy, gx, gy, n, lx, ly;
bool ob[300][300], v[300][300][6];

int main() {
  cin >> sx >> sy >> gx >> gy >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    ob[x + 150][y + 150] = 1;
  }
  cin >> lx >> ly;

  priority_queue<pair<pi, pi>> q;
  q.push(mp(mp(0, 0), mp(sx, sy)));
  while (!q.empty()) {
    int x = q.top().second.first, y = q.top().second.second;
    int t = -q.top().first.second, co = q.top().first.first;
    q.pop();
    if (v[x + 150][y + 150][t % 6])
      continue;
    v[x + 150][y + 150][t % 6] = 1;

    if (x == gx && y == gy) {
      cout << -co << endl;
      return 0;
    }
    int dd = abs(x * y * t) % 6;
    rep(d, 7) {
      int nx = x + dx[d], ny = y + dy[d];
      if (dx[d] && x % 2)
        ny++;
      if (nx > abs(lx) || ny > abs(ly) || ob[nx + 150][ny + 150])
        continue;
      q.push(mp(mp(co - (d != dd), -t - 1), mp(nx, ny)));
    }
  }
  cout << -1 << endl;
  return 0;
}