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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
typedef pair<P, int> PP;
const int MAX_XY = 210;
const int D = 7;
const int INF = 1e9;

int sx, sy, gx, gy, lx, ly, N;
int mx[] = {0, 1, 1, 0, -1, -1, 0};
int my0[] = {1, 0, -1, -1, -1, 0, 0};
int my1[] = {1, 1, 0, -1, 0, 1, 0};

class C {
public:
  int x, y, t, cost;
  C() {}
  C(int xx, int yy, int tt, int cc) {
    x = xx;
    y = yy;
    t = tt;
    cost = cc;
  }
  bool operator>(const C &c) const { return cost > c.cost; }
};

int solve(map<P, bool> &M) {
  priority_queue<C, vector<C>, greater<C>> open;
  open.push(C(sx, sy, 0, 0));
  int closed[MAX_XY][MAX_XY][D];
  REP(i, MAX_XY) REP(j, MAX_XY) REP(k, D) closed[i][j][k] = INF;
  while (!open.empty()) {
    C c = open.top();
    open.pop();
    int x = c.x, y = c.y, t = c.t, cost = c.cost;
    // cout <<"x = " <<x - 100 <<", y = " <<y - 100 <<", t = " <<t <<", cost = "
    // <<cost <<endl;
    if (x == gx && y == gy)
      return cost;
    if (closed[x][y][t % 6] <= cost)
      continue;
    closed[x][y][t % 6] = cost;
    REP(i, D) {
      int ny = y + (x % 2 ? my1[i] : my0[i]);
      int nx = x + mx[i],
          nc = cost + (abs((x - 100) * (y - 100) * t) % 6 == i ? 0 : 1);
      if (abs(nx - 100) > lx || abs(ny - 100) > ly ||
          M.find(P(nx, ny)) != M.end())
        continue;
      // cout <<"  -> " <<nx - 100 <<", " <<ny - 100 <<", " <<nc <<" ( " <<i <<"
      // ) " <<endl;
      open.push(C(nx, ny, t + 1, nc));
    }
  }
  return -1;
}

int main() {
  cin >> sx >> sy >> gx >> gy;
  sx += 100;
  sy += 100;
  gx += 100;
  gy += 100;
  cin >> N;
  map<P, bool> M;
  REP(i, N) {
    int x, y;
    cin >> x >> y;
    M[P(x + 100, y + 100)] = 1;
  }
  cin >> lx >> ly;
  cout << solve(M) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
typedef pair<P, int> PP;
const int MAX_XY = 210;
const int D = 7;
const int INF = 1e9;

int sx, sy, gx, gy, lx, ly, N;
int my[] = {1, 0, -1, -1, -1, 0, 0};
int mx[] = {0, 1, 1, 0, -1, -1, 0};

class C {
public:
  int x, y, t;
  C() {}
  C(int xx, int yy, int tt) {
    x = xx;
    y = yy;
    t = tt;
  }
  bool operator>(const C &c) const { return t > c.t; }
};

int solve(map<P, bool> &M) {
  priority_queue<C, vector<C>, greater<C>> open;
  open.push(C(sx, sy, 0));
  int closed[MAX_XY][MAX_XY][D];
  REP(i, MAX_XY) REP(j, MAX_XY) REP(k, D) closed[i][j][k] = INF;
  while (!open.empty()) {
    C c = open.top();
    open.pop();
    int x = c.x, y = c.y, t = c.t;
    if (x == gx && y == gy)
      return t;
    REP(nd, D) {
      int ny = y + my[nd], nx = x + mx[nd], nt = (abs(x * y * t) == nd ? 0 : 1);
      if (abs(nx - 100) > lx || abs(ny - 100) > ny ||
          M.find(P(nx, ny)) != M.end())
        continue;
      if (closed[nx][ny][nd] > nt) {
        closed[nx][ny][nd] = nt;
        open.push(C(nx, ny, nt));
      }
    }
  }
  return -1;
}

int main() {
  cin >> sx >> sy >> gx >> gy;
  sx += 100;
  sy += 100;
  gx += 100;
  gy += 100;
  cin >> N;
  map<P, bool> M;
  REP(i, N) {
    int x, y;
    cin >> x >> y;
    M[P(x + 100, y + 100)] = 1;
  }
  cin >> lx >> ly;
  lx += 100;
  ly += 100;
  cout << solve(M) << endl;
  return 0;
}