Problem p00295 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

class FC {
public:
  int p[31];
  int n;
  bool is_over() {
    return p[1] == p[2] && p[2] == p[3] && p[3] == p[4] && p[4] == p[5] &&
           p[5] == p[6] && p[6] == p[7] && p[7] == p[8] && p[8] == p[9] &&
           p[10] == p[11] && p[11] == p[12] && p[13] == p[14] &&
           p[14] == p[15] && p[16] == p[17] && p[17] == p[18] &&
           p[19] == p[20] && p[20] == p[21] && p[22] == p[23] &&
           p[23] == p[24] && p[24] == p[25] && p[25] == p[26] &&
           p[26] == p[27] && p[27] == p[28] && p[28] == p[29] && p[29] == p[30];
  }
  void rot1() {
    swap(p[7], p[22]);
    swap(p[8], p[23]);
    swap(p[9], p[24]);
    swap(p[13], p[18]);
    swap(p[10], p[12]);
  }
  void rot2() {
    swap(p[1], p[28]);
    swap(p[2], p[29]);
    swap(p[3], p[30]);
    swap(p[15], p[16]);
    swap(p[21], p[19]);
  }
  void rot3() {
    swap(p[1], p[24]);
    swap(p[4], p[27]);
    swap(p[7], p[30]);
    swap(p[21], p[10]);
    swap(p[16], p[18]);
  }
  void rot4() {
    swap(p[3], p[22]);
    swap(p[6], p[25]);
    swap(p[9], p[28]);
    swap(p[13], p[15]);
    swap(p[19], p[12]);
  }
};

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    FC now;
    for (int j = 1; j <= 30; j++)
      cin >> now.p[j];
    now.n = 0;
    queue<FC> que;
    que.push(now);
    while (!que.empty()) {
      now = que.front();
      que.pop();
      if (now.is_over()) {
        cout << now.n << endl;
        break;
      }
      FC nex1 = now;
      nex1.rot1();
      nex1.n++;
      que.push(nex1);
      FC nex2 = now;
      nex2.rot2();
      nex2.n++;
      que.push(nex2);
      FC nex3 = now;
      nex3.rot3();
      nex3.n++;
      que.push(nex3);
      FC nex4 = now;
      nex4.rot4();
      nex4.n++;
      que.push(nex4);
    }
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

class FC {
public:
  int p[31];
  int n;
  bool is_over() {
    return p[1] == p[2] && p[2] == p[3] && p[3] == p[4] && p[4] == p[5] &&
           p[5] == p[6] && p[6] == p[7] && p[7] == p[8] && p[8] == p[9] &&
           p[10] == p[11] && p[11] == p[12] && p[13] == p[14] &&
           p[14] == p[15] && p[16] == p[17] && p[17] == p[18] &&
           p[19] == p[20] && p[20] == p[21] && p[22] == p[23] &&
           p[23] == p[24] && p[24] == p[25] && p[25] == p[26] &&
           p[26] == p[27] && p[27] == p[28] && p[28] == p[29] && p[29] == p[30];
  }
  void rot1() {
    swap(p[7], p[22]);
    swap(p[8], p[23]);
    swap(p[9], p[24]);
    swap(p[13], p[18]);
    swap(p[10], p[12]);
  }
  void rot2() {
    swap(p[1], p[28]);
    swap(p[2], p[29]);
    swap(p[3], p[30]);
    swap(p[15], p[16]);
    swap(p[21], p[19]);
  }
  void rot3() {
    swap(p[1], p[24]);
    swap(p[4], p[27]);
    swap(p[7], p[30]);
    swap(p[21], p[10]);
    swap(p[16], p[18]);
  }
  void rot4() {
    swap(p[3], p[22]);
    swap(p[6], p[25]);
    swap(p[9], p[28]);
    swap(p[13], p[15]);
    swap(p[19], p[12]);
  }
};

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    FC now;
    for (int j = 1; j <= 30; j++)
      cin >> now.p[j];
    now.n = 0;
    queue<FC> que;
    que.push(now);
    while (!que.empty()) {
      now = que.front();
      que.pop();
      if (now.is_over()) {
        cout << now.n << endl;
        break;
      }
      FC nex1 = now;
      nex1.rot1();
      nex1.n++;
      que.push(nex1);
      FC nex2 = now;
      nex2.rot1();
      nex2.n++;
      que.push(nex2);
      FC nex3 = now;
      nex3.rot1();
      nex3.n++;
      que.push(nex3);
      FC nex4 = now;
      nex4.rot1();
      nex4.n++;
      que.push(nex4);
    }
  }
  return 0;
}