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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;
map<vector<vector<int>>, int> memo;
vector<vector<int>> goal(4, vector<int>(8, 0));

int solve(vector<vector<int>> &card) {
  if (card == goal)
    return 0;
  if (memo.find(card) != memo.end())
    return memo[card];

  int ret = INF;

  for (int i = 0; i < 4; ++i) {
    for (int j = 1; j < 8; ++j) {
      if (card[i][j] != 0)
        continue;
      for (int k = 0; k < 4; ++k) {
        for (int l = 1; l < 8; ++l) {
          if (card[i][j - 1] + 1 == card[k][l]) {
            swap(card[i][j], card[k][l]);
            ret = min(ret, solve(card) + 1);
            swap(card[i][j], card[k][l]);
          }
        }
      }
    }
  }

  return memo[card] = ret;
}

int main() {
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 7; ++j) {
      goal[i][j] = 11 + i * 10 + j;
    }
  }

  int d;
  cin >> d;

  while (--d >= 0) {
    vector<vector<int>> card(4, vector<int>(8));
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j < 8; ++j) {
        cin >> card[i][j];
        if (card[i][j] % 10 == 1)
          card[i][j] = 0;
      }
    }
    for (int i = 0; i < 4; ++i)
      card[i][0] = 11 + 10 * i;

    memo.clear();
    int ret = solve(card);
    if (ret == INF)
      cout << -1 << endl;
    else
      cout << ret << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;
map<vector<vector<int>>, int> memo;
vector<vector<int>> goal(4, vector<int>(8, 0));

int solve(vector<vector<int>> &card) {
  if (card == goal)
    return 0;
  if (memo.find(card) != memo.end())
    return memo[card];

  int ret = INF;

  for (int i = 0; i < 4; ++i) {
    for (int j = 1; j < 8; ++j) {
      if (card[i][j] != 0)
        continue;
      for (int k = 0; k < 4; ++k) {
        for (int l = 1; l < 8; ++l) {
          if (card[i][j - 1] + 1 == card[k][l]) {
            swap(card[i][j], card[k][l]);
            ret = min(ret, solve(card) + 1);
            swap(card[i][j], card[k][l]);
          }
        }
      }
    }
  }

  return memo[card] = ret;
}

int main() {
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 7; ++j) {
      goal[i][j] = 11 + i * 10 + j;
    }
  }

  int d;
  cin >> d;

  while (--d >= 0) {
    vector<vector<int>> card(4, vector<int>(8));
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j < 8; ++j) {
        cin >> card[i][j];
        if (card[i][j] % 10 == 1)
          card[i][j] = 0;
      }
    }
    for (int i = 0; i < 4; ++i)
      card[i][0] = 11 + 10 * i;

    int ret = solve(card);
    if (ret == INF)
      cout << -1 << endl;
    else
      cout << ret << endl;
  }

  return 0;
}