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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define FOR(v, c) for (auto &v : c)

#define EACH(it, c) for (auto it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()
#define DRANGE(c, p) (c).begin(), (c).begin() + p, (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

#include <deque>

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

struct State {
  int x, y, turn;

  State(int x, int y, int t) : x(x), y(y), turn(t) { return; }

  bool operator<(const State &a) const {
    if (x != a.x) {
      return x < a.x;
    }
    if (y != a.y) {
      return y < a.y;
    }
    return turn < a.turn;
  }

  State normalize() const { return State(x, y, turn % 6); }

  int direction() const { return abs(x * y * turn) % 6; }
};

int solve() {
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  int n;
  cin >> n;

  vector<PII> objects;
  REP(i, 0, n) {
    int x, y;
    cin >> x >> y;
    objects.PB(MP(x, y));
  }
  sort(ALL(objects));

  int lx, ly;
  cin >> lx >> ly;

  deque<State> que;
  que.PB(State(sx, sy, 0));
  void (deque<State>::*const pb)(const State &s) = &deque<State>::push_back;
  void (deque<State>::*const pf)(const State &s) = &deque<State>::push_front;

  map<State, int> cost;
  cost[State(sx, sy, 0)] = 0;

  while (!que.empty()) {
    const State cur = que.front();
    que.pop_front();

    if (cur.x == gx && cur.y == gy) {
      return cost[cur.normalize()];
    }

    const int dir = cur.direction();
    REP(d, 0, 6) {
      State next(cur);
      next.x += dx[d];
      next.y += dy[abs(cur.x % 2)][d];
      next.turn++;

      if (abs(next.x) <= lx && abs(next.y) <= ly &&
          !binary_search(ALL(objects), MP(next.x, next.y)) &&
          (!EXIST(cost, next.normalize()) ||
           cost[cur.normalize()] + (dir != d) < cost[next.normalize()])) {
        (que.*(dir == d ? pf : pb))(next);
        cost[next.normalize()] = cost[cur.normalize()] + (dir != d);
      }
    }

    { // stay
      State next(cur);
      next.turn++;

      if (!EXIST(cost, next.normalize()) ||
          cost[cur.normalize()] + 1 < cost[next.normalize()]) {
        que.PB(next);
        cost[next.normalize()] = cost[cur.normalize()] + 1;
      }
    }
  }

  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cout << solve() << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define FOR(v, c) for (auto &v : c)

#define EACH(it, c) for (auto it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()
#define DRANGE(c, p) (c).begin(), (c).begin() + p, (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

#include <deque>

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

struct State {
  int x, y, turn;

  State(int x, int y, int t) : x(x), y(y), turn(t) { return; }

  bool operator<(const State &a) const {
    if (x != a.x) {
      return x < a.x;
    }
    if (y != a.y) {
      return y < a.y;
    }
    return turn < a.turn;
  }

  State normalize() const { return State(x, y, direction()); }

  int direction() const { return abs(x * y * turn) % 6; }
};

int solve() {
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  int n;
  cin >> n;

  vector<PII> objects;
  REP(i, 0, n) {
    int x, y;
    cin >> x >> y;
    objects.PB(MP(x, y));
  }
  sort(ALL(objects));

  int lx, ly;
  cin >> lx >> ly;

  deque<State> que;
  que.PB(State(sx, sy, 0));

  map<State, int> cost;
  cost[State(sx, sy, 0)] = 0;

  while (!que.empty()) {
    State cur = que.front();
    que.pop_front();

    if (cur.x == gx && cur.y == gy) {
      return cost[cur.normalize()];
    }

    REP(d, 0, 6) {
      const int dir = cur.direction();

      State next(cur);
      next.x += dx[d];
      next.y += dy[cur.x % 2][d];
      next.turn++;

      if (abs(next.x) <= lx && abs(next.y) <= ly &&
          !binary_search(ALL(objects), MP(next.x, next.y)) &&
          (!EXIST(cost, next.normalize()) ||
           cost[cur.normalize()] + dir != d < cost[next.normalize()])) {
        if (dir == d) {
          que.push_front(next);
        } else {
          que.PB(next);
        }
        cost[next.normalize()] = cost[cur.normalize()] + dir != d;
      }
    }

    { // stay
      State next(cur);
      next.turn++;

      if (!EXIST(cost, next.normalize())) {
        que.PB(next);
        cost[next.normalize()] = cost[cur.normalize()] + 1;
      }
    }
  }

  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cout << solve() << endl;

  return 0;
}