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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

vector<int> g[24][4]; // 0?????????, ??????????????¶????????????
int maps[10][10];
int m, n;
int cnt;

typedef pair<int, int> pii;

int main() {
  while (cin >> m >> n, m || n) {
    REP(i, 24) REP(j, 4) g[i][j].clear();

    REP(i, n) REP(j, m) scanf("%d", &maps[i][j]);

    cnt = 1;
    REP(i, n) {
      REP(j, m) {
        int tmp = -1;
        switch (maps[i][j]) {
        case 1:
          tmp = cnt++;
          break;
        case 2:
          tmp = 0;
          break;
        }
        maps[i][j] = tmp;
      }
    }

    REP(i, n) REP(j, m) {
      int from = maps[i][j];
      if (from != -1) {
        int tmp = 0;
        REP(k, n) if (maps[k][j] != -1) {
          if (k == i) {
            tmp++;
            continue;
          }
          g[from][tmp].push_back(maps[k][j]);
        }
        tmp = 2;
        REP(k, m) if (maps[i][k] != -1) {
          if (k == j) {
            tmp++;
            continue;
          }
          g[from][tmp].push_back(maps[i][k]);
        }
        reverse(g[from][0].begin(), g[from][0].end());
        reverse(g[from][2].begin(), g[from][2].end());
      }
    }

    map<int, int> now[24]; // [?????¨?????????][?????????????????¶?????¶???] =
                           // ????????????????????°
    now[0][0] = 1;

    int ans = 0;
    REP(i, cnt) {
      map<int, int> next[24];
      REP(j, cnt) {
        for (map<int, int>::iterator it = now[j].begin(); it != now[j].end();
             ++it) {
          int bit = (*it).first;

          REP(k, 4) REP(l, g[j][k].size()) {
            int nextPos = g[j][k][l];
            int nextBit = bit | (1 << nextPos);

            if ((bit >> nextPos) & 1)
              continue;
            if (nextPos == 0 && nextBit != (1 << cnt) - 1)
              continue;

            next[nextPos][nextBit] += (*it).second;

            break;
          }
        }
      }
      REP(j, cnt) now[j] = next[j];
    }

    cout << now[0][(1 << cnt) - 1] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

map<int, int> data[100]; // [?????¨?????????][????????§????¨??????????????????°]
                         // = ????????????????????°
vector<int> g[100][4]; // 0?????????, ??????????????¶????????????g[i][j]
                       // i??????????????? j:0???????????????, 1???, 2???,
                       // 3???????????§?????????
int maps[10][10];
int m, n;
int cnt;

int dfs(int used, int pos) {
  if (data[pos].find(used) != data[pos].end())
    return data[pos][used];
  if (pos == 0)
    return used == (1 << cnt) - 1;

  int res = 0;

  REP(i, 4) {
    REP(j, g[pos][i].size()) {
      int to = g[pos][i][j];
      if ((used >> to) & 1)
        break;
      res += dfs(used | (1 << to), to);
    }
  }

  return data[pos][used] = res;
}

int main() {
  while (cin >> m >> n, m || n) {
    REP(i, 100) REP(j, 4) g[i][j].clear();
    REP(i, 100) data[i].clear();

    REP(i, n) REP(j, m) scanf("%d", &maps[i][j]);

    cnt = 1;
    REP(i, n) {
      REP(j, m) {
        int tmp = -1;
        switch (maps[i][j]) {
        case 1:
          tmp = cnt++;
          break;
        case 2:
          tmp = 0;
          break;
        }
        maps[i][j] = tmp;
      }
    }

    REP(i, n) REP(j, m) {
      int from = maps[i][j];
      if (from != -1) {
        int tmp = 0;
        REP(k, n) if (maps[k][j] != -1) {
          if (k == i)
            tmp++;
          else
            g[from][tmp].push_back(maps[k][j]);
        }

        tmp = 2;
        REP(k, m) if (maps[i][k] != -1) {
          if (k == j)
            tmp++;
          else
            g[from][tmp].push_back(maps[i][k]);
        }

        reverse(g[from][0].begin(), g[from][0].end());
        reverse(g[from][2].begin(), g[from][2].end());
      }
    }

    int ans = 0;
    REP(i, 4) REP(j, g[0][i].size()) ans += dfs(1 << g[0][i][j], g[0][i][j]);
    cout << ans << endl;
  }
  return 0;
}