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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int x[] = {1, 8, 6, 3, 2, 0, 7, 4, 3, 1, 8, 5, 4, 2, 0, 6, 5, 3,
           1, 7, 6, 4, 2, 8, 7, 5, 3, 0, 8, 6, 4, 1, 0, 7, 5, 2};
vector<int> a[362889];
int p[11], cnt, fact[11];
bool used[11];
void build(int pos) {
  if (pos == 9) {
    a[cnt].resize(9);
    for (int i = 0; i < 9; i++)
      a[cnt][i] = p[i];
    cnt++;
    return;
  }
  for (int i = 0; i < 9; i++) {
    if (!used[i]) {
      used[i] = true;
      p[pos] = i;
      build(pos + 1);
      used[i] = false;
    }
  }
}
int perm(vector<int> v) {
  int ret = 0;
  for (int i = 0; i < 9; i++) {
    for (int j = i + 1; j < 9; j++) {
      if (v[i] > v[j])
        ret += fact[8 - i];
    }
  }
  return ret;
}
struct state {
  int pos, cost;
};
bool operator<(const state &s1, const state &s2) { return s1.cost < s2.cost; }
int c[2], dist[362889], g[1451529];
int main() {
  build(0);
  fact[0] = 1;
  for (int i = 1; i <= 9; i++)
    fact[i] = fact[i - 1] * i;
  for (int i = 0; i < 362880; i++) {
    int ptr = -1;
    for (int j = 0; j < 9; j++) {
      if (a[i][j] == 0)
        ptr = j;
    }
    vector<int> t = a[i];
    for (int j = 0; j < 4; j++) {
      swap(t[ptr], t[x[ptr * 4 + j]]);
      g[i * 4 + j] = perm(t);
      swap(t[ptr], t[x[ptr * 4 + j]]);
    }
  }
  while (cin >> c[0] >> c[1], c[0] | c[1]) {
    vector<int> d(9), e(9);
    for (int i = 0; i < 9; i++)
      cin >> d[i];
    for (int i = 0; i < 9; i++)
      cin >> e[i];
    int fd = perm(d), fe = perm(e);
    fill(dist, dist + 362880, 999999999);
    dist[fd] = 0;
    priority_queue<state> que;
    que.push(state{fd, 0});
    while (!que.empty()) {
      int u = que.top().pos;
      que.pop();
      for (int i = u * 4; i < u * 4 + 4; i++) {
        int z = g[i], cost = c[(i & 2) >> 1];
        if (dist[z] > dist[u] + cost) {
          dist[z] = dist[u] + cost;
          que.push(state{z, -dist[z]});
        }
      }
    }
    cout << dist[fe] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
int a, b, fact[11];
vector<int> r1, r2;
inline int func(vector<int> &v) {
  int ret = 1;
  for (int i = 0; i < 9; i++) {
    int cnt = 0;
    for (int j = i + 1; j < 9; j++) {
      if (v[i] > v[j])
        cnt++;
    }
    ret += cnt * fact[8 - i];
  }
  return ret;
}
int solve() {
  if (r1 == r2)
    return 0;
  bool d[370000];
  fill(d, d + 362880, false);
  d[func(r1)] = true;
  priority_queue<tuple<int, vector<int>>> que;
  que.push(make_pair(0, r1));
  int w = 0;
  while (true) {
    tuple<int, vector<int>> state = que.top();
    que.pop();
    int cost = -get<0>(state);
    vector<int> v = get<1>(state);
    int ptr = find(v.begin(), v.end(), 0) - v.begin();
    if (v == r2)
      return cost;
    d[func(v)] = true;
    for (int i = 1; i <= 8; i += 7) {
      vector<int> u = v;
      swap(u[ptr], u[(ptr + i) % 9]);
      if (!d[func(u)]) {
        que.push(make_tuple(-cost - a, u));
      }
    }
    for (int i = ptr % 3; i < 9; i += 3) {
      if (i == ptr)
        continue;
      vector<int> u = v;
      swap(u[ptr], u[i]);
      if (!d[func(u)])
        que.push(make_tuple(-cost - b, u));
    }
  }
  return -1;
}
int main() {
  fact[0] = 1;
  for (int i = 1; i < 10; i++)
    fact[i] = fact[i - 1] * i;
  while (cin >> a >> b, a | b) {
    r1 = vector<int>(9);
    for (int i = 0; i < 9; i++)
      cin >> r1[i];
    r2 = vector<int>(9);
    for (int i = 0; i < 9; i++)
      cin >> r2[i];
    cout << solve() << endl;
  }
}