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

#include <iostream>

bool is_complete(int *);

int solve(int *, int);
void rot(int *, int);

int min(int *);
void swap(int &, int &);

int main() {
  int N;
  int **p;
  std::cin >> N;
  p = new int *[N];
  for (int i = 0; i < N; i++) {
    p[i] = new int[30];
    for (int j = 0; j < 30; j++)
      std::cin >> p[i][j];
  }
  for (int i = 0; i < N; i++)
    std::cout << solve(p[i], 0) << std::endl;
}

bool is_complete(int *p) {
  int cmp = p[0];
  for (int i = 0; i < 30; i++) {
    if (i == 9)
      cmp = p[9];
    else if (i == 12)
      cmp = p[12];
    else if (i == 15)
      cmp = p[15];
    else if (i == 18)
      cmp = p[18];
    else if (i == 21)
      cmp = p[21];
    else if (cmp != p[i])
      return false;
  }
  return true;
}

int solve(int *p, int cnt) {
  if (cnt == 9 || is_complete(p))
    return cnt;
  int ans[4];
  for (int i = 0; i < 4; i++) {
    rot(p, i);
    if (is_complete(p))
      ans[i] = cnt + 1;
    else
      ans[i] = solve(p, cnt + 1);
    rot(p, i);
  }
  return min(ans);
}

void rot(int *p, int dir) {
  if (dir == 0) {
    swap(p[6], p[21]);
    swap(p[7], p[22]);
    swap(p[8], p[23]);
    swap(p[9], p[11]);
    swap(p[12], p[17]);
  } else if (dir == 1) {
    swap(p[0], p[27]);
    swap(p[1], p[28]);
    swap(p[2], p[29]);
    swap(p[18], p[20]);
    swap(p[14], p[15]);
  } else if (dir == 2) {
    swap(p[0], p[23]);
    swap(p[3], p[26]);
    swap(p[6], p[29]);
    swap(p[15], p[17]);
    swap(p[9], p[20]);
  } else {
    swap(p[2], p[21]);
    swap(p[5], p[24]);
    swap(p[8], p[27]);
    swap(p[12], p[14]);
    swap(p[11], p[18]);
  }
}

int min(int *a) {
  int ans = a[0];
  for (int i = 1; i < 4; i++)
    if (ans > a[i])
      ans = a[i];
  return ans;
}

void swap(int &a, int &b) {
  int tmp = a;
  a = b;
  b = tmp;
}

Error: MLE

Fixed submission:

#include <iostream>

bool is_complete(int *);

int rot(int *, int, int);

int min(int, int, int, int);
void swap(int &, int &);

int main() {
  int N;
  int **p;
  std::cin >> N;
  p = new int *[N];
  for (int i = 0; i < N; i++) {
    p[i] = new int[30];
    for (int j = 0; j < 30; j++)
      std::cin >> p[i][j];
  }
  for (int i = 0; i < N; i++)
    std::cout << min(rot(p[i], 1, 0), rot(p[i], 2, 0), rot(p[i], 3, 0),
                     rot(p[i], 4, 0))
              << std::endl;
}

bool is_complete(int *p) {
  int cmp = p[0];
  for (int i = 0; i < 30; i++) {
    if (i == 9)
      cmp = p[9];
    else if (i == 12)
      cmp = p[12];
    else if (i == 15)
      cmp = p[15];
    else if (i == 18)
      cmp = p[18];
    else if (i == 21)
      cmp = p[21];
    else if (cmp != p[i])
      return false;
  }
  return true;
}

int rot(int *p, int dir, int cnt) {
  if (cnt == 9 || is_complete(p))
    return cnt;
  int *pp = new int[30];
  for (int i = 0; i < 30; i++)
    pp[i] = p[i];
  if (dir == 1) {
    swap(pp[6], pp[21]);
    swap(pp[7], pp[22]);
    swap(pp[8], pp[23]);
    swap(pp[9], pp[11]);
    swap(pp[12], pp[17]);
  } else if (dir == 2) {
    swap(pp[0], pp[27]);
    swap(pp[1], pp[28]);
    swap(pp[2], pp[29]);
    swap(pp[18], pp[20]);
    swap(pp[14], pp[15]);
  } else if (dir == 3) {
    swap(pp[0], pp[23]);
    swap(pp[3], pp[26]);
    swap(pp[6], pp[29]);
    swap(pp[15], pp[17]);
    swap(pp[9], pp[20]);
  } else {
    swap(pp[2], pp[21]);
    swap(pp[5], pp[24]);
    swap(pp[8], pp[27]);
    swap(pp[12], pp[14]);
    swap(pp[11], pp[18]);
  }
  return min(rot(pp, 1, cnt + 1), rot(pp, 2, cnt + 1), rot(pp, 3, cnt + 1),
             rot(pp, 4, cnt + 1));
}

int min(int a, int b, int c, int d) {
  int ans = a;
  if (ans > b)
    ans = b;
  if (ans > c)
    ans = c;
  if (ans > d)
    ans = d;
  return ans;
}

void swap(int &a, int &b) {
  int tmp = a;
  a = b;
  b = tmp;
}