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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 10000000;

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

struct Edge {
  int to, cost;

  Edge(int t, int c) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
};
typedef vector<vector<Edge>> Graph;

int idx(int y, int x, int t) { return ((y + 100) * 210 + (x + 100)) * 6 + t; }

void Dijkstra(const Graph &edges, vector<int> &d, int s) {
  priority_queue<PII, vector<PII>, greater<PII>> pq;
  fill(d.begin(), d.end(), INF);
  d[s] = 0;
  pq.push(MP(0, s));

  while (!pq.empty()) {
    PII pii = pq.top();
    pq.pop();
    int v = pii.second;
    if (d[v] < pii.first)
      continue;
    for (int i = 0; i < edges[v].size(); ++i) {
      const Edge &e = edges[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(MP(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int sx, sy, gx, gy, n, lx, ly;
  cin >> sx >> sy >> gx >> gy >> n;

  bool ok[201][201];
  fill((bool *)ok, (bool *)ok + 201 * 201, true);
  REP(i, n) {
    int x, y;
    cin >> x >> y;
    ok[y + 100][x + 100] = false;
  }
  cin >> lx >> ly;

  Graph G(210 * 210 * 10);
  for (int y = -ly; y <= ly; ++y)
    for (int x = -lx; x <= lx; ++x)
      for (int t = 0; t < 6; ++t) {
        int dir = abs(x * y * t) % 6;
        REP(i, 6) {
          int tx = x + dx[abs(x) % 2][i];
          int ty = y + dy[abs(x) % 2][i];
          if (abs(tx) > lx || abs(ty) > ly || !ok[ty + 100][tx + 100])
            continue;
          G[idx(y, x, t)].PB(Edge(idx(ty, tx, (t + 1) % 6), i != dir));
        }
        G[idx(y, x, t)].PB(Edge(idx(y, x, (t + 1) % 6), 1));
      }
  VI dist(210 * 210 * 10);
  Dijkstra(G, dist, idx(sy, sx, 0));
  int ans = INF;
  REP(i, 6)
  ans = min(ans, dist[idx(gy, gx, i)]);

  cout << (ans >= INF ? -1 : ans) << endl;

  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 10000000;

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int sx, sy, gx, gy, n, lx, ly;
  cin >> sx >> sy >> gx >> gy >> n;

  bool ok[201][201];
  fill((bool *)ok, (bool *)ok + 201 * 201, true);
  REP(i, n) {
    int x, y;
    cin >> x >> y;
    ok[y + 100][x + 100] = false;
  }
  cin >> lx >> ly;

  int ans = INF;
  queue<pair<int, PII>> q;
  int crt = 0, nxt = 1;
  q.push(MP(0, MP(sx + 100, sy + 100)));
  for (int t = 0; t < 10000 && ans >= INF; ++t) {
    //	cout<<t<<endl;
    queue<pair<int, PII>> nxt;

    while (!q.empty()) {
      auto piii = q.front();
      q.pop();
      int tm = piii.first, x = piii.second.first, y = piii.second.second;

      for (int j = tm; j < 300; ++j) {
        nxt.push(MP(j, MP(x, y)));

        int dir = abs((x - 100) * (y - 100) * j) % 6;
        int tx = x + dx[x % 2][dir];
        int ty = y + dy[x % 2][dir];
        if (lx < abs(tx - 100) || ly < abs(ty - 100) || !ok[ty][tx])
          break;
        x = tx, y = ty;
      }
    }

    while (!nxt.empty()) {
      auto piii = nxt.front();
      nxt.pop();
      int tm = piii.first, x = piii.second.first, y = piii.second.second;
      q.push(MP(tm + 1, MP(x, y)));

      REP(i, 6) {
        if (i == abs((x - 100) * (y - 100) * tm) % 6)
          continue;
        int tx = x + dx[x % 2][i];
        int ty = y + dy[x % 2][i];
        if (lx < abs(tx - 100) || ly < abs(ty - 100) || !ok[ty][tx])
          continue;
        q.push(MP(tm + 1, MP(tx, ty)));
      }

      if (x == gx + 100 && y == gy + 100)
        ans = t;
    }
  }
  cout << (ans >= INF ? -1 : ans) << endl;

  return 0;
}