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

// 35
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

struct S {
  vector<int> v;
  int t;
};

int main() {
  for (;;) {
    vector<int> v(13);
    cin >> v[2] >> v[1] >> v[4] >> v[7] >> v[0] >> v[3] >> v[6] >> v[9] >>
        v[12] >> v[5] >> v[8] >> v[11] >> v[10];
    if (v[2] == -1)
      return 0;
    queue<S> que;
    S is = {v, 0};
    que.push(is);
    set<vector<int>> p;
    while (!que.empty() && que.front().t <= 20) {
      S c = que.front();
      int g[] = {4, 1, 0, 5, 2, 9, 6, 3, 10, 7, 0, 11, 8};
      if (c.v == vector<int>(g, g + 13))
        break;
      que.pop();
      if (p.count(c.v))
        continue;
      int nr = 0;
      for (int i = 0; i < 13; i++) {
        nr += g[i] && c.v[i] == g[i];
      }
      if (20 - c.t < 11 - nr)
        continue;
      p.insert(c.v);
      for (int i = 0; i < 13; i++) {
        if (c.v[i] == 0) {
          for (int j = 0; j < 4; j++) {
            static int d[] = {-2, 3, 2, -3};
            static int b[][5] = {{0, 1, 2, 7, 12},
                                 {2, 7, 12, 11, 10},
                                 {12, 11, 10, 5, 0},
                                 {10, 5, 0, 1, 2}};
            int k;
            for (k = 0; k < 5; k++) {
              if (i == b[j][k])
                break;
            }
            if (k == 5) {
              vector<int> n = c.v;
              swap(n[i], n[i + d[j]]);
              S ns = {n, c.t + 1};
              if (!p.count(n)) {
                que.push(ns);
              }
            }
          }
        }
      }
    }
    if (que.empty() || que.front().t > 20) {
      cout << "NA" << endl;
    } else {
      cout << que.front().t << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

// 35
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

struct S {
  vector<int> v;
  int t;
};

int main() {
  for (;;) {
    vector<int> v(13);
    cin >> v[2] >> v[1] >> v[4] >> v[7] >> v[0] >> v[3] >> v[6] >> v[9] >>
        v[12] >> v[5] >> v[8] >> v[11] >> v[10];
    if (v[2] == -1)
      return 0;
    queue<S> que;
    S is = {v, 0};
    que.push(is);
    set<vector<int>> p;
    while (!que.empty() && que.front().t <= 20) {
      S c = que.front();
      int g[] = {4, 1, 0, 5, 2, 9, 6, 3, 10, 7, 0, 11, 8};
      if (c.v == vector<int>(g, g + 13))
        break;
      que.pop();
      if (p.count(c.v))
        continue;
      p.insert(c.v);
      for (int i = 0; i < 13; i++) {
        if (v[i] == 0) {
          for (int j = 0; j < 4; j++) {
            int d[] = {-2, 3, 2, -3};
            int b[][5] = {{0, 1, 2, 7, 12},
                          {2, 7, 12, 11, 10},
                          {12, 11, 10, 5, 0},
                          {10, 5, 0, 1, 2}};
            int k;
            for (k = 0; k < 5; k++) {
              if (i == b[j][k])
                break;
            }
            if (k == 5) {
              vector<int> n = c.v;
              swap(n[i], n[i + d[j]]);
              S ns = {n, c.t + 1};
              que.push(ns);
            }
          }
        }
      }
    }
    if (que.empty() || que.front().t > 20) {
      cout << "NA" << endl;
    } else {
      cout << que.front().t << endl;
    }
  }
  return 0;
}