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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <queue>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 28
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
static const int dx[7] = {0, 1, 1, 0, -1, -1, 0};
static const int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0},
                             {1, 1, 0, -1, 0, 1, 0}}; // even,odd
static const int dif = 150;

int main() {
  int d[400][400][7];
  bool bmap[400][400];
  rep(i, 400) {
    rep(j, 400) {
      rep(k, 7) { d[i][j][k] = INF; }
      bmap[i][j] = false;
    }
  }

  P s, g, l;
  int n, ans = -1;
  scanf("%d %d %d %d", &s.F, &s.S, &g.F, &g.S);
  scanf("%d", &n);
  rep(i, n) {
    int x, y;
    scanf("%d %d", &x, &y);
    x += dif;
    y += dif;
    bmap[y][x] = true;
  }
  scanf("%d %d", &l.F, &l.S);
  queue<PP> que;

  que.push(PP(s, P(0, 0)));
  d[s.S + dif][s.F + dif][0] = 0;

  deque<PP> deq;
  deq.push_front(PP(s, P(0, 0)));

  while (!deq.empty()) {
    PP p = deq.front();
    deq.pop_front();
    if (p.F == g) {
      ans = p.S.S;
      break;
    }
    int burden = abs(p.F.F * p.F.S * p.S.F) % 6, dir = (p.S.F + 1) % 6;
    rep(i, 7) {
      int nx = p.F.F + dx[i], ny = p.F.S + dy[p.F.F & 1][i];
      if (bmap[ny + dif][nx + dif])
        continue;
      if (ny < -l.S || nx < -l.F || l.S < ny || l.F < nx)
        continue;

      if (i == burden) {
        if (d[ny + dif][nx + dif][dir] > p.S.S) {
          d[ny + dif][nx + dif][dir] = p.S.S;
          deq.push_front(PP(P(nx, ny), P(dir, p.S.S)));
        }
      } else {
        if (d[ny + dif][nx + dif][dir] > p.S.S + 1) {
          d[ny + dif][nx + dif][dir] = p.S.S + 1;
          deq.push_back(PP(P(nx, ny), P(dir, p.S.S + 1)));
        }
      }
    }
  }

  /*
  while(!que.empty()){
    PP pp = que.front();que.pop();
    if(pp.F == g){
      ans = min(ans,pp.S.S);
      continue;
    }

    int burden = abs((pp.F.F-dif)*(pp.F.S-dif)*pp.S.F)%6,dir = (pp.S.F+1)%6;

    rep(i,7){
      int nx = pp.F.F+dx[i],ny = pp.F.S+dy[(pp.F.F-dif)%2][i];
      int Dcost = i==burden+1? pp.S.S:pp.S.S+1;



      if( (nx*nx<=l.F*l.F) && (ny*ny<=l.S*l.S) && !bmap[ny][nx] && (ans >
  pp.S.S) && d[ny][nx][dir] > Dcost){
        //cout << "in! nx = " << nx-dif << ",ny = " << ny-dif  << ", Dcost = "
  << Dcost<< endl; d[ny][nx][dir] = Dcost; que.push(PP(P(nx,ny),P(dir,Dcost)));
      }
    }

  }
*/
  cout << ans << endl;
  // if(ans != INF)printf("%d\\n",ans);
  // else printf("-1\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
static const int dx[7] = {0, 0, 1, 1, 0, -1, -1};
static const int dy[2][7] = {{0, 1, 0, -1, -1, -1, 0},
                             {0, 1, 1, 0, -1, 0, 1}}; // even,odd
static const int dif = 150;

int main() {
  int d[400][400][7];
  bool bmap[400][400];
  rep(i, 400) {
    rep(j, 400) {
      rep(k, 7) { d[i][j][k] = INF; }
      bmap[i][j] = false;
    }
  }

  P s, g, l;
  int n, ans = INF;
  scanf("%d %d %d %d", &s.F, &s.S, &g.F, &g.S);
  scanf("%d", &n);
  rep(i, n) {
    int x, y;
    scanf("%d %d", &x, &y);
    x += dif;
    y += dif;
    bmap[y][x] = true;
  }
  scanf("%d %d", &l.F, &l.S);
  queue<PP> que;
  s.F += dif;
  g.F += dif;
  l.F += dif;
  s.S += dif;
  g.S += dif;
  l.S += dif;
  que.push(PP(s, P(0, 0)));
  d[s.S][s.F][0] = 0;

  while (!que.empty()) {
    PP pp = que.front();
    que.pop();
    if (pp.F == g) {
      ans = min(ans, pp.S.S);
      continue;
    }

    int burden = abs((pp.F.F - dif) * (pp.F.S - dif) * pp.S.F) % 6,
        dir = (pp.S.F + 1) % 6;

    rep(i, 7) {
      int nx = pp.F.F + dx[i], ny = pp.F.S + dy[(pp.F.F - dif) % 2][i];
      int Dcost = i == burden + 1 ? pp.S.S : pp.S.S + 1;

      if ((nx * nx <= l.F * l.F) && (ny * ny <= l.S * l.S) && !bmap[ny][nx] &&
          (ans > pp.S.S) && d[ny][nx][dir] >= Dcost) {
        // cout << "in! nx = " << nx-dif << ",ny = " << ny-dif  << ", Dcost = "
        // << Dcost<< endl;
        d[ny][nx][dir] = Dcost;
        que.push(PP(P(nx, ny), P(pp.S.F + 1, Dcost)));
      }
    }
  }
  if (ans != INF)
    printf("%d\\n", ans);
  else
    printf("-1\\n");
  return 0;
}