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

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  int m, n;
  while (cin >> m >> n, m || n) {
    int ba[m][n];
    int num = 0;
    int sx, sy;
    REP(i, n) {
      REP(j, m) {
        int a;
        cin >> a;
        if (a == 1) {
          ba[j][i] = ++num;
        } else {
          ba[j][i] = 0;
          if (a == 2) {
            sx = j;
            sy = i;
          }
        }
      }
    }
    const int dx[] = {0, 1, 0, -1};
    const int dy[] = {-1, 0, 1, 0};
    map<int, int> mp;
    mp[((1 << num) - 1) * 100 + sx * 10 + sy] = 1;
    REP(i, num) {
      map<int, int> nextmp;
      FOR(it, mp) {
        int hoge = it->first;
        int S = hoge / 100;
        int x = (hoge % 100) / 10;
        int y = hoge % 10;
        //        cout << bitset<7>(S) << " " << x << " "<<y << " " <<
        //        it->second << endl;
        REP(k, 4) {
          int xx = x, yy = y;
          while (1) {
            xx += dx[k];
            yy += dy[k];
            if (xx < 0 || xx >= m || yy < 0 || yy >= n)
              break;
            if (ba[xx][yy]) {
              if (S & (1 << (ba[xx][yy] - 1))) {
                nextmp[(S - (1 << (ba[xx][yy] - 1))) * 100 + xx * 10 + yy] +=
                    it->second;
                break;
              }
            }
          }
        }
      }
      //  cout << endl;
      mp = nextmp;
    }
    int res = 0;
    REP(y, n) {
      REP(x, m) {
        if (x == sx || y == sy)
          res += mp[x * 10 + y];
      }
    }
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

vector<int> hx, hy;
int sx, sy;
int num;

bool C1(int S, int x1, int x2, int y) {
  REP(j, num)
  if ((1 << j) & S && hy[j] == y &&
      (x1 < hx[j] && hx[j] < x2 || x2 < hx[j] && hx[j] < x1))
    return false;
  return true;
}
bool C2(int S, int x1, int x2, int y) {
  REP(j, num)
  if ((1 << j) & S && hx[j] == y &&
      (x1 < hy[j] && hy[j] < x2 || x2 < hy[j] && hy[j] < x1))
    return false;
  return true;
}
map<int, int> visited; //[1<<23][10][10];

int rec(int S, int nx, int ny) {
  int nest = 0;
  REP(i, num) {
    if ((S >> i) & 1)
      nest++;
  }
  // cout << string(nest,' ');
  // cout << bitset<7>(S) << " " << nx << " "<<ny << endl;

  if (visited.count(S * 100 + nx * 10 + ny)) {
    //    cout << "VISITED" << endl;
    return visited[S * 100 + nx * 10 + ny];
  }
  if (S == 0) {
    if (sx == nx || sy == ny)
      return 1;
    else
      return 0;
  }
  int res = 0;
  int hoge = 0;
  REP(i, num) {
    if (!((1 << i) & S))
      continue;
    hoge++;
    if (hy[i] == ny && C1(S, hx[i], nx, ny) ||
        hx[i] == nx && C2(S, hy[i], ny, nx))
      res += rec(S - (1 << i), hx[i], hy[i]);
  }
  if (hoge < 18)
    visited[S * 100 + nx * 10 + ny] = res;
  return res;
}

int main() {
  int m, n;
  while (cin >> m >> n, m || n) {
    hx.clear();
    hy.clear();
    visited.clear();
    REP(i, n) {
      REP(j, m) {
        int a;
        cin >> a;
        if (a == 1) {
          hx.push_back(j);
          hy.push_back(i);
        } else if (a == 2) {
          sx = j;
          sy = i;
        }
      }
    }
    num = hx.size();
    cout << rec((1 << num) - 1, sx, sy) << endl;
  }
}