Problem p00824 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
map<vector<vector<int>>, int> mp;

vector<vector<int>> goal;

int getans(const vector<vector<int>> &field) {
  if (field == goal)
    return 0;
  auto it = mp.find(field);
  if (it != mp.end())
    return it->second;
  else {
    int ans = 1e5;
    vector<vector<int>> nfield(field);
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j < 8; ++j) {
        if (!field[i][j]) {
          int from = field[i][j - 1];
          for (int k = 0; k < 4; ++k) {
            for (int l = 0; l < 8; ++l) {
              if (nfield[k][l] == from + 1) {
                swap(nfield[i][j], nfield[k][l]);
                ans = min(getans(nfield) + 1, ans);
                swap(nfield[i][j], nfield[k][l]);
              }
            }
          }
        }
      }
    }
    return mp[field] = ans;
  }
}
int main() {
  int N;
  cin >> N;
  while (N--) {
    vector<vector<int>> field(4, vector<int>(8));
    goal = field;
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 7; ++j) {
        goal[i][j] = 10 * (i + 1) + j + 1;
      }
    }
    for (int i = 0; i < 4; ++i) {
      field[i][0] = i * 10 + 11;
      for (int j = 0; j < 7; ++j) {
        int a;
        cin >> a;
        if (a % 10 != 1) {
          field[i][j + 1] = a;
        }
      }
    }
    int ans = getans(field);
    if (ans > 9999)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
map<vector<vector<int>>, int> mp;

vector<vector<int>> goal;

int getans(const vector<vector<int>> &field) {
  if (field == goal)
    return 0;
  auto it = mp.find(field);
  if (it != mp.end())
    return it->second;
  else {
    int ans = 1e5;
    vector<vector<int>> nfield(field);
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j < 8; ++j) {
        if (!field[i][j]) {
          int from = field[i][j - 1];
          for (int k = 0; k < 4; ++k) {
            for (int l = 0; l < 8; ++l) {
              if (nfield[k][l] == from + 1) {
                swap(nfield[i][j], nfield[k][l]);
                ans = min(getans(nfield) + 1, ans);
                swap(nfield[i][j], nfield[k][l]);
              }
            }
          }
        }
      }
    }
    return mp[field] = ans;
  }
}
int main() {
  int N;
  cin >> N;
  while (N--) {
    mp.clear();
    vector<vector<int>> field(4, vector<int>(8));
    goal = field;
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 7; ++j) {
        goal[i][j] = 10 * (i + 1) + j + 1;
      }
    }
    for (int i = 0; i < 4; ++i) {
      field[i][0] = i * 10 + 11;
      for (int j = 0; j < 7; ++j) {
        int a;
        cin >> a;
        if (a % 10 != 1) {
          field[i][j + 1] = a;
        }
      }
    }
    int ans = getans(field);
    if (ans > 9999)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}