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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<short, int> P;

const int INF = 1 << 30;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// const long long int INF_=1<<60;

int M, N;
map<P, int> houses;
map<P, int> memo;

inline void MakeGraph(vector<vector<P>> &G, vector<vector<int>> &g) {
  REP(y, 0, N) REP(x, 0, M) {
    if (g[y][x] == 1 || g[y][x] == 2) {
      REP(i, 0, 4) {
        int ny = y, nx = x;
        while (true) {
          ny += dy[i];
          nx += dx[i];
          if (!(0 <= ny && ny < N && 0 <= nx && nx < M))
            break;
          if (g[ny][nx] == 1 || g[ny][nx] == 2) {
            G[houses[MP(y, x)]].push_back(MP(i, houses[MP(ny, nx)]));
            break;
          }
        }
      }
    }
  }
}

inline int DFS(vector<vector<P>> &G, int house_num, int dir, int state) {
  if ((state & ((1 << (houses.size() - 1)) - 1)) ==
          ((1 << (houses.size() - 1)) - 1) &&
      house_num == 0)
    return 1;
  int result1 = 0, result2 = 0;
  REP(i, 0, G[house_num].size()) {
    if (G[house_num][i].F_ == dir &&
        (house_num == 0 || ((state >> (house_num - 1)) & 1) == 1))
      result1 += DFS(G, G[house_num][i].S_, dir, state);
    if ((__builtin_popcount(state) > 13 || memo[MP(house_num, state)] == 0) &&
        house_num != 0 && ((state >> (house_num - 1)) & 1) != 1) {
      result2 += DFS(G, G[house_num][i].S_, G[house_num][i].F_,
                     state | (1 << (house_num - 1)));
    }
  }
  if (__builtin_popcount(state) <= 13 && memo[MP(house_num, state)] == 0)
    memo[MP(house_num, state)] = result2 + 1;
  return (__builtin_popcount(state) <= 13 ? memo[MP(house_num, state)]
                                          : result2 + 1) +
         result1 - 1;
}

inline lli Solve() {
  houses.clear();
  memo.clear();
  vector<vector<int>> g(N, vector<int>(M));
  int cnt = 1;
  REP(y, 0, N) REP(x, 0, M) {
    cin >> g[y][x];
    if (g[y][x] == 1) {
      houses[MP(y, x)] = cnt;
      ++cnt;
    } else if (g[y][x] == 2)
      houses[MP(y, x)] = 0;
  }
  vector<vector<P>> G(cnt);
  MakeGraph(G, g);
  lli ans = 0;
  REP(i, 0, G[0].size()) { ans += DFS(G, G[0][i].S_, G[0][i].F_, 0); }
  return ans;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  while (cin >> M >> N && M && N) {
    cout << Solve() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<short, int> P;

const int INF = 1 << 30;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// const long long int INF_=1<<60;

int M, N;
map<P, int> houses;
map<P, int> memo;

inline void MakeGraph(vector<vector<P>> &G, vector<vector<int>> &g) {
  REP(y, 0, N) REP(x, 0, M) {
    if (g[y][x] == 1 || g[y][x] == 2) {
      REP(i, 0, 4) {
        int ny = y, nx = x;
        while (true) {
          ny += dy[i];
          nx += dx[i];
          if (!(0 <= ny && ny < N && 0 <= nx && nx < M))
            break;
          if (g[ny][nx] == 1 || g[ny][nx] == 2) {
            G[houses[MP(y, x)]].push_back(MP(i, houses[MP(ny, nx)]));
            break;
          }
        }
      }
    }
  }
}

inline int DFS(vector<vector<P>> &G, int house_num, int dir, int state) {
  // cout << house_num << ' ' << dir << ' ' << state << ' ' <<
  // ((1<<(houses.size()-1))-1) << endl;
  if ((state & ((1 << (houses.size() - 1)) - 1)) ==
          ((1 << (houses.size() - 1)) - 1) &&
      house_num == 0)
    return 1;
  int result1 = 0, result2 = 0;
  REP(i, 0, G[house_num].size()) {
    if (G[house_num][i].F_ == dir &&
        (house_num == 0 || ((state >> (house_num - 1)) & 1) == 1))
      result1 += DFS(G, G[house_num][i].S_, dir, state);
    if ((__builtin_popcount(state) > 14 || memo[MP(house_num, state)] == 0) &&
        house_num != 0 && ((state >> (house_num - 1)) & 1) != 1) {
      result2 += DFS(G, G[house_num][i].S_, G[house_num][i].F_,
                     state | (1 << (house_num - 1)));
    }
  }
  // cout << result1 << ' ' << result2 << endl;
  if (__builtin_popcount(state) <= 14 && memo[MP(house_num, state)] == 0)
    memo[MP(house_num, state)] = result2 + 1;
  return (__builtin_popcount(state) <= 14 ? memo[MP(house_num, state)]
                                          : result2 + 1) +
         result1 - 1;
}

inline lli Solve() {
  houses.clear();
  memo.clear();
  vector<vector<int>> g(N, vector<int>(M));
  int cnt = 1;
  REP(y, 0, N) REP(x, 0, M) {
    cin >> g[y][x];
    if (g[y][x] == 1) {
      houses[MP(y, x)] = cnt;
      ++cnt;
    } else if (g[y][x] == 2)
      houses[MP(y, x)] = 0;
  }
  vector<vector<P>> G(cnt);
  MakeGraph(G, g);
  lli ans = 0;
  REP(i, 0, G[0].size()) { ans += DFS(G, G[0][i].S_, G[0][i].F_, 0); }
  return ans;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  while (cin >> M >> N && M && N) {
    cout << Solve() << endl;
  }
  return 0;
}