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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
// queue?????\\??£???????????????????????????pop?????????
queue<std::vector<int>> cubes;
vector<int> puzzle;
vector<int> tmppuzzle;
namespace my {
int top, left, right, up, down, back;
}

void clear() {
  for (int i = 0; i < 31; i++) {
    tmppuzzle[i] = puzzle[i];
  }
}

int BFS() {

  while (!cubes.empty()) {
    cubes.pop();
  }

  bool flag = true;
  puzzle[30] = 0;
  cubes.push(puzzle);

  while (1) {

    flag = true;
    puzzle = cubes.front();
    cubes.pop();
    if (puzzle[30] == 8) {
      return 8;
    }
    my::top = puzzle[0];
    my::left = puzzle[15];
    my::right = puzzle[12];
    my::up = puzzle[18];
    my::down = puzzle[9];
    my::back = puzzle[21];

    if (flag) {
      for (int i = 1; i < 9; i++)
        if (my::top != puzzle[i]) {
          flag = false;
          break;
        }
    }

    if (flag) {
      for (int i = 16; i < 18; i++) {
        if (my::left != puzzle[i]) {
          flag = false;
          break;
        }
      }
    }

    if (flag) {
      for (int i = 13; i < 15; i++) {
        if (my::right != puzzle[i]) {
          flag = false;
          break;
        }
      }
    }

    if (flag) {
      for (int i = 19; i < 21; i++) {
        if (my::up != puzzle[i]) {
          flag = false;
          break;
        }
      }
    }

    if (flag) {
      for (int i = 9; i < 12; i++) {
        if (my::down != puzzle[i]) {
          flag = false;
          break;
        }
      }
    }

    if (flag) {
      for (int i = 22; i < 30; i++) {
        if (my::back != puzzle[i]) {
          flag = false;
          break;
        }
      }
    }

    if (flag) {
      return puzzle[30];
    }

    puzzle[30] += 1;
    clear();
    swap(tmppuzzle[17], tmppuzzle[12]);
    swap(tmppuzzle[7], tmppuzzle[22]);
    swap(tmppuzzle[6], tmppuzzle[21]);
    swap(tmppuzzle[8], tmppuzzle[23]);
    swap(tmppuzzle[9], tmppuzzle[11]); //??´??¢
    cubes.push(tmppuzzle);

    clear();
    swap(tmppuzzle[0], tmppuzzle[27]);
    swap(tmppuzzle[1], tmppuzzle[28]);
    swap(tmppuzzle[2], tmppuzzle[29]);
    swap(tmppuzzle[14], tmppuzzle[15]);
    swap(tmppuzzle[18], tmppuzzle[20]); //??´??¢
    cubes.push(tmppuzzle);

    clear();
    swap(tmppuzzle[0], tmppuzzle[23]);
    swap(tmppuzzle[3], tmppuzzle[26]);
    swap(tmppuzzle[6], tmppuzzle[29]);
    swap(tmppuzzle[9], tmppuzzle[20]);
    swap(tmppuzzle[15], tmppuzzle[17]); //??´??¢
    cubes.push(tmppuzzle);

    clear();
    swap(tmppuzzle[2], tmppuzzle[21]);
    swap(tmppuzzle[5], tmppuzzle[24]);
    swap(tmppuzzle[8], tmppuzzle[27]);
    swap(tmppuzzle[11], tmppuzzle[18]);
    swap(tmppuzzle[12], tmppuzzle[14]); //??´??¢
    cubes.push(tmppuzzle);
  }

  return -1;
}

int main() {

  int n;
  cin >> n;

  puzzle.resize(31);
  tmppuzzle.resize(31);

  for (int i = 0; i < n; i++) {
    for (int i = 0; i < 30; i++) {
      cin >> puzzle[i];
    }
    cout << BFS() << endl;
  }

  return 0;
}

Error: -6
free(): double free detected in tcache 2


Fixed submission:

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

queue<int *> cubes;
int *puzzle;
int *tmppuzzle;
namespace my {
int top, left, right, up, down, back;
}

void clear() {
  for (int i = 0; i < 30; i++) {
    tmppuzzle[i] = puzzle[i];
  }
}

int BFS() {
  puzzle[30] = 0;
  cubes.push(puzzle);

  while (!cubes.empty()) {
    puzzle = cubes.front();
    cubes.pop();
    for (int i = 0; i < 9; i++) {
      my::top += puzzle[i];
    }
    for (int i = 0; i < 3; i++) {
      my::left += puzzle[15 + i];
    }
    for (int i = 0; i < 3; i++) {
      my::right += puzzle[12 + i];
    }
    for (int i = 0; i < 3; i++) {
      my::up += puzzle[18 + i];
    }
    for (int i = 0; i < 3; i++) {
      my::down += puzzle[9 + i];
    }
    for (int i = 0; i < 9; i++) {
      my::back += puzzle[21 + i];
    }
    if ((my::top % 9 == 0) && (my::left % 3 == 0) && (my::right % 3 == 0) &&
        (my::up % 3 == 0) && (my::down % 3 == 0) && (my::back % 9 == 0)) {
      return puzzle[30];
    }
    if (puzzle[30] == 8) {
      continue;
    }
    puzzle[30] += 1;
    clear();
    swap(tmppuzzle[17], tmppuzzle[12]);
    swap(tmppuzzle[7], tmppuzzle[22]);
    swap(tmppuzzle[6], tmppuzzle[21]);
    swap(tmppuzzle[8], tmppuzzle[23]);
    cubes.push(tmppuzzle);

    clear();
    swap(tmppuzzle[0], tmppuzzle[27]);
    swap(tmppuzzle[1], tmppuzzle[28]);
    swap(tmppuzzle[2], tmppuzzle[29]);
    swap(tmppuzzle[14], tmppuzzle[15]);
    cubes.push(tmppuzzle);

    clear();
    swap(tmppuzzle[0], tmppuzzle[23]);
    swap(tmppuzzle[3], tmppuzzle[26]);
    swap(tmppuzzle[6], tmppuzzle[29]);
    swap(tmppuzzle[9], tmppuzzle[20]);
    cubes.push(tmppuzzle);

    clear();
    swap(tmppuzzle[2], tmppuzzle[21]);
    swap(tmppuzzle[5], tmppuzzle[24]);
    swap(tmppuzzle[8], tmppuzzle[27]);
    swap(tmppuzzle[11], tmppuzzle[18]);
    cubes.push(tmppuzzle);
  }

  return -1;
}

int main() {

  int n;
  cin >> n;

  puzzle = new int[33];
  tmppuzzle = new int[33];

  for (int i = 0; i < n; i++) {
    for (int i = 0; i < 30; i++) {
      cin >> puzzle[i];
    }
    puzzle[30] = 0;
    cout << BFS() << endl;
  }
  delete[] puzzle;
  delete[] tmppuzzle;
  return 0;
}