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

#include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2425&lang=jp
typedef long long ll;
#define INF 1 << 30
#define MAXL 256
#define CENTER 128
int d[MAXL][MAXL][6];
bool masu[MAXL][MAXL];
int dx[7] = {0, 1, 1, 0, -1, -1, 0};
int dy1[7] = {1, 0, -1, -1, -1, 0, 0};
int dy2[7] = {1, 1, 0, -1, 0, 1, 0};
void init(int lx, int ly) {
  for (int i = -ly - 1; i <= ly + 1; i++) {
    masu[CENTER + lx + 1][CENTER + i] = true;
    masu[CENTER - lx - 1][CENTER + i] = true;
  }
  for (int i = -lx - 1; i <= lx + 1; i++) {
    masu[CENTER + i][CENTER + ly + 1] = true;
    masu[CENTER + i][CENTER - ly - 1] = true;
  }

  for (int i = 0; i < MAXL; i++) {
    for (int j = 0; j < MAXL; j++) {
      for (int k = 0; k < 6; k++) {
        d[i][j][k] = INF;
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx += CENTER;
  sy += CENTER;
  gx += CENTER;
  gy += CENTER;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    masu[CENTER + x][CENTER + y] = true;
  }
  int lx, ly;
  cin >> lx >> ly;
  init(lx, ly);

  /*                 x    y    t   */
  using Key = tuple<int, int, int>;
  d[sx][sy][0] = 0;
  queue<Key> q;
  q.push(Key(sx, sy, 0));
  while (!q.empty()) {
    int x, y, t;
    tie(x, y, t) = q.front();
    q.pop();
    int dir = abs((x - CENTER) * (y - CENTER) * t) % 6;
    // int dir = t % 6;
    int nx, ny;
    int nt = t + 1;
    for (int i = 0; i < 7; i++) {
      nx = x + dx[i];
      if (x % 2 == 0) {
        ny = y + dy1[i];
      } else {
        ny = y + dy2[i];
      }

      int cost = 1;
      if (masu[nx][ny])
        continue;

      if (i == dir)
        cost = 0;

      // int next_dir = abs((nx - CENTER)*(ny - CENTER)*nt) % 6;
      // int next_dir = nt % 6;
      if (d[nx][ny][nt % 6] > d[x][y][t % 6] + cost) {
        d[nx][ny][nt % 6] = d[x][y][t % 6] + cost;
        if (nx == gx && ny == gy)
          continue;
        q.push(Key(nx, ny, nt));
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < 6; i++) {
    ans = min(ans, d[gx][gy][i]);
  }
  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2425&lang=jp
typedef long long ll;
#define INF 1 << 30
#define MAXL 2012
#define CENTER 1006
int d[MAXL][MAXL][6];
bool masu[MAXL][MAXL];
int dx[6] = {0, 1, 1, 0, -1, -1};
int dy1[6] = {1, 0, -1, -1, -1, 0};
int dy2[6] = {1, 1, 0, -1, 0, 1};
void init(int lx, int ly) {
  for (int i = -ly - 1; i <= ly + 1; i++) {
    masu[CENTER + lx + 1][CENTER + i] = true;
    masu[CENTER - lx - 1][CENTER + i] = true;
  }
  for (int i = -lx - 1; i <= lx + 1; i++) {
    masu[CENTER + i][CENTER + ly + 1] = true;
    masu[CENTER + i][CENTER - ly - 1] = true;
  }

  for (int i = 0; i < MAXL; i++) {
    for (int j = 0; j < MAXL; j++) {
      for (int k = 0; k < 6; k++) {
        d[i][j][k] = INF;
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx += CENTER;
  sy += CENTER;
  gx += CENTER;
  gy += CENTER;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    masu[CENTER + x][CENTER + y] = true;
  }
  int lx, ly;
  cin >> lx >> ly;
  init(lx, ly);
  // for (int i = +4; i >= -4;i--) {
  //	for (int j = -4; j <= 4;j++) {
  //		if (masu[CENTER + i][CENTER + j]) {
  //			cout << "INF ";
  //		}
  //		else {
  //			cout << setw(3) << masu[CENTER + i][CENTER + j] << " ";
  //		}
  //	}
  //	cout << endl;
  // }

  /*                 x    y    t   */
  using Key = tuple<int, int, int>;
  d[sx][sy][0] = 0;
  queue<Key> q;
  q.push(Key(sx, sy, 0));
  while (!q.empty()) {
    int x, y, t;
    tie(x, y, t) = q.front();
    q.pop();
    // cout << "(" << x-CENTER << ", " << y-CENTER << ") : " << t << endl;
    int dir = abs((x - CENTER) * (y - CENTER) * t) % 6;
    // cout << dir << " // " << d[x][y][dir] << endl;
    int nx, ny;
    nx = x + dx[dir];
    if (x % 2 == 0) {
      ny = y + dy1[dir];
    } else
      ny = y + dy2[dir];

    int nt = t + 1;
    if (!masu[nx][ny]) {
      int next_dir = abs((nx - CENTER) * (ny - CENTER) * nt) % 6;
      if (d[nx][ny][next_dir] > d[x][y][dir]) {
        d[nx][ny][next_dir] = d[x][y][dir];
        if (nx == gx && ny == gy)
          continue;
        q.push(Key(nx, ny, nt));
      }
      continue;
    }
    for (int i = 0; i < 6; i++) {
      nx = x + dx[i];
      if (x % 2 == 0) {
        ny = y + dy1[i];
      } else
        ny = y + dy2[i];

      if (masu[nx][ny])
        continue;
      int next_dir = abs((nx - CENTER) * (ny - CENTER) * nt) % 6;
      if (d[nx][ny][next_dir] > d[x][y][dir] + 1) {
        d[nx][ny][next_dir] = d[x][y][dir] + 1;
        if (nx == gx && ny == gy)
          continue;
        q.push(Key(nx, ny, nt));
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < 6; i++) {
    ans = min(ans, d[gx][gy][i]);
  }
  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}