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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int, int> P;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool f[8][8][8][8][8][8][8][8];
short cnt[8][8][8][8][8][8][8][8];
int r[2][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}};
struct puzzle {
  int card[2][4];
  int cnt;
};

int main() {
  puzzle sp;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 4; ++j) {
      sp.card[i][j] = r[i][j];
    }
  }
  memset(f, false, sizeof(f));
  memset(cnt, 0, sizeof(cnt));
  sp.cnt = 0;
  queue<puzzle> que;
  que.push(sp);
  while (que.size()) {
    puzzle pz = que.front();
    que.pop();
    cnt[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]]
       [pz.card[1][0]][pz.card[1][1]][pz.card[1][2]][pz.card[1][3]] = pz.cnt;
    f[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]][pz.card[1][0]]
     [pz.card[1][1]][pz.card[1][2]][pz.card[1][3]] = true;
    for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 4; ++j) {
        if (pz.card[i][j] == 0) {
          if (0 <= i - 1) {
            puzzle np = pz;
            np.cnt = pz.cnt + 1;
            swap(np.card[i - 1][j], np.card[i][j]);
            if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]]
                  [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])
              que.push(np);
          }
          if (i + 1 < 2) {
            puzzle np = pz;
            np.cnt = pz.cnt + 1;
            swap(np.card[i + 1][j], np.card[i][j]);
            if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]]
                  [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])
              que.push(np);
          }
          if (0 <= j - 1) {
            puzzle np = pz;
            np.cnt = pz.cnt + 1;
            swap(np.card[i][j - 1], np.card[i][j]);
            if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]]
                  [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])
              que.push(np);
          }
          if (j + 1 < 4) {
            puzzle np = pz;
            np.cnt = pz.cnt + 1;
            swap(np.card[i][j + 1], np.card[i][j]);
            if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]]
                  [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])
              que.push(np);
          }
        }
      }
    }
  }
  int n[8];
  int ans;
  while (cin >> n[0] >> n[1] >> n[2] >> n[3] >> n[4] >> n[5] >> n[6] >> n[7]) {
    ans = cnt[n[0]][n[1]][n[2]][n[3]][n[4]][n[5]][n[6]][n[7]];
    cout << ans << "\\n";
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int, int> P;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool f[8][8][8][8][8][8][8][8];
int cnt[8][8][8][8][8][8][8][8];
int r[2][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}};
struct puzzle {
  int card[2][4];
  int cnt;
};

int main() {
  puzzle sp;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 4; ++j) {
      sp.card[i][j] = r[i][j];
    }
  }
  memset(f, false, sizeof(f));
  memset(cnt, 0, sizeof(cnt));
  sp.cnt = 0;
  queue<puzzle> que;
  que.push(sp);
  while (que.size()) {
    puzzle pz = que.front();
    que.pop();
    cnt[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]]
       [pz.card[1][0]][pz.card[1][1]][pz.card[1][2]][pz.card[1][3]] = pz.cnt;
    f[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]][pz.card[1][0]]
     [pz.card[1][1]][pz.card[1][2]][pz.card[1][3]] = true;
    for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 4; ++j) {
        if (pz.card[i][j] == 0) {
          if (0 <= i - 1) {
            puzzle np = pz;
            np.cnt = pz.cnt + 1;
            swap(np.card[i - 1][j], np.card[i][j]);
            if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]]
                  [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])
              que.push(np);
          }
          if (i + 1 < 2) {
            puzzle np = pz;
            np.cnt = pz.cnt + 1;
            swap(np.card[i + 1][j], np.card[i][j]);
            if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]]
                  [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])
              que.push(np);
          }
          if (0 <= j - 1) {
            puzzle np = pz;
            np.cnt = pz.cnt + 1;
            swap(np.card[i][j - 1], np.card[i][j]);
            if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]]
                  [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])
              que.push(np);
          }
          if (j + 1 < 4) {
            puzzle np = pz;
            np.cnt = pz.cnt + 1;
            swap(np.card[i][j + 1], np.card[i][j]);
            if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]]
                  [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]])
              que.push(np);
          }
        }
      }
    }
  }
  int n[8];
  int ans;
  while (cin >> n[0] >> n[1] >> n[2] >> n[3] >> n[4] >> n[5] >> n[6] >> n[7]) {
    ans = cnt[n[0]][n[1]][n[2]][n[3]][n[4]][n[5]][n[6]][n[7]];
    cout << ans << "\\n";
  }
  return 0;
}