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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define INF 1e4
using namespace std;

class data {
public:
  int x, y, t;
  data() {}
  data(int xx, int yy, int tt) {
    x = xx;
    y = yy;
    t = tt;
  }
};

int dx[6] = {0, 1, 1, 0, -1, -1};
int dy[2][6] = {1, 0, -1, -1, -1, 0, 1, 1, 0, -1, 0, 1};
int sx, sy, gx, gy;
int n;
int lx, ly;
int fie[2001][2001];
short dp[2001][2001][6];

int bfs() {
  for (int i = 0; i <= 2000; i++) {
    for (int j = 0; j <= 2000; j++) {
      for (int k = 0; k < 6; k++) {
        dp[i][j][k] = INF;
      }
    }
  }
  queue<data> que;
  que.push(data(sx + 1000, sy + 1000, 0));
  dp[sx + 1000][sy + 1000][0] = 0;
  while (que.size()) {
    queue<data> que2;
    while (que.size()) {
      data q = que.front();
      que.pop();
      if (q.x - 1000 == gx && q.y - 1000 == gy)
        return dp[q.x][q.y][q.t];
      que2.push(q);
      bool flag = true;
      while (flag) {
        flag = false;
        int sdir = abs((q.x - 1000) * (q.y - 1000) * q.t) % 6;
        int nx = q.x + dx[sdir], ny = q.y + dy[q.x % 2][sdir];
        if (nx >= 1000 - lx && nx <= lx + 1000 && ny >= 1000 - ly &&
            ny <= 1000 + ly) {
          if (fie[nx][ny] != -1 && dp[nx][ny][(q.t + 1) % 6] == INF) {
            flag = true;
            if (nx - 1000 == gx && ny - 1000 == gy)
              return dp[q.x][q.y][q.t];
            que2.push(data(nx, ny, (q.t + 1) % 6));
            dp[nx][ny][(q.t + 1) % 6] = dp[q.x][q.y][q.t];
          }
        }
        q.x = nx;
        q.y = ny;
        q.t = (q.t + 1) % 6;
      }
    }
    while (que2.size()) {
      data q = que2.front();
      que2.pop();
      if (dp[q.x][q.y][(q.t + 1) % 6] == INF) {
        dp[q.x][q.y][(q.t + 1) % 6] = dp[q.x][q.y][q.t] + 1;
        que.push(data(q.x, q.y, (q.t + 1) % 6));
      }
      for (int i = 0; i < 6; i++) {
        int nx = q.x + dx[i], ny = q.y + dy[q.x % 2][i];
        if (nx >= 1000 - lx && nx <= lx + 1000 && ny >= 1000 - ly &&
            ny <= 1000 + ly) {
          if (fie[nx][ny] != -1 && dp[nx][ny][(q.t + 1) % 6] == INF) {
            dp[nx][ny][(q.t + 1) % 6] = dp[q.x][q.y][q.t] + 1;
            que.push(data(nx, ny, (q.t + 1) % 6));
          }
        }
      }
    }
  }
  return -1;
}

int main(void) {
  scanf("%d%d%d%d", &sx, &sy, &gx, &gy);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    fie[x + 1000][y + 1000] = -1;
  }
  scanf("%d%d", &lx, &ly);
  printf("%d\\n", bfs());
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define INF 1e9
using namespace std;

class data {
public:
  int x, y, t;
  data() {}
  data(int xx, int yy, int tt) {
    x = xx;
    y = yy;
    t = tt;
  }
};

int dx[6] = {0, 1, 1, 0, -1, -1};
int dy[2][6] = {1, 0, -1, -1, -1, 0, 1, 1, 0, -1, 0, 1};
int sx, sy, gx, gy;
int n;
int lx, ly;
int fie[2001][2001];
int dp[2001][2001][6];

int bfs() {
  for (int i = 0; i <= 2000; i++) {
    for (int j = 0; j <= 2000; j++) {
      for (int k = 0; k < 6; k++) {
        dp[i][j][k] = INF;
      }
    }
  }
  queue<data> que;
  que.push(data(sx + 1000, sy + 1000, 0));
  dp[sx + 1000][sy + 1000][0] = 0;
  while (que.size()) {
    queue<data> que2;
    while (que.size()) {
      data q = que.front();
      que.pop();
      if (q.x - 1000 == gx && q.y - 1000 == gy)
        return dp[q.x][q.y][q.t];
      que2.push(q);
      bool flag = true;
      while (flag) {
        flag = false;
        int sdir = abs((q.x - 1000) * (q.y - 1000) * q.t) % 6;
        int nx = q.x + dx[sdir], ny = q.y + dy[q.x % 2][sdir];
        if (nx >= 1000 - lx && nx <= lx + 1000 && ny >= 1000 - ly &&
            ny <= 1000 + ly) {
          if (fie[nx][ny] != -1 && dp[nx][ny][(q.t + 1) % 6] == INF) {
            flag = true;
            if (nx - 1000 == gx && ny - 1000 == gy)
              return dp[q.x][q.y][q.t];
            que2.push(data(nx, ny, (q.t + 1) % 6));
            dp[nx][ny][(q.t + 1) % 6] = dp[q.x][q.y][q.t];
          }
        }
        q.x = nx;
        q.y = ny;
        q.t = (q.t + 1) % 6;
      }
    }
    while (que2.size()) {
      data q = que2.front();
      que2.pop();
      if (dp[q.x][q.y][(q.t + 1) % 6] == INF) {
        dp[q.x][q.y][(q.t + 1) % 6] = dp[q.x][q.y][q.t] + 1;
        que.push(data(q.x, q.y, (q.t + 1) % 6));
      }
      for (int i = 0; i < 6; i++) {
        int nx = q.x + dx[i], ny = q.y + dy[q.x % 2][i];
        if (nx >= 1000 - lx && nx <= lx + 1000 && ny >= 1000 - ly &&
            ny <= 1000 + ly) {
          if (fie[nx][ny] != -1 && dp[nx][ny][(q.t + 1) % 6] == INF) {
            dp[nx][ny][(q.t + 1) % 6] = dp[q.x][q.y][q.t] + 1;
            que.push(data(nx, ny, (q.t + 1) % 6));
          }
        }
      }
    }
  }
  return -1;
}

int main(void) {
  scanf("%d%d%d%d", &sx, &sy, &gx, &gy);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    fie[x + 1000][y + 1000] = -1;
  }
  scanf("%d%d", &lx, &ly);
  printf("%d\\n", bfs());
  return 0;
}