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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

inline bool ok(vector<int> &v) {
  rep(i, 1, 9) if (v[i] != v[i - 1]) return false;
  rep(i, 10, 12) if (v[i] != v[i - 1]) return false;
  rep(i, 13, 15) if (v[i] != v[i - 1]) return false;
  rep(i, 16, 18) if (v[i] != v[i - 1]) return false;
  rep(i, 19, 21) if (v[i] != v[i - 1]) return false;
  rep(i, 22, 30) if (v[i] != v[i - 1]) return false;
  return true;
}

inline vector<int> rotate1(vector<int> v) {
  swap(v[0], v[27]);
  swap(v[1], v[28]);
  swap(v[2], v[29]);

  swap(v[14], v[15]);

  swap(v[20], v[18]);
  return move(v);
}

inline vector<int> rotate2(vector<int> v) {
  swap(v[0], v[23]);
  swap(v[3], v[26]);
  swap(v[6], v[29]);

  swap(v[20], v[9]);

  swap(v[15], v[17]);
  return move(v);
}

inline vector<int> rotate3(vector<int> v) {
  swap(v[6], v[21]);
  swap(v[7], v[22]);
  swap(v[8], v[23]);

  swap(v[12], v[17]);

  swap(v[9], v[11]);
  return move(v);
}

inline vector<int> rotate4(vector<int> v) {
  swap(v[2], v[21]);
  swap(v[5], v[24]);
  swap(v[8], v[27]);

  swap(v[11], v[18]);

  swap(v[12], v[14]);
  return move(v);
}

void solve_() {
  vector<int> p(30);
  for (auto &x : p)
    cin >> x;

  queue<pair<vector<int>, int>> que;
  que.push(make_pair(p, 0));
  while (true) {
    vector<int> v = que.front().first;
    int d = que.front().second;
    que.pop();
    if (ok(v)) {
      cout << d << endl;
      return;
    }

    // assert(d<8);

    // assert(v==rotate1(rotate1(v)));
    // assert(v==rotate2(rotate2(v)));
    // assert(v==rotate3(rotate3(v)));
    // assert(v==rotate4(rotate4(v)));

    que.push(make_pair(rotate1(v), d + 1));
    que.push(make_pair(rotate2(v), d + 1));
    que.push(make_pair(rotate3(v), d + 1));
    que.push(make_pair(rotate4(v), d + 1));
  }
}

void solve() {
  int n;
  cin >> n;
  rep(i, 0, n) solve_();
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

inline bool ok(vector<int> &v) {
  rep(i, 1, 9) if (v[i] != v[i - 1]) return false;
  rep(i, 10, 12) if (v[i] != v[i - 1]) return false;
  rep(i, 13, 15) if (v[i] != v[i - 1]) return false;
  rep(i, 16, 18) if (v[i] != v[i - 1]) return false;
  rep(i, 19, 21) if (v[i] != v[i - 1]) return false;
  rep(i, 22, 30) if (v[i] != v[i - 1]) return false;
  return true;
}

inline vector<int> rotate1(vector<int> v) {
  swap(v[0], v[27]);
  swap(v[1], v[28]);
  swap(v[2], v[29]);

  swap(v[14], v[15]);

  swap(v[20], v[18]);
  return move(v);
}

inline vector<int> rotate2(vector<int> v) {
  swap(v[0], v[23]);
  swap(v[3], v[26]);
  swap(v[6], v[29]);

  swap(v[20], v[9]);

  swap(v[15], v[17]);
  return move(v);
}

inline vector<int> rotate3(vector<int> v) {
  swap(v[6], v[21]);
  swap(v[7], v[22]);
  swap(v[8], v[23]);

  swap(v[12], v[17]);

  swap(v[9], v[11]);
  return move(v);
}

inline vector<int> rotate4(vector<int> v) {
  swap(v[2], v[21]);
  swap(v[5], v[24]);
  swap(v[8], v[27]);

  swap(v[11], v[18]);

  swap(v[12], v[14]);
  return move(v);
}

void solve_() {
  vector<int> p(30);
  for (auto &x : p)
    cin >> x;

  queue<pair<vector<int>, int>> que;
  que.push(make_pair(p, 0));
  while (true) {
    vector<int> v = que.front().first;
    int d = que.front().second;
    que.pop();
    if (ok(v)) {
      cout << d << endl;
      return;
    }

    assert(d < 8);

    assert(v == rotate1(rotate1(v)));
    assert(v == rotate2(rotate2(v)));
    assert(v == rotate3(rotate3(v)));
    assert(v == rotate4(rotate4(v)));

    que.push(make_pair(rotate1(v), d + 1));
    que.push(make_pair(rotate2(v), d + 1));
    que.push(make_pair(rotate3(v), d + 1));
    que.push(make_pair(rotate4(v), d + 1));
  }
}

void solve() {
  int n;
  cin >> n;
  rep(i, 0, n) solve_();
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  solve();
  return 0;
}