Problem p01539 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

int dx[7] = {0, 1, 1, 0, -1, -1, 0};
int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}};

struct state {
  int x, y, t, c;
  state(int x, int y, int t, int c) : x(x), y(y), t(t), c(c){};
  bool operator<(const state &o) const { return o.c < c; }
};

int main() {
  vvi field(200 + 1, vi(200 + 1));
  int sx, sy, gx, gy, n, x, y, lx, ly;
  cin >> sx >> sy >> gx >> gy >> n;
  REP(i, n) {
    cin >> x >> y;
    field[y + 100][x + 100] = 1;
  }
  cin >> lx >> ly;

  priority_queue<state> Q;
  Q.push(state(sx + 100, sy + 100, 0, 0));
  vector<vvi> cost(200 + 1, vvi(200 + 1, vi(6, INF)));
  cost[sy][sx][0] = 0;
  while (!Q.empty()) {
    state st = Q.top();
    Q.pop();

    if (st.x == gx + 100 && st.y == gy + 100) {
      cout << st.c << endl;
      return 0;
    }

    REP(d, 7) {
      int nx = st.x + dx[d];
      int ny = st.y + dy[st.x % 2][d];
      if (nx < 0 || lx < abs(nx - 100) || ny < 0 || ly < abs(ny - 100) ||
          field[ny][nx]) {
        continue;
      }

      int nc = st.c;
      if (abs((st.x - 100) * (st.y - 100) * st.t) % 6 != d) {
        nc++;
      }

      if (nc < cost[ny][nx][(st.t + 1) % 6]) {
        cost[ny][nx][(st.t + 1) % 6] = nc;
        Q.push(state(nx, ny, st.t + 1, nc));
      }
    }
  }

  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

int dx[7] = {0, 1, 1, 0, -1, -1, 0};
int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}};

struct state {
  int x, y, t, c;
  state(int x, int y, int t, int c) : x(x), y(y), t(t), c(c){};
  bool operator<(const state &o) const { return o.c < c; }
};

int main() {
  vvi field(200 + 1, vi(200 + 1));
  int sx, sy, gx, gy, n, x, y, lx, ly;
  cin >> sx >> sy >> gx >> gy >> n;
  REP(i, n) {
    cin >> x >> y;
    field[y + 100][x + 100] = 1;
  }
  cin >> lx >> ly;

  priority_queue<state> Q;
  Q.push(state(sx + 100, sy + 100, 0, 0));
  vector<vvi> cost(200 + 1, vvi(200 + 1, vi(6, INF)));
  cost[sy + 100][sx + 100][0] = 0;
  while (!Q.empty()) {
    state st = Q.top();
    Q.pop();

    if (st.x == gx + 100 && st.y == gy + 100) {
      cout << st.c << endl;
      return 0;
    }

    REP(d, 7) {
      int nx = st.x + dx[d];
      int ny = st.y + dy[st.x % 2][d];
      if (nx < 0 || lx < abs(nx - 100) || ny < 0 || ly < abs(ny - 100) ||
          field[ny][nx]) {
        continue;
      }

      int nc = st.c;
      if (abs((st.x - 100) * (st.y - 100) * st.t) % 6 != d) {
        nc++;
      }

      if (nc < cost[ny][nx][(st.t + 1) % 6]) {
        cost[ny][nx][(st.t + 1) % 6] = nc;
        Q.push(state(nx, ny, st.t + 1, nc));
      }
    }
  }

  cout << -1 << endl;
  return 0;
}