Problem p00471 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define F first
#define S second
using namespace std;
int n, m;
int c;
int f[22][22];
typedef pair<int, int> P;
vector<P> v;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
bool used[33];
map<P, int> dp;
int solve(int bit, int h) {
  // cout << h << " = (" << v[h].F << "," << v[h].S << ")" << endl;
  if (bit == (1 << c) - 1) {
    if (v[h].F == v[0].F || v[h].S == v[0].S) {
      return 1;
    }
    return 0;
  }

  if (dp.find(P(bit, h)) != dp.end()) {
    // cout << h << " = (" << v[h].F << "," << v[h].S << ") : " << dp[P(bit,h)]
    // << endl;
    return dp[P(bit, h)];
  }

  int ret = 0;
  for (int i = 0; i < 4; i++) {
    int nx = v[h].F + dx[i];
    int ny = v[h].S + dy[i];
    while (0 < nx && nx <= n && 0 < ny && ny <= m) {
      if (f[nx][ny] && !used[f[nx][ny]]) {
        used[f[nx][ny]] = true;
        ret += solve(bit | (1 << f[nx][ny]), f[nx][ny]);
        used[f[nx][ny]] = false;
        break;
      }
      nx += dx[i];
      ny += dy[i];
    }
  }
  // cout << h << " : " << ret << endl;
  if (__builtin_popcount(bit) > 15)
    dp[P(bit, h)] = ret;
  return ret;
}

int main(void) {
  while (1) {
    cin >> n >> m;
    if (!n)
      break;
    c = 1;
    v.clear();
    v.push_back(P(0, 0));
    memset(f, 0, sizeof(f));
    memset(used, false, sizeof(used));
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) {
        cin >> f[j][i];
        if (f[j][i] == 2) {
          v[0].F = j;
          v[0].S = i;
          f[j][i] = 0;
        } else if (f[j][i] == 1) {
          f[j][i] = c;
          v.push_back(P(j, i));
          c++;
        }
      }
    }

    dp.clear();
    cout << solve(1, 0) << endl;
    // break;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define F first
#define S second
using namespace std;
int n, m;
int c;
int f[22][22];
typedef pair<int, int> P;
vector<P> v;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
bool used[33];
map<P, int> dp;
int solve(int bit, int h) {
  // cout << h << " = (" << v[h].F << "," << v[h].S << ")" << endl;
  if (bit == (1 << c) - 1) {
    if (v[h].F == v[0].F || v[h].S == v[0].S) {
      return 1;
    }
    return 0;
  }

  if (dp.find(P(bit, h)) != dp.end()) {
    // cout << h << " = (" << v[h].F << "," << v[h].S << ") : " << dp[P(bit,h)]
    // << endl;
    return dp[P(bit, h)];
  }

  int ret = 0;
  for (int i = 0; i < 4; i++) {
    int nx = v[h].F + dx[i];
    int ny = v[h].S + dy[i];
    while (0 < nx && nx <= n && 0 < ny && ny <= m) {
      if (f[nx][ny] && !used[f[nx][ny]]) {
        used[f[nx][ny]] = true;
        ret += solve(bit | (1 << f[nx][ny]), f[nx][ny]);
        used[f[nx][ny]] = false;
        break;
      }
      nx += dx[i];
      ny += dy[i];
    }
  }
  // cout << h << " : " << ret << endl;
  if (__builtin_popcount(bit) > 18)
    dp[P(bit, h)] = ret;
  return ret;
}

int main(void) {
  while (1) {
    cin >> n >> m;
    if (!n)
      break;
    c = 1;
    v.clear();
    v.push_back(P(0, 0));
    memset(f, 0, sizeof(f));
    memset(used, false, sizeof(used));
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) {
        cin >> f[j][i];
        if (f[j][i] == 2) {
          v[0].F = j;
          v[0].S = i;
          f[j][i] = 0;
        } else if (f[j][i] == 1) {
          f[j][i] = c;
          v.push_back(P(j, i));
          c++;
        }
      }
    }

    dp.clear();
    cout << solve(1, 0) << endl;
    // break;
  }
}