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

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<vector<int>, int> P;
vector<int> v[13], s(13), t(13);
map<vector<int>, int> m;

int has[14];

void init() {

  v[0].push_back(2);

  v[1].push_back(2);
  v[1].push_back(5);

  v[2].push_back(0);
  v[2].push_back(1);
  v[2].push_back(3);
  v[2].push_back(6);

  v[3].push_back(2);
  v[3].push_back(7);

  v[4].push_back(5);

  v[5].push_back(1);
  v[5].push_back(4);
  v[5].push_back(6);
  v[5].push_back(9);

  v[6].push_back(2);
  v[6].push_back(5);
  v[6].push_back(7);
  v[6].push_back(10);

  v[7].push_back(3);
  v[7].push_back(6);
  v[7].push_back(8);
  v[7].push_back(11);

  v[8].push_back(7);

  v[9].push_back(5);
  v[9].push_back(10);

  v[10].push_back(6);
  v[10].push_back(9);
  v[10].push_back(11);
  v[10].push_back(12);

  v[11].push_back(7);
  v[11].push_back(10);

  v[12].push_back(10);

  int a = 20;
  int p = 1;

  r(i, 13) {
    has[i] = p;
    p *= a;
  }
}

int ha(vector<int> &x) {

  int re = 0;

  r(i, 13) { re += x[i] * has[i]; }

  return re;
}

vector<int> ah(int x) {
  vector<int> y(13);
  r(i, 13) {
    y[i] = x % 20;
    x /= 20;
  }
  return y;
}

void BFS1() {
  s[0] = 0;
  s[12] = 0;
  r(i, 11) s[i + 1] = i + 1;
  set<vector<int>> st;
  st.insert(s);
  m[s] = 0;
  queue<P> q;
  q.push(P(s, 0));
  while (1) {
    vector<int> now = q.front().first;
    int cost = q.front().second;
    q.pop();
    if (cost == 10)
      break;
    // cout<<m.size()<<' '<<cost<<endl;
    r(i, 13) if (now[i] == 0) {
      r(j, v[i].size()) {
        swap(now[i], now[v[i][j]]);
        if (!st.count(now)) {
          st.insert(now);
          m[now] = cost + 1;
          q.push(P(now, cost + 1));
        }
        swap(now[i], now[v[i][j]]);
      }
    }
  }
}

void BFS2() {
  set<vector<int>> st;
  st.insert(t);
  queue<P> q;
  q.push(P(t, 0));
  while (1) {
    vector<int> now = q.front().first;
    int cost = q.front().second;
    q.pop();
    if (m.count(now)) {
      cout << m[now] + cost << endl;
      return;
    }
    if (cost == 10)
      break;
    r(i, 13) if (now[i] == 0) {
      r(j, v[i].size()) {
        swap(now[i], now[v[i][j]]);
        if (!st.count(now)) {
          st.insert(now);
          if (m.count(now)) {
            cout << m[now] + cost + 1 << endl;
            return;
          }
          q.push(P(now, cost + 1));
        }
        swap(now[i], now[v[i][j]]);
      }
    }
  }
  cout << "NA" << endl;
}

signed main() {
  init();
  BFS1();
  while (cin >> t[0], t[0] != -1) {
    r(i, 12) cin >> t[i + 1];
    BFS2();
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
vector<int> v[13], s(13), t(13);
unordered_map<int, int> m;

int has[14];

void init() {

  v[0].push_back(2);

  v[1].push_back(2);
  v[1].push_back(5);

  v[2].push_back(0);
  v[2].push_back(1);
  v[2].push_back(3);
  v[2].push_back(6);

  v[3].push_back(2);
  v[3].push_back(7);

  v[4].push_back(5);

  v[5].push_back(1);
  v[5].push_back(4);
  v[5].push_back(6);
  v[5].push_back(9);

  v[6].push_back(2);
  v[6].push_back(5);
  v[6].push_back(7);
  v[6].push_back(10);

  v[7].push_back(3);
  v[7].push_back(6);
  v[7].push_back(8);
  v[7].push_back(11);

  v[8].push_back(7);

  v[9].push_back(5);
  v[9].push_back(10);

  v[10].push_back(6);
  v[10].push_back(9);
  v[10].push_back(11);
  v[10].push_back(12);

  v[11].push_back(7);
  v[11].push_back(10);

  v[12].push_back(10);

  int a = 20;
  int p = 1;

  r(i, 13) {
    has[i] = p;
    p *= a;
  }
}

int ha(vector<int> &x) {

  int re = 0;

  r(i, 13) { re += x[i] * has[i]; }

  return re;
}

vector<int> ah(int x) {
  vector<int> y(13);
  r(i, 13) {
    y[i] = x % 20;
    x /= 20;
  }
  return y;
}

void BFS1() {
  s[0] = 0;
  s[12] = 0;
  r(i, 11) s[i + 1] = i + 1;
  unordered_set<int> st;
  st.insert(ha(s));
  m[ha(s)] = 0;
  queue<P> q;
  q.push(P(ha(s), 0));
  while (1) {
    vector<int> now = ah(q.front().first);
    int cost = q.front().second;
    q.pop();
    if (cost == 8)
      break;
    // cout<<m.size()<<' '<<cost<<endl;
    r(i, 13) if (now[i] != 0) {
      r(j, v[i].size()) {
        swap(now[i], now[v[i][j]]);
        int yy = ha(now);
        if (!st.count(yy)) {
          st.insert(yy);
          m[yy] = cost + 1;
          q.push(P(yy, cost + 1));
        }
        swap(now[i], now[v[i][j]]);
      }
    }
  }
}

void BFS2() {
  unordered_set<int> st;
  st.insert(ha(t));
  queue<P> q;
  q.push(P(ha(t), 0));
  while (1) {
    vector<int> now = ah(q.front().first);
    int cost = q.front().second;
    int y = ha(now);
    if (m.count(y)) {
      cout << m[y] + cost << endl;
      return;
    }
    q.pop();
    if (cost == 6)
      break;
    r(i, 13) if (now[i] != 0) {
      r(j, v[i].size()) {
        swap(now[i], now[v[i][j]]);
        int yy = ha(now);
        if (!st.count(yy)) {
          st.insert(yy);
          q.push(P(yy, cost + 1));
        }
        swap(now[i], now[v[i][j]]);
      }
    }
  }
  cout << "NA" << endl;
}

signed main() {
  init();
  BFS1();
  while (cin >> t[0], t[0] != -1) {
    r(i, 12) cin >> t[i + 1];
    BFS2();
  }
}