Problem p01539 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

const int INF = (1 << 29);

const int dy[2][6] = {{1, 0, -1, -1, -1, 0}, {1, 1, 0, -1, 0, 1}};
const int dx[6] = {0, 1, 1, 0, -1, -1};
int wmin, hmin, wmax, hmax;

class state {
public:
  int y, x, t, cost;
  state(int y = 0, int x = 0, int t = 0, int c = 0)
      : y(y), x(x), t(t), cost(c) {}
  bool operator<(const state &s) const { return cost > s.cost; }
};

bool check(int y, int x) {
  if (y < hmin || y > hmax)
    return false;
  if (x < wmin || x > wmax)
    return false;
  return true;
}

int main() {
  int sx, sy, gx, gy;
  while (cin >> sx >> sy >> gx >> gy) {
    sx += 100;
    sy += 100;
    gx += 100;
    gy += 100;
    int n, h, w, a[6][201][201], ans = INF;
    state u(sy, sx, 0, 0), v;
    queue<state> Q;
    for (int i = 0; i < 6; i++)
      for (int j = 0; j < 201; j++)
        for (int k = 0; k < 201; k++)
          a[i][j][k] = INF;
    a[0][sy][sx] = 0;

    cin >> n;
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      x += 100;
      y += 100;
      for (int j = 0; j < 6; j++)
        a[j][y][x] = -1;
    }
    cin >> w >> h;
    wmin = 100 - w;
    wmax = 100 + w;
    hmin = 100 - h;
    hmax = 100 + h;

    for (Q.push(u); !Q.empty();) {
      u = Q.front();
      Q.pop();
      if (u.y == gy && u.x == gx || ans <= u.cost) {
        ans = min(ans, u.cost);
        continue;
      }

      int next = abs((u.x - 100) * (u.y - 100) * u.t) % 6;
      v = u;
      v.y += dy[u.x % 2][next];
      v.x += dx[next];
      v.t++;
      if (check(v.y, v.x) && a[v.t % 6][v.y][v.x] > v.cost) {
        a[v.t % 6][v.y][v.x] = v.cost;
        Q.push(v);
      }

      v = u;
      v.t++;
      v.cost++;
      if (check(v.y, v.x) && a[v.t % 6][v.y][v.x] > v.cost) {
        a[v.t % 6][v.y][v.x] = v.cost;
        Q.push(v);
      }

      for (int i = 0; i < 6; i++) {
        v = u;
        v.y += dy[u.x % 2][i];
        v.x += dx[i];
        v.t++;
        v.cost++;
        if (check(v.y, v.x) && a[v.t % 6][v.y][v.x] > v.cost) {
          a[v.t % 6][v.y][v.x] = v.cost;
          Q.push(v);
        }
      }
    }

    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

const int INF = (1 << 29);

const int dy[2][6] = {{1, 0, -1, -1, -1, 0}, {1, 1, 0, -1, 0, 1}};
const int dx[6] = {0, 1, -1, 0, 1, -1};
int wmin, hmin, wmax, hmax;

class state {
public:
  int y, x, t, cost;
  state(int y = 0, int x = 0, int t = 0, int c = 0)
      : y(y), x(x), t(t), cost(c) {}
  bool operator<(const state &s) const { return cost > s.cost; }
};

bool check(int y, int x) {
  if (y < hmin || y > hmax)
    return false;
  if (x < wmin || x > wmax)
    return false;
  return true;
}

int main() {
  int sx, sy, gx, gy;
  while (cin >> sx >> sy >> gx >> gy) {
    sx += 100;
    sy += 100;
    gx += 100;
    gy += 100;
    int n, h, w, a[6][201][201], ans = INF;
    state u(sy, sx, 1, 0), v;
    queue<state> Q;
    for (int i = 0; i < 6; i++)
      for (int j = 0; j < 201; j++)
        for (int k = 0; k < 201; k++)
          a[i][j][k] = INF;
    a[1][sy][sx] = 0;

    cin >> n;
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      x += 100;
      y += 100;
      for (int j = 0; j < 6; j++)
        a[j][y][x] = -1;
    }
    cin >> w >> h;
    wmin = 100 - w;
    wmax = 100 + w;
    hmin = 100 - h;
    hmax = 100 + h;

    for (Q.push(u); !Q.empty(); Q.pop()) {
      u = Q.front();
      if (u.y == gy && u.x == gx || ans <= u.cost) {
        ans = min(ans, u.cost);
        continue;
      }

      int next = abs((u.x - 100) * (u.y - 100) * u.t) % 6;
      v = u;
      v.y += dy[u.x % 2][next];
      v.x += dx[next];
      v.t++;
      if (check(v.y, v.x) && a[v.t % 6][v.y][v.x] >= v.cost) {
        a[v.t % 6][v.y][v.x] = v.cost;
        Q.push(v);
      }

      v = u;
      v.t++;
      v.cost++;
      if (check(v.y, v.x) && a[v.t % 6][v.y][v.x] > v.cost) {
        a[v.t % 6][v.y][v.x] = v.cost;
        Q.push(v);
      }

      for (int i = 0; i < 6; i++) {
        v = u;
        v.y += dy[u.x % 2][i];
        v.x += dx[i];
        v.t++;
        v.cost++;
        if (check(v.y, v.x) && a[v.t % 6][v.y][v.x] > v.cost) {
          a[v.t % 6][v.y][v.x] = v.cost;
          Q.push(v);
        }
      }
    }

    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}