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

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << "\\n"
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i))
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a), i##_len = (n); i > i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define chmax(a, b)                                                            \\
  if (a < b)                                                                   \\
  a = b
#define chmin(a, b)                                                            \\
  if (b < a)                                                                   \\
  a = b
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};
constexpr char dir[4] = {'r', 'u', 'l', 'd'};
#define N 4
#define N2 16
#define LIMIT 100
vv(int, MDT, N2, N2, 0);
struct Puzzle {
  vi f;
  int space, MD;
  Puzzle(int s = 0, int m = 0) : space(s), MD(m) {
    vi a(N2, 0);
    f = a;
  }
};
Puzzle state;
int limit;
vi path(LIMIT, 0);
int getAllMD(Puzzle pz) {
  int sum = 0;
  rep(i, 0, N2) {
    if (pz.f[i] == N2)
      continue;
    sum += MDT[i][pz.f[i] - 1];
  }
  return sum;
}
bool isSolved() {
  rep(i, 0, N2) if (state.f[i] != i + 1) return false;
  return true;
}
bool dfs(int depth, int prev) {
  if (state.MD == 0)
    return true;
  if (depth + state.MD > limit)
    return false;
  int sx = state.space / N;
  int sy = state.space % N;
  Puzzle tmp;
  int tx, ty;
  rep(r, 0, 4) {
    tx = sx + dx[r];
    ty = sy + dy[r];
    if (tx < 0 || tx >= N || ty < 0 || ty >= N)
      continue;
    if (max(prev, r) - min(prev, r) == 2)
      continue;
    tmp = state;
    state.MD -= MDT[tx * N + ty][state.f[tx * N + ty] - 1];
    state.MD += MDT[sx * N + sy][state.f[tx * N + ty] - 1];
    swap(state.f[tx * N + ty], state.f[sx * N + sy]);
    state.space = tx * N + ty;
    if (dfs(depth + 1, r)) {
      path[depth] = r;
      return true;
    }
    state = tmp;
  }
  return false;
}
string iterative_deepening(Puzzle in) {
  in.MD = getAllMD(in);
  string ans = "";
  for (limit = in.MD; limit <= LIMIT; ++limit) {
    state = in;
    if (dfs(0, -100)) {
      ans = "";
      rep(i, 0, limit) ans += dir[path[i]];
      return ans;
    }
  }
  return "unsolvable";
}
int main() {
  rep(i, 0, N2) {
    rep(j, 0, N2) { MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N); }
  }
  Puzzle in;
  rep(i, 0, N2) {
    cin >> in.f[i];
    if (in.f[i] == 0) {
      in.f[i] = N2;
      in.space = i;
    }
  }
  P(len(iterative_deepening(in)));
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << "\\n"
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i))
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a), i##_len = (n); i > i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define chmax(a, b)                                                            \\
  if (a < b)                                                                   \\
  a = b
#define chmin(a, b)                                                            \\
  if (b < a)                                                                   \\
  a = b
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};
constexpr char dir[4] = {'r', 'u', 'l', 'd'};
#define N 4
#define N2 16
vv(int, MDT, N2, N2, 0);
struct Puzzle {
  vi f;
  int space, MD, cost;
  Puzzle(int s = 0, int m = 0, int c = 0) : space(s), MD(m), cost(c) {
    vi a(N2, 0);
    f = a;
  }
  bool operator<(const Puzzle &p) const {
    rep(i, 0, N2) {
      if (f[i] == p.f[i])
        continue;
      return f[i] < p.f[i];
    }
    return false;
  }
};
struct State {
  Puzzle puzzle;
  int estimated;
  bool operator<(const State &s) const { return estimated > s.estimated; }
};
int getAllMD(Puzzle pz) {
  int sum = 0;
  rep(i, 0, N2) {
    if (pz.f[i] == N2)
      continue;
    sum += MDT[i][pz.f[i] - 1];
  }
  return sum;
}
int astar(Puzzle s) {
  priority_queue<State> PQ;
  s.MD = getAllMD(s);
  s.cost = 0;
  map<Puzzle, bool> V;
  Puzzle u, v;
  State initial;
  initial.puzzle = s;
  initial.estimated = getAllMD(s);
  PQ.emplace(initial);
  State st;
  int sx, sy, tx, ty;
  while (!PQ.empty()) {
    st = PQ.top();
    PQ.pop();
    u = st.puzzle;
    if (u.MD == 0)
      return u.cost;
    V[u] = true;
    sx = u.space / N;
    sy = u.space % N;
    rep(r, 0, 4) {
      tx = sx + dx[r];
      ty = sy + dy[r];
      if (tx < 0 || tx >= N || ty < 0 || ty >= N)
        continue;
      v = u;
      v.MD -= MDT[tx * N + ty][v.f[tx * N + ty] - 1];
      v.MD += MDT[sx * N + sy][v.f[tx * N + ty] - 1];
      swap(v.f[sx * N + sy], v.f[tx * N + ty]);
      v.space = tx * N + ty;
      if (!V[v]) {
        ++v.cost;
        State news;
        news.puzzle = v;
        news.estimated = v.cost + v.MD;
        PQ.emplace(news);
      }
    }
  }
  return -1;
}
int main() {
  rep(i, 0, N2) {
    rep(j, 0, N2) { MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N); }
  }
  Puzzle in;
  rep(i, 0, N2) {
    cin >> in.f[i];
    if (in.f[i] == 0) {
      in.f[i] = N2;
      in.space = i;
    }
  }
  P(astar(in));
  return 0;
}