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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

map<vector<int>, int> mp;

int main() {
  vector<int> v;
  v.push_back(0);
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  v.push_back(4);
  v.push_back(5);
  v.push_back(6);
  v.push_back(7);
  v.push_back(8);
  v.push_back(9);
  v.push_back(10);
  v.push_back(11);
  v.push_back(0);
  queue<pair<int, vector<int>>> que;
  que.push(make_pair(0, v));
  while (!que.empty()) {
    pair<int, vector<int>> now = que.front();
    que.pop();
    if (mp.count(now.second) != 0)
      continue;
    mp[now.second] = now.first;
    if (now.first == 10)
      continue;
    rep(i, 13) {
      if (now.second[i] != 0)
        continue;
      if (i == 0) {
        v = now.second;
        swap(v[0], v[2]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 1) {
        v = now.second;
        swap(v[1], v[2]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[1], v[5]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 2) {
        v = now.second;
        swap(v[2], v[0]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[2], v[1]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[2], v[3]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[2], v[6]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 3) {
        v = now.second;
        swap(v[3], v[2]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[3], v[7]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 4) {
        v = now.second;
        swap(v[4], v[5]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 5) {
        v = now.second;
        swap(v[5], v[1]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[5], v[4]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[5], v[6]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[5], v[9]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 6) {
        v = now.second;
        swap(v[6], v[2]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[6], v[5]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[6], v[7]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[6], v[10]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 7) {
        v = now.second;
        swap(v[7], v[3]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[7], v[6]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[7], v[8]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[7], v[11]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 8) {
        v = now.second;
        swap(v[8], v[7]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 9) {
        v = now.second;
        swap(v[9], v[5]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[9], v[10]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 10) {
        v = now.second;
        swap(v[10], v[6]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[10], v[9]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[10], v[11]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[10], v[12]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 11) {
        v = now.second;
        swap(v[11], v[7]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[11], v[10]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 12) {
        v = now.second;
        swap(v[12], v[10]);
        que.push(make_pair(now.first + 1, v));
      }
    }
  }
  vector<int> p(13);
  for (;;) {
    cin >> p[0];
    if (p[0] == -1)
      break;
    rep(i, 12) cin >> p[i + 1];
    while (!que.empty())
      que.pop();
    map<vector<int>, int> m;
    que.push(make_pair(0, p));
    bool find = false;
    while (!que.empty()) {
      pair<int, vector<int>> now = que.front();
      que.pop();
      if (mp.count(now.second) != 0) {
        cout << now.first + mp[now.second] << endl;
        find = true;
        break;
      }
      if (m.count(now.second) != 0)
        continue;
      m[now.second] = now.first;
      if (now.first == 10)
        continue;
      rep(i, 13) {
        if (now.second[i] != 0)
          continue;
        if (i == 0) {
          v = now.second;
          swap(v[0], v[2]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 1) {
          v = now.second;
          swap(v[1], v[2]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[1], v[5]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 2) {
          v = now.second;
          swap(v[2], v[0]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[2], v[1]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[2], v[3]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[2], v[6]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 3) {
          v = now.second;
          swap(v[3], v[2]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[3], v[7]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 4) {
          v = now.second;
          swap(v[4], v[5]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 5) {
          v = now.second;
          swap(v[5], v[1]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[5], v[4]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[5], v[6]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[5], v[9]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 6) {
          v = now.second;
          swap(v[6], v[2]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[6], v[5]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[6], v[7]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[6], v[10]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 7) {
          v = now.second;
          swap(v[7], v[3]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[7], v[6]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[7], v[8]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[7], v[11]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 8) {
          v = now.second;
          swap(v[8], v[7]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 9) {
          v = now.second;
          swap(v[9], v[5]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[9], v[10]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 10) {
          v = now.second;
          swap(v[10], v[6]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[10], v[9]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[10], v[11]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[10], v[12]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 11) {
          v = now.second;
          swap(v[11], v[7]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[11], v[10]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 12) {
          v = now.second;
          swap(v[12], v[10]);
          que.push(make_pair(now.first + 1, v));
        }
      }
    }
    if (!find) {
      cout << "NA" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

map<vector<int>, int> mp;

int main() {
  vector<int> v;
  v.push_back(0);
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  v.push_back(4);
  v.push_back(5);
  v.push_back(6);
  v.push_back(7);
  v.push_back(8);
  v.push_back(9);
  v.push_back(10);
  v.push_back(11);
  v.push_back(0);
  queue<pair<int, vector<int>>> que;
  que.push(make_pair(0, v));
  while (!que.empty()) {
    pair<int, vector<int>> now = que.front();
    que.pop();
    if (mp.count(now.second) != 0)
      continue;
    mp[now.second] = now.first;
    if (now.first == 15)
      continue;
    rep(i, 13) {
      if (now.second[i] != 0)
        continue;
      if (i == 0) {
        v = now.second;
        swap(v[0], v[2]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 1) {
        v = now.second;
        swap(v[1], v[2]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[1], v[5]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 2) {
        v = now.second;
        swap(v[2], v[0]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[2], v[1]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[2], v[3]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[2], v[6]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 3) {
        v = now.second;
        swap(v[3], v[2]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[3], v[7]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 4) {
        v = now.second;
        swap(v[4], v[5]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 5) {
        v = now.second;
        swap(v[5], v[1]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[5], v[4]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[5], v[6]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[5], v[9]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 6) {
        v = now.second;
        swap(v[6], v[2]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[6], v[5]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[6], v[7]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[6], v[10]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 7) {
        v = now.second;
        swap(v[7], v[3]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[7], v[6]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[7], v[8]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[7], v[11]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 8) {
        v = now.second;
        swap(v[8], v[7]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 9) {
        v = now.second;
        swap(v[9], v[5]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[9], v[10]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 10) {
        v = now.second;
        swap(v[10], v[6]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[10], v[9]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[10], v[11]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[10], v[12]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 11) {
        v = now.second;
        swap(v[11], v[7]);
        que.push(make_pair(now.first + 1, v));
        v = now.second;
        swap(v[11], v[10]);
        que.push(make_pair(now.first + 1, v));
      }
      if (i == 12) {
        v = now.second;
        swap(v[12], v[10]);
        que.push(make_pair(now.first + 1, v));
      }
    }
  }
  vector<int> p(13);
  for (;;) {
    cin >> p[0];
    if (p[0] == -1)
      break;
    rep(i, 12) cin >> p[i + 1];
    while (!que.empty())
      que.pop();
    que.push(make_pair(0, p));
    bool find = false;
    while (!que.empty()) {
      pair<int, vector<int>> now = que.front();
      que.pop();
      if (mp.count(now.second) != 0) {
        cout << now.first + mp[now.second] << endl;
        find = true;
        break;
      }
      if (now.first == 5)
        continue;
      rep(i, 13) {
        if (now.second[i] != 0)
          continue;
        if (i == 0) {
          v = now.second;
          swap(v[0], v[2]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 1) {
          v = now.second;
          swap(v[1], v[2]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[1], v[5]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 2) {
          v = now.second;
          swap(v[2], v[0]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[2], v[1]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[2], v[3]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[2], v[6]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 3) {
          v = now.second;
          swap(v[3], v[2]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[3], v[7]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 4) {
          v = now.second;
          swap(v[4], v[5]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 5) {
          v = now.second;
          swap(v[5], v[1]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[5], v[4]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[5], v[6]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[5], v[9]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 6) {
          v = now.second;
          swap(v[6], v[2]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[6], v[5]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[6], v[7]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[6], v[10]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 7) {
          v = now.second;
          swap(v[7], v[3]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[7], v[6]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[7], v[8]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[7], v[11]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 8) {
          v = now.second;
          swap(v[8], v[7]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 9) {
          v = now.second;
          swap(v[9], v[5]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[9], v[10]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 10) {
          v = now.second;
          swap(v[10], v[6]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[10], v[9]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[10], v[11]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[10], v[12]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 11) {
          v = now.second;
          swap(v[11], v[7]);
          que.push(make_pair(now.first + 1, v));
          v = now.second;
          swap(v[11], v[10]);
          que.push(make_pair(now.first + 1, v));
        }
        if (i == 12) {
          v = now.second;
          swap(v[12], v[10]);
          que.push(make_pair(now.first + 1, v));
        }
      }
    }
    if (!find) {
      cout << "NA" << endl;
    }
  }
  return 0;
}