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

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int num = 16, ud[] = {0, -1, 0, 1}, lr[] = {1, 0, -1, 0}, nd = 4;
int md[num][num];
struct puzzle {
  int sp, p[num], mdp, cost;
  bool operator<(const puzzle &pu) const {
    for (int i = 0; i < num; i++) {
      if (p[i] == pu.p[i])
        continue;
      return p[i] < pu.p[i];
    }
    return false;
  }
};
struct state {
  puzzle pu;
  int es;
  bool operator<(const state &s) const {
    // return pu.mdp + pu.cost > s.pu.mdp + s.pu.cost;
    return es > s.es;
  }
};
int astar(puzzle pz) {
  priority_queue<state> pq;
  int sum = 0;
  for (int i = 0; i < num; i++) {
    if (pz.p[i] == num)
      continue;
    sum += md[i][pz.p[i] - 1];
  }
  pz.mdp = sum;
  pz.cost = 0;
  map<puzzle, bool> m;
  puzzle u, v;
  state initial;
  initial.pu = pz;
  initial.es = pz.mdp;
  pq.push(initial);
  while (!pq.empty()) {
    state st = pq.top();
    pq.pop();
    u = st.pu;
    if (!u.mdp)
      return u.cost;
    m[u] = true;
    int sx = u.sp / nd;
    int sy = u.sp % nd;
    for (int r = 0; r < nd; r++) {
      int tx = sx + ud[r];
      int ty = sy + lr[r];
      if (tx >= nd || tx < 0 || ty >= nd || ty < 0)
        continue;
      v = u;
      // v.mdp = p.mdp - md[tx * nd + ty][tmp.p[tx * nd + ty] - 1] + md[sx*nd +
      // sy][tmp.p[tx * nd + ty] - 1];
      v.mdp -= md[tx * nd + ty][v.p[tx * nd + ty] - 1];
      v.mdp += md[sx * nd + sy][v.p[tx * nd + ty] - 1];
      swap(v.p[sx * nd + sy], v.p[tx * nd + ty]);
      v.sp = tx * nd + ty;
      if (!m[v]) {
        v.cost++;
        state news;
        news.pu = v;
        news.es = v.cost + v.mdp;
        pq.push(news);
      }
    }
  }
  return -1;
}

int main() {
  puzzle in;
  for (int i = 0; i < num; i++)
    for (int j = 0; j < num; j++)
      md[i][j] = abs(i / nd - j / nd) + abs(i % nd - j % nd);
  for (int i = 0; i < num; i++) {
    cin >> in.p[i];
    if (!in.p[i]) {
      in.sp = i;
      in.p[i] = num;
    }
  }
  cout << astar(in) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int num = 16, ud[] = {-1, 0, 1, 0}, lr[] = {0, -1, 0, 1}, nd = 4;
int md[num][num];
struct puzzle {
  int sp, p[num], mdp, cost;
  bool operator<(const puzzle &pu) const {
    for (int i = 0; i < num; i++) {
      if (p[i] == pu.p[i])
        continue;
      return p[i] < pu.p[i];
    }
    return false;
  }
};
struct state {
  puzzle pu;
  int es;
  bool operator<(const state &s) const {
    // return pu.mdp + pu.cost > s.pu.mdp + s.pu.cost;
    return es > s.es;
  }
};
int astar(puzzle pz) {
  priority_queue<state> pq;
  int sum = 0;
  for (int i = 0; i < num; i++) {
    if (pz.p[i] == num)
      continue;
    sum += md[i][pz.p[i] - 1];
  }
  pz.mdp = sum;
  pz.cost = 0;
  map<puzzle, bool> m;
  puzzle u, v;
  state initial;
  initial.pu = pz;
  initial.es = pz.mdp;
  pq.push(initial);
  while (!pq.empty()) {
    state st = pq.top();
    pq.pop();
    u = st.pu;
    if (!u.mdp)
      return u.cost;
    m[u] = true;
    int sx = u.sp / nd;
    int sy = u.sp % nd;
    for (int r = 0; r < nd; r++) {
      int tx = sx + ud[r];
      int ty = sy + lr[r];
      if (tx >= nd || tx < 0 || ty >= nd || ty < 0)
        continue;
      v = u;
      // v.mdp = p.mdp - md[tx * nd + ty][tmp.p[tx * nd + ty] - 1] + md[sx*nd +
      // sy][tmp.p[tx * nd + ty] - 1];
      v.mdp -= md[tx * nd + ty][v.p[tx * nd + ty] - 1];
      v.mdp += md[sx * nd + sy][v.p[tx * nd + ty] - 1];
      swap(v.p[sx * nd + sy], v.p[tx * nd + ty]);
      v.sp = tx * nd + ty;
      if (!m[v]) {
        v.cost++;
        state news;
        news.pu = v;
        news.es = v.cost + v.mdp;
        pq.push(news);
      }
    }
  }
  return -1;
}

int main() {
  puzzle in;
  for (int i = 0; i < num; i++)
    for (int j = 0; j < num; j++)
      md[i][j] = abs(i / nd - j / nd) + abs(i % nd - j % nd);
  for (int i = 0; i < num; i++) {
    cin >> in.p[i];
    if (!in.p[i]) {
      in.sp = i;
      in.p[i] = num;
    }
  }
  cout << astar(in) << endl;
  return 0;
}