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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;
const ll FINAL = 0x0ba9876543210ll;

struct Data {
  ll s;
  int d;
  bool f;
  Data(ll s, int d, bool f) : s(s), d(d), f(f) {}
};

int bfs(ll init) {
  if (init == FINAL)
    return 0;

  const int move[13][5] = {
      {2, -1}, {2, 5, -1},       {0, 1, 3, 6, -1},   {2, 7, -1},
      {5, -1}, {1, 4, 6, 9, -1}, {2, 5, 7, 10, -1},  {3, 6, 8, 11, -1},
      {7, -1}, {5, 10, -1},      {6, 9, 11, 12, -1}, {7, 10, -1},
      {10, -1}};

  set<ll> visited[2];
  queue<Data> Q;
  Q.push(Data(init, 0, false));
  Q.push(Data(FINAL, 0, true));
  visited[0].insert(init);
  visited[1].insert(FINAL);
  int now[2] = {0};

  while (!Q.empty()) {
    Data data = Q.front();
    Q.pop();
    now[data.f] = data.d + 1;

    for (int i = 0, cnt = 2; cnt; ++i) {
      if (((data.s >> (i * 4)) & 0xfll) == 0) {
        for (int j = 0; move[i][j] != -1; ++j) {
          ll next = data.s;
          next &= ~((0xfll << (i * 4)) | (0xfll << (move[i][j] * 4)));
          next |= ((data.s >> (move[i][j] * 4)) & 0xfll) << (i * 4);

          if (visited[!data.f].find(next) != visited[!data.f].end())
            return now[0] + now[1];

          if (visited[data.f].find(next) == visited[data.f].end()) {
            visited[data.f].insert(next);
            if (now[0] + now[1] < 20)
              Q.push(Data(next, data.d + 1, data.f));
          }
        }
        --cnt;
      }
    }
  }
  return -1;
}

int main() {
  for (;;) {
    int input[13];
    rep(i, 13) {
      scanf("%d", &input[i]);
      if (input[i] == -1)
        return 0;
    }

    ll init = 0;
    rep(i, 13) init = (init << 4) | input[12 - i];

    int ans = bfs(init);
    if (ans == -1 || ans > 20)
      puts("NA");
    else
      printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;
const ll SAMPLE = 0x0ba9876543210ll;

int bfs(ll init) {
  if (init == SAMPLE)
    return 0;

  const int move[13][5] = {
      {2, -1}, {2, 5, -1},       {0, 1, 3, 6, -1},   {2, 7, -1},
      {5, -1}, {1, 4, 6, 9, -1}, {2, 5, 7, 10, -1},  {3, 6, 8, 11, -1},
      {7, -1}, {5, 10, -1},      {6, 9, 11, 12, -1}, {7, 10, -1},
      {10, -1}};

  set<ll> visited;
  queue<pair<ll, int>> Q;
  Q.push(make_pair(init, 0));
  visited.insert(init);
  while (!Q.empty()) {
    pair<ll, int> data = Q.front();
    Q.pop();

    for (int i = 0, cnt = 2; cnt; ++i) {
      if (((data.first >> (i * 4)) & 0xfll) == 0) {
        for (int j = 0; move[i][j] != -1; ++j) {
          ll next = data.first;
          next &= ~((0xfll << (i * 4)) | (0xfll << (move[i][j] * 4)));
          next |= ((data.first >> (move[i][j] * 4)) & 0xfll) << (i * 4);
          if (next == SAMPLE)
            return data.second + 1;

          if (visited.find(next) == visited.end()) {
            visited.insert(next);
            if (data.second < 19)
              Q.push(make_pair(next, data.second + 1));
          }
        }
        --cnt;
      }
    }
  }
  return -1;
}

int main() {
  for (;;) {
    int input[13];
    rep(i, 13) {
      scanf("%d", &input[i]);
      if (input[i] == -1)
        return 0;
    }

    ll init = 0;
    rep(i, 13) init = (init << 4) | input[12 - i];

    int ans = bfs(init);
    if (ans == -1)
      puts("NA");
    else
      printf("%d\\n", ans);
  }
}