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

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define POWT(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODI 10000
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

signed main() {
  int w, h;
  while (scanf("%d %d", &w, &h)) {
    if (!w && !h)
      break;
    vector<vector<char>> mp(w, vector<char>(h, -1));
    pii cha;
    vector<pair<char, char>> homp;
    int homc = 0;
    REP(i, h) {
      REP(j, w) {
        int a = 0;
        scanf("%d", &a);
        if (a == 2) {
          cha = {j, i};
        }
        if (a == 1) {
          homp.push_back({j, i});
          mp[j][i] = homc;
          homc++;
        }
      }
    }
    homp.push_back(cha);
    map<pair<int, char>, int> dp[2]; //?±\\??´????????¨??°
    dp[0][{0, homc}] = 1;
    bool t = 0;
    REP(i, homc) {
      for (auto cur : dp[t]) {
        pii curpos = homp[cur.first.second];
        int cnum;
        for (int j = curpos.second + 1; h > j; j++) { //???
          cnum = mp[curpos.first][j];
          if (cnum == -1)
            continue;
          if (!bitcheck(cur.first.first, cnum)) {
            int buf = cur.first.first;
            bitset(buf, cnum);
            dp[1 - t][{buf, cnum}] += cur.second;
            break;
          }
        }
        for (int j = curpos.second - 1; 0 <= j; j--) { //???
          cnum = mp[curpos.first][j];
          if (cnum == -1)
            continue;
          if (!bitcheck(cur.first.first, cnum)) {
            int buf = cur.first.first;
            bitset(buf, cnum);
            dp[1 - t][{buf, cnum}] += cur.second;
            break;
          }
        }
        for (int j = curpos.first + 1; w > j; j++) { //???
          cnum = mp[j][curpos.second];
          if (cnum == -1)
            continue;
          if (!bitcheck(cur.first.first, cnum)) {
            int buf = cur.first.first;
            bitset(buf, cnum);
            dp[1 - t][{buf, cnum}] += cur.second;
            break;
          }
        }
        for (int j = curpos.first - 1; 0 <= j; j--) { //???
          cnum = mp[j][curpos.second];
          if (cnum == -1)
            continue;
          if (!bitcheck(cur.first.first, cnum)) {
            int buf = cur.first.first;
            bitset(buf, cnum);
            dp[1 - t][{buf, cnum}] += cur.second;
            break;
          }
        }
      }
      dp[t].clear();
      t = 1 - t;
    }
    int ans = 0;

    for (auto cur : dp[t]) {
      if (homp[cur.first.second].first == cha.first ||
          homp[cur.first.second].second == cha.second)
        ans += cur.second;
    }

    dp[1 - t].clear();
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define POWT(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODI 10000
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

signed main() {
  int w, h;
  while (scanf("%d %d", &w, &h)) {
    if (!w && !h)
      break;
    vector<vector<char>> mp(w, vector<char>(h, -1));
    pii cha;
    vector<pair<char, char>> homp;
    int homc = 0;
    REP(i, h) {
      REP(j, w) {
        int a = 0;
        scanf("%d", &a);
        if (a == 2) {
          cha = {j, i};
        }
        if (a == 1) {
          homp.push_back({j, i});
          mp[j][i] = homc;
          homc++;
        }
      }
    }
    homp.push_back(cha);
    set<int> targets;
    int ccn;
    for (int j = cha.first; w > j; j++) {
      ccn = mp[j][cha.second];
      if (ccn > -1) {
        targets.insert(ccn);
        break;
      }
    }
    for (int j = cha.first; 0 <= j; j--) {
      ccn = mp[j][cha.second];
      if (ccn > -1) {
        targets.insert(ccn);
        break;
      }
    }
    for (int j = cha.second; h > j; j++) {
      ccn = mp[cha.first][j];
      if (ccn > -1) {
        targets.insert(ccn);
        break;
      }
    }
    for (int j = cha.second; 0 <= j; j--) {
      ccn = mp[cha.first][j];
      if (ccn > -1) {
        targets.insert(ccn);
        break;
      }
    }
    map<pair<int, char>, int> dp[2]; //?±\\??´????????¨??°
    dp[0][{0, homc}] = 1;
    bool t = 0;
    REP(i, homc) {
      for (auto cur : dp[t]) {
        pii curpos = homp[cur.first.second];
        int cnum;
        for (int j = curpos.second + 1; h > j; j++) { //???
          cnum = mp[curpos.first][j];
          if (cnum == -1)
            continue;
          if (!bitcheck(cur.first.first, cnum)) {
            int buf = cur.first.first;
            bitset(buf, cnum);
            dp[1 - t][{buf, cnum}] += cur.second;
            break;
          } else
            break;
        }
        for (int j = curpos.second - 1; 0 <= j; j--) { //???
          cnum = mp[curpos.first][j];
          if (cnum == -1)
            continue;
          if (!bitcheck(cur.first.first, cnum)) {
            int buf = cur.first.first;
            bitset(buf, cnum);
            dp[1 - t][{buf, cnum}] += cur.second;
          } else
            break;
        }
        for (int j = curpos.first + 1; w > j; j++) { //???
          cnum = mp[j][curpos.second];
          if (cnum == -1)
            continue;
          if (!bitcheck(cur.first.first, cnum)) {
            int buf = cur.first.first;
            bitset(buf, cnum);
            dp[1 - t][{buf, cnum}] += cur.second;
          } else
            break;
        }
        for (int j = curpos.first - 1; 0 <= j; j--) { //???
          cnum = mp[j][curpos.second];
          if (cnum == -1)
            continue;
          if (!bitcheck(cur.first.first, cnum)) {
            int buf = cur.first.first;
            bitset(buf, cnum);
            dp[1 - t][{buf, cnum}] += cur.second;
          } else
            break;
        }
      }
      dp[t].clear();
      t = 1 - t;
    }
    int ans = 0;

    for (auto cur : dp[t]) {
      if (targets.find(cur.first.second) != targets.end())
        ans += cur.second;
    }

    dp[1 - t].clear();
    printf("%d\\n", ans);
  }
  return 0;
}