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

// Bokann ga bokka--nn!!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
map<P, int> ma;
int f[15][15];
int linkk[15][15];
// int dp[(1<<23)][24]={};
P za[25];
int n, m;
int val;
int calc(int mask, int cur) {
  int ret = 0;
  // if(dp[mask][cur]) return dp[mask][cur];
  int x = za[cur].first;
  int y = za[cur].second;
  if (mask == (1 << val) - 1) {
    if (x == za[23].first || y == za[23].second)
      return 1;
    else
      return 0;
  }
  for (int i = x - 1; i >= 0; i--) {
    int s = linkk[i][y];
    if (f[i][y] == 1 && !((mask >> s) & 1)) {
      ret += calc(mask | (1 << s), s);
      break;
    }
  }
  for (int i = x + 1; i < m; i++) {
    int s = linkk[i][y];
    if (f[i][y] == 1 && !((mask >> s) & 1)) {
      ret += calc(mask | (1 << s), s);
      break;
    }
  }

  for (int i = y - 1; i >= 0; i--) {
    int s = linkk[x][i];
    if (f[x][i] == 1 && !((mask >> s) & 1)) {
      ret += calc(mask | (1 << s), s);
      break;
    }
  }
  for (int i = y + 1; i < n; i++) {
    int s = linkk[x][i];
    if (f[x][i] == 1 && !((mask >> s) & 1)) {
      ret += calc(mask | (1 << s), s);
      break;
    }
  }
  // if(ret) dp[mask][cur]=ret;
  return ret;
}
int main() {
  while (1) {
    cin >> n >> m;
    //	ma.clear();
    if (!n)
      return 0;
    int num = 0;
    val = 0;
    for (int i = 0; i < 15; i++)
      for (int j = 0; j < 15; j++) {
        f[i][j] = INF;
        linkk[i][j] = -1;
      }

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cin >> f[i][j];
        if (f[i][j] == 1) {
          linkk[i][j] = num;
          za[num++] = mp(i, j);
          val++;
        }
        if (f[i][j] == 2)
          za[23] = mp(i, j);
      }
    }
    cout << calc(0, 23) << endl;
  }
}

Error: MLE

Fixed submission:

// Bokann ga bokka--nn!!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
map<P, int> ma;
int f[15][15];
int linkk[15][15];
P za[25];
int n, m;
int val;
int dp(int mask, int cur) {
  int ret = 0;
  if (ma[mp(mask, cur)])
    return ma[mp(mask, cur)];
  int x = za[cur].first;
  int y = za[cur].second;
  if (mask == (1 << val) - 1) {
    if (x == za[23].first || y == za[23].second)
      return 1;
    else
      return 0;
  }
  for (int i = x - 1; i >= 0; i--) {
    int s = linkk[i][y];
    if (f[i][y] == 1 && !((mask >> s) & 1)) {
      ret += dp(mask | (1 << s), s);
      break;
    }
  }
  for (int i = x + 1; i < m; i++) {
    int s = linkk[i][y];
    if (f[i][y] == 1 && !((mask >> s) & 1)) {
      ret += dp(mask | (1 << s), s);
      break;
    }
  }

  for (int i = y - 1; i >= 0; i--) {
    int s = linkk[x][i];
    if (f[x][i] == 1 && !((mask >> s) & 1)) {
      ret += dp(mask | (1 << s), s);
      break;
    }
  }
  for (int i = y + 1; i < n; i++) {
    int s = linkk[x][i];
    if (f[x][i] == 1 && !((mask >> s) & 1)) {
      ret += dp(mask | (1 << s), s);
      break;
    }
  }
  if (ret)
    ma[mp(mask, cur)] = ret;
  return ret;
}
int main() {
  while (1) {
    cin >> n >> m;
    ma.clear();
    if (!n)
      return 0;
    int num = 0;
    val = 0;
    for (int i = 0; i < 15; i++)
      for (int j = 0; j < 15; j++) {
        f[i][j] = INF;
        linkk[i][j] = -1;
      }

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cin >> f[i][j];
        if (f[i][j] == 1) {
          linkk[i][j] = num;
          za[num++] = mp(i, j);
          val++;
        }
        if (f[i][j] == 2)
          za[23] = mp(i, j);
      }
    }
    cout << dp(0, 23) << endl;
  }
}