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

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int px[24], py[24], kx, ky;
int num[24][4] = {0};

int H, W, N, ans;
int field[12][12] = {0};

int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
bool done[12][12];

map<int, int> memo[24];

int dfs(int pos, int bit) {
  if (memo[pos].find(bit) != memo[pos].end())
    return memo[pos][bit];
  if (bit == (1 << N) - 1) {
    rep(d, 4) {
      int cx = px[pos] + dx[d], cy = py[pos] + dy[d];
      while (field[cy][cx] != -1) {
        if (field[cy][cx] == 64)
          return 1;
        cy += dy[d], cx += dx[d];
      }
    }
    return 0;
  }
  int ans = 0;
  rep(d, 4) {
    int cx = px[pos] + dx[d], cy = py[pos] + dy[d];
    while (field[cy][cx] == -2 || done[cy][cx] == 1 || field[cy][cx] == 64) {
      cy += dy[d], cx += dx[d];
    }
    if (field[cy][cx] != -1) {
      done[cy][cx] = 1;
      ans += dfs(field[cy][cx], bit | (1 << field[cy][cx]));
      done[cy][cx] = 0;
    }
  }
  if (__builtin_popcount(bit) < 15) {
    return memo[pos][bit] = ans;
  } else {
    return ans;
  }
}

int main() {
  while (cin >> W >> H, W) {
    N = ans = 0;
    rep(i, 24) rep(j, 4) num[i][j] = -1;
    rep(i, 12) rep(j, 12) field[i][j] = -1;
    rep(i, H) rep(j, W) {
      int t;
      cin >> t;
      switch (t) {
      case 0:
        field[i + 1][j + 1] = -2;
        break;
      case 1:
        field[i + 1][j + 1] = N;
        px[N] = j + 1, py[N] = i + 1;
        N++;
        break;
      case 2:
        kx = j + 1, ky = i + 1;
        field[i + 1][j + 1] = 64;
        break;
      }
    }
    px[N] = kx, py[N] = ky;
    rep(i, 24) memo[i].clear();
    cout << dfs(N, 0) << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int px[24], py[24], kx, ky;
int num[24][4] = {0};

int H, W, N, ans;
int field[12][12] = {0};

int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
bool done[12][12];

map<int, int> memo[24];

int dfs(int pos, int bit) {
  if (memo[pos].find(bit) != memo[pos].end())
    return memo[pos][bit];
  if (bit == (1 << N) - 1) {
    rep(d, 4) {
      int cx = px[pos] + dx[d], cy = py[pos] + dy[d];
      while (field[cy][cx] != -1) {
        if (field[cy][cx] == 64)
          return 1;
        cy += dy[d], cx += dx[d];
      }
    }
    return 0;
  }
  int ans = 0;
  rep(d, 4) {
    int cx = px[pos] + dx[d], cy = py[pos] + dy[d];
    while (field[cy][cx] == -2 || done[cy][cx] == 1 || field[cy][cx] == 64) {
      cy += dy[d], cx += dx[d];
    }
    if (field[cy][cx] != -1) {
      done[cy][cx] = 1;
      ans += dfs(field[cy][cx], bit | (1 << field[cy][cx]));
      done[cy][cx] = 0;
    }
  }
  return memo[pos][bit] = ans;
}

int main() {
  while (cin >> W >> H, W) {
    N = ans = 0;
    rep(i, 24) rep(j, 4) num[i][j] = -1;
    rep(i, 12) rep(j, 12) field[i][j] = -1;
    rep(i, H) rep(j, W) {
      int t;
      cin >> t;
      switch (t) {
      case 0:
        field[i + 1][j + 1] = -2;
        break;
      case 1:
        field[i + 1][j + 1] = N;
        px[N] = j + 1, py[N] = i + 1;
        N++;
        break;
      case 2:
        kx = j + 1, ky = i + 1;
        field[i + 1][j + 1] = 64;
        break;
      }
    }
    px[N] = kx, py[N] = ky;
    rep(i, 24) memo[i].clear();
    cout << dfs(N, 0) << endl;
  }
}