Problem p01539 (C++)

Buggy submission (status: Runtime Error):
// ?????¬???????????¬??????

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

// x coordinate mod 2 = 0
int dx1[] = {0, 1, 1, 0, -1, -1};
int dy1[] = {1, 0, -1, -1, -1, 0};

// mod 2 = 1
int dx2[] = {0, 1, 1, 0, -1, -1};
int dy2[] = {1, 1, 0, -1, 0, 1};

constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// ??§?¨? (x, y), ?????? z (mod 6) ????????????????°????
int rec[210][210][6];
int sx, sy, gx, gy;
int N, X[210], Y[210];
bool exist[210][210];
int lx, ly;

struct Elem {
  int x, y, t;
};

signed main() {
  rep(i, 0, 210) rep(j, 0, 210) rep(k, 0, 6) rec[i][j][k] = INF;
  cin >> sx >> sy >> gx >> gy;
  cin >> N;
  rep(i, 0, N) cin >> X[i] >> Y[i];
  cin >> lx >> ly;

  sx += lx, sy += ly, gx += lx, gy += ly;
  rep(i, 0, N) { exist[X[i] + lx][Y[i] + ly] = true; }

  rec[sx][sy][0] = 0;
  queue<Elem> q;
  q.push(Elem{sx, sy, 0});
  while (!q.empty()) {
    Elem cur = q.front();
    q.pop();

    // move
    int ax = abs(cur.x - lx), ay = abs(cur.y - ly);
    int dir = (ax * ay * (cur.t)) % 6;
    rep(i, 0, 6) {
      int nx, ny;
      if (ax % 2)
        nx = cur.x + dx2[i], ny = cur.y + dy2[i];
      else
        nx = cur.x + dx1[i], ny = cur.y + dy1[i];
      if (nx < 0 || nx > 2 * lx || ny < 0 || ny > 2 * ly)
        continue;
      if (exist[nx][ny])
        continue;
      if (rec[nx][ny][(cur.t + 1) % 6] >
          rec[cur.x][cur.y][cur.t] + (dir != i)) {
        rec[nx][ny][(cur.t + 1) % 6] = rec[cur.x][cur.y][cur.t] + (dir != i);
        /*
        if(nx-lx >= 0 && ny-ly >= 0)
            printf("(%lld, %lld) -> (%lld, %lld), time = %lld, cost = %lld\n",
            cur.x-lx, cur.y-ly, nx-lx, ny-ly, cur.t+1,
        rec[nx][ny][(cur.t+1)%6]);
        */
        q.push(Elem{nx, ny, (cur.t + 1) % 6});
      }
    }

    // stay
    if (rec[cur.x][cur.y][(cur.t + 1) % 6] > rec[cur.x][cur.y][cur.t] + 1) {
      rec[cur.x][cur.y][(cur.t + 1) % 6] = rec[cur.x][cur.y][cur.t] + 1;
      q.push(Elem{cur.x, cur.y, (cur.t + 1) % 6});
    }
  }

  int ans = INF;
  rep(i, 0, 6) chmin(ans, rec[gx][gy][i]);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Fixed submission:
// ?????¬???????????¬??????

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

// x coordinate mod 2 = 0
int dx1[] = {0, 1, 1, 0, -1, -1};
int dy1[] = {1, 0, -1, -1, -1, 0};

// mod 2 = 1
int dx2[] = {0, 1, 1, 0, -1, -1};
int dy2[] = {1, 1, 0, -1, 0, 1};

constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// ??§?¨? (x, y), ?????? z (mod 6) ????????????????°????
int rec[210][210][6];
int sx, sy, gx, gy;
int N, X[1010], Y[1010];
bool exist[210][210];
int lx, ly;

struct Elem {
  int x, y, t;
};

signed main() {
  rep(i, 0, 210) rep(j, 0, 210) rep(k, 0, 6) rec[i][j][k] = INF;
  cin >> sx >> sy >> gx >> gy;
  cin >> N;
  rep(i, 0, N) cin >> X[i] >> Y[i];
  cin >> lx >> ly;

  sx += lx, sy += ly, gx += lx, gy += ly;
  rep(i, 0, N) { exist[X[i] + lx][Y[i] + ly] = true; }

  rec[sx][sy][0] = 0;
  queue<Elem> q;
  q.push(Elem{sx, sy, 0});
  while (!q.empty()) {
    Elem cur = q.front();
    q.pop();

    // move
    int ax = abs(cur.x - lx), ay = abs(cur.y - ly);
    int dir = (ax * ay * (cur.t)) % 6;
    rep(i, 0, 6) {
      int nx, ny;
      if (ax % 2)
        nx = cur.x + dx2[i], ny = cur.y + dy2[i];
      else
        nx = cur.x + dx1[i], ny = cur.y + dy1[i];
      if (nx < 0 || nx > 2 * lx || ny < 0 || ny > 2 * ly)
        continue;
      if (exist[nx][ny])
        continue;
      if (rec[nx][ny][(cur.t + 1) % 6] >
          rec[cur.x][cur.y][cur.t] + (dir != i)) {
        rec[nx][ny][(cur.t + 1) % 6] = rec[cur.x][cur.y][cur.t] + (dir != i);
        /*
        if(nx-lx >= 0 && ny-ly >= 0)
            printf("(%lld, %lld) -> (%lld, %lld), time = %lld, cost = %lld\n",
            cur.x-lx, cur.y-ly, nx-lx, ny-ly, cur.t+1,
        rec[nx][ny][(cur.t+1)%6]);
        */
        q.push(Elem{nx, ny, (cur.t + 1) % 6});
      }
    }

    // stay
    if (rec[cur.x][cur.y][(cur.t + 1) % 6] > rec[cur.x][cur.y][cur.t] + 1) {
      rec[cur.x][cur.y][(cur.t + 1) % 6] = rec[cur.x][cur.y][cur.t] + 1;
      q.push(Elem{cur.x, cur.y, (cur.t + 1) % 6});
    }
  }

  int ans = INF;
  rep(i, 0, 6) chmin(ans, rec[gx][gy][i]);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}