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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

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 pb push_back
#define mp make_pair
#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 range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

const int offset = 100;
int dist[6][210][210];
bool obs[210][210];

int dx[2][7] = {{0, 1, 1, 0, -1, -1, 0}, {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}};

typedef tuple<int, int, int, int> state;

int main(void) {
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  int n;
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    obs[x + offset][y + offset] = true;
  }
  int lx, ly;
  cin >> lx >> ly;
  rep(i, 6) for (int x = -lx; x <= lx; ++x) for (int y = -ly; y <= ly; ++y)
      dist[i][x + offset][y + offset] = inf;

  dist[0][sx + offset][sy + offset] = 0;
  priority_queue<state, vector<state>, greater<state>> q;
  q.push(state{0, 0, sx, sy});
  while (!q.empty()) {
    int cost, t, x, y;
    tie(cost, t, x, y) = q.top();
    q.pop();
    int p = abs(x * y * t) % 6;
    rep(i, 7) {
      int nx = x + dx[abs(x) & 1][i], ny = y + dy[abs(x) & 1][i],
          ncost = cost + (i != p);
      if (nx < -lx || lx < nx || ny < -ly || ly < ny)
        continue;
      if (obs[nx + offset][ny + offset])
        continue;
      if (dist[(t + 1) % 6][nx + offset][ny + offset] > ncost) {
        dist[(t + 1) % 6][nx + offset][ny + offset] = ncost;
        q.push(state{ncost, (t + 1) % 6, nx, ny});
      }
    }
  }
  int ans = inf;
  rep(i, 6) ans = min(ans, dist[i][gx + offset][gy + offset]);

  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

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 pb push_back
#define mp make_pair
#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 range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

const int offset = 100;
int dp[2][210][210];
bool obs[210][210];

int dx[2][7] = {{0, 1, 1, 0, -1, -1, 0}, {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}};

int main(void) {
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  int n;
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    obs[x + offset][y + offset] = true;
  }
  int lx, ly;
  cin >> lx >> ly;
  for (int x = -lx; x <= lx; ++x)
    for (int y = -ly; y <= ly; ++y)
      dp[0][x + offset][y + offset] = dp[1][x + offset][y + offset] = inf;

  dp[0][sx + offset][sy + offset] = 0;
  int ans = inf;
  rep(t, 50000) {
    ans = min(ans, dp[0][gx + offset][gy + offset]);
    for (int x = -lx; x <= lx; ++x) {
      for (int y = -ly; y <= ly; ++y) {
        int p = abs(x * y * t) % 6;
        rep(i, 7) {
          int nx = x + dx[abs(x) & 1][i], ny = y + dy[abs(x) & 1][i];
          if (nx < -lx || lx < nx || ny < -ly || ly < ny)
            continue;
          if (obs[nx + offset][ny + offset])
            continue;
          dp[1][nx + offset][ny + offset] =
              min(dp[1][nx + offset][ny + offset],
                  dp[0][x + offset][y + offset] + (i != p));
        }
      }
    }
    for (int x = -lx; x <= lx; ++x) {
      for (int y = -ly; y <= ly; ++y) {
        dp[0][x + offset][y + offset] = dp[1][x + offset][y + offset];
        dp[1][x + offset][y + offset] = inf;
      }
    }
  }

  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}