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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_D 3
#define MAX_N 9
#define MAX_C 362880
int fact1[MAX_N][MAX_N][MAX_N][MAX_N][MAX_N][MAX_N][MAX_N][2];
vector<int> fact2[MAX_C];
int MAP[MAX_D][MAX_D];
int DP[MAX_C], K;
void FACT() {
  int a[MAX_N];
  for (int i = 0; i < MAX_N; i++) {
    a[i] = i;
  }
  int cnt = 0;
  do {
    if (a[MAX_N - 2] < a[MAX_N - 1]) {
      fact1[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][0] = cnt;
    } else {
      fact1[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][1] = cnt;
    }
    vector<int> vec;
    for (int i = 0; i < MAX_N; i++) {
      vec.push_back(a[i]);
    }
    fact2[cnt] = vec;
    cnt++;
  } while (next_permutation(a, a + MAX_N));
}
int main() {
  for (int i = 0; i < MAX_D; i++) {
    for (int j = 0; j < MAX_D; j++) {
      cin >> MAP[i][j];
    }
  }
  FACT();
  for (int i = 0; i < MAX_C; i++) {
    DP[i] = MAX_C;
  }
  int T = fact1[1][2][3][4][5][6][7][1];
  if (MAP[2][1] < MAP[2][2]) {
    K = fact1[MAP[0][0]][MAP[0][1]][MAP[0][2]][MAP[1][0]][MAP[1][1]][MAP[1][2]]
             [MAP[2][0]][0];
  } else {
    K = fact1[MAP[0][0]][MAP[0][1]][MAP[0][2]][MAP[1][0]][MAP[1][1]][MAP[1][2]]
             [MAP[2][0]][1];
  }
  DP[K] = 0;
  while (DP[T] == MAX_C) {
    for (int i = 0; i < MAX_C; i++) {
      if (DP[i] >= MAX_C) {
        continue;
      }
      vector<int> X = fact2[i];
      int y[MAX_D][MAX_D], z[MAX_D][MAX_D], cx, cy;
      int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
      for (int j = 0; j < X.size(); j++) {
        y[j / 3][j % 3] = X[j];
        if (X[j] == 0) {
          cx = j / 3;
          cy = j % 3;
        }
      }
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < MAX_N; k++) {
          z[k / 3][k % 3] = y[k / 3][k % 3];
        }
        int ex = cx + dx[j], ey = cy + dy[j];
        if (ex >= 3 || ex <= -1 || ey >= 3 || ey <= -1) {
          continue;
        }
        swap(z[cx][cy], z[ex][ey]);
        if (z[2][1] < z[2][2]) {
          K = fact1[z[0][0]][z[0][1]][z[0][2]][z[1][0]][z[1][1]][z[1][2]]
                   [z[2][0]][0];
        } else {
          K = fact1[z[0][0]][z[0][1]][z[0][2]][z[1][0]][z[1][1]][z[1][2]]
                   [z[2][0]][1];
        }
        DP[K] = min(DP[K], DP[i] + 1);
      }
    }
  }
  cout << DP[T] << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

queue<char> Q[3][3];
queue<char> R[3][3];

int main() {
  int a, b, c, d, e, sum = 0;
  int x[3][3];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a;
      Q[i][j].push(a);
    }
  }
  while (true) {
    while (!Q[0][0].empty()) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          if (Q[i][j].front() == 0) {
            b = i;
            c = j;
          }
        }
      }
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          x[i][j] = Q[i][j].front();
        }
      }
      if (x[0][0] == 1 && x[0][1] == 2 && x[0][2] == 3 && x[1][0] == 4 &&
          x[1][1] == 5 && x[1][2] == 6 && x[2][0] == 7 && x[2][1] == 8) {
        goto owata;
      }
      if (b >= 1) {
        d = x[b][c];
        e = x[b - 1][c];
        x[b][c] = e;
        x[b - 1][c] = d;
        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++) {
            R[i][j].push(x[i][j]);
          }
        }
      }
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          x[i][j] = Q[i][j].front();
        }
      }
      if (b <= 1) {
        d = x[b][c];
        e = x[b + 1][c];
        x[b][c] = e;
        x[b + 1][c] = d;
        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++) {
            R[i][j].push(x[i][j]);
          }
        }
      }
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          x[i][j] = Q[i][j].front();
        }
      }
      if (c >= 1) {
        d = x[b][c];
        e = x[b][c - 1];
        x[b][c] = e;
        x[b][c - 1] = d;
        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++) {
            R[i][j].push(x[i][j]);
          }
        }
      }
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          x[i][j] = Q[i][j].front();
        }
      }
      if (c <= 1) {
        d = x[b][c];
        e = x[b][c + 1];
        x[b][c] = e;
        x[b][c + 1] = d;
        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++) {
            R[i][j].push(x[i][j]);
          }
        }
      }
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          Q[i][j].pop();
        }
      }
    }
    while (!R[0][0].empty()) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          Q[i][j].push(R[i][j].front());
          R[i][j].pop();
        }
      }
    }
    sum++;
  }
owata:;
  cout << sum << endl;
  return 0;
}