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

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = (a); i < (b); ++i)

const int dx[7] = {0, 1, 1, 0, -1, -1, 0};

const int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}};

inline int cnv(int x) { return x + 150; }
inline bool in_range(int x, int mn, int mx) { return mn <= x && x < mx; }

int sx, sy, gx, gy, n, lx, ly;
bool board[300][300];

struct State {
  int x, y, t, ignore;
  bool operator>(const State &a) const { return ignore > a.ignore; }
};

int dst[300][300][7];

int djk() {
  memset(dst, -1, sizeof(dst));

  priority_queue<State, vector<State>, greater<State>> que;
  que.push(State{sx, sy, 0, 0});

  dst[cnv(sy)][cnv(sx)][0] = 0;

  while (!que.empty()) {
    State s = que.top();
    que.pop();

    int x = s.x, y = s.y, t = s.t, ignore = s.ignore;

    if (dst[cnv(y)][cnv(x)][t] < ignore)
      continue;

    if (x == gx && y == gy)
      return ignore;

    int d = ((int)abs(x * y * t)) % 6;
    int nt = (t + 1) % 6;

    for_(dd, 0, 7) {
      int nx = x + dx[dd], ny = y + dy[abs(x) & 1][dd];

      bool flag = false;

      if (!in_range(nx, -lx, lx + 1) || !in_range(ny, -ly, ly + 1))
        flag = true;

      if (!flag && board[cnv(ny)][cnv(nx)])
        flag = true;

      int add = (d == dd) ? 0 : 1;

      if (d == dd && flag) {
        nx = x;
        ny = y;
        flag = false;
        add = 1;
      }

      if (!flag) {
        int &nx_dst = dst[cnv(ny)][cnv(nx)][nt];

        if (nx_dst == -1 || nx_dst > ignore + add) {
          nx_dst = ignore + add;
          que.push(State{nx, ny, nt, ignore + add});
        }
      }
    }
  }

  return -1;
}

int main() {
  cin >> sx >> sy >> gx >> gy;
  cin >> n;

  memset(board, 0, sizeof(board));

  for_(i, 0, n) {
    int x, y;
    cin >> x >> y;
    board[cnv(y)][cnv(x)] = 1;
  }

  cin >> lx >> ly;

  cout << djk() << endl;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct State {
  int x, y, cnt, time;
  State(int _x, int _y, int _cn, int _t) : x(_x), y(_y), cnt(_cn), time(_t) {}
};

bool operator>(const State &s1, const State &s2) {
  if (s1.cnt > s2.cnt)
    return true;
  return false;
}

int sx, sy, gx, gy, lx, ly;
int n;

int MED = 101;
int map[202][202];

int dx[6] = {0, 1, 1, 0, -1, -1};
int dy[6] = {1, 0, -1, -1, -1, 0};
int dx2[6] = {0, 1, 1, 0, -1, -1};
int dy2[6] = {1, 1, 0, -1, 0, 1};

int INF = (int)1e9;
int dist[202][202][400];

void solve() {
  for (int i = 0; i < 202; i++) {
    for (int j = 0; j < 202; j++) {
      fill(dist[i][j], dist[i][j] + 400, INF);
    }
  }

  priority_queue<State, vector<State>, greater<State>> que;
  que.push(State(MED + sx, MED + sy, 0, 0));
  dist[MED + sy][MED + sx][0] = 0;

  int ans = INF;

  while (!que.empty()) {
    State s = que.top();
    que.pop();

    int x = s.x, y = s.y;
    int cnt = s.cnt, time = s.time;

    if (x == MED + gx && y == MED + gy) {
      ans = cnt;
      break;
    }

    int xyt = (int)abs((x - MED) * (y - MED) * time);
    int d = xyt % 6;
    time++;

    if (time >= 400)
      continue;

    if (dist[y][x][time] > cnt + 1) {
      dist[y][x][time] = cnt + 1;
      que.push(State(x, y, cnt + 1, time));
    }

    for (int i = 0; i < 6; i++) {
      int nx, ny;
      if ((x - MED) % 2) {
        nx = x + dx2[i];
        ny = y + dy2[i];
      } else {
        nx = x + dx[i];
        ny = y + dy[i];
      }

      if (abs(ny - MED) > ly || abs(nx - MED) > lx)
        continue;
      if (map[ny][nx] == -1)
        continue;

      if (d == i) {
        if (dist[ny][nx][time] > cnt) {
          dist[ny][nx][time] = cnt;
          que.push(State(nx, ny, cnt, time));
        }
      } else if (dist[ny][nx][time] > cnt + 1) {
        dist[ny][nx][time] = cnt + 1;
        que.push(State(nx, ny, cnt + 1, time));
      }
    }
  }

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

int main() {
  cin >> sx >> sy >> gx >> gy;

  cin >> n;
  memset(map, 0, sizeof(map));
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    map[y + MED][x + MED] = -1;
  }

  cin >> lx >> ly;

  solve();

  return 0;
}