Problem p00471 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

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

typedef pair<int, int> state;

int main() {
  int n, m, field[10][10];
  while (cin >> m >> n, n) {
    int cnt = 0, x[25], y[25], sx, sy, res = 0;
    map<state, int> ans;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> field[i][j];
        if (field[i][j] == 2) {
          field[i][j] = -1;
          sx = i;
          sy = j;
        } else if (field[i][j] == 1) {
          x[cnt] = i;
          y[cnt] = j;
          field[i][j] = ++cnt;
        }
      }
    }
    x[cnt] = sx;
    y[cnt] = sy;
    priority_queue<state, vector<state>, greater<state>> que;
    que.push(state(0, cnt));
    ans[state(0, cnt)] = 1;
    while (!que.empty()) {
      state st = que.top();
      que.pop();
      for (int i = 0; i < 4; i++) {
        int nx = x[st.second] + dx[i], ny = y[st.second] + dy[i];
        while (nx >= 0 && nx < n && ny >= 0 && ny < m) {
          if (st.first == (1 << cnt) - 1) {
            if (field[nx][ny] == -1) {
              res += ans[st];
              break;
            }
          } else if (field[nx][ny] > 0 &&
                     !(st.first >> (field[nx][ny] - 1) & 1)) {
            state now =
                state(st.first | (1 << (field[nx][ny] - 1)), field[nx][ny] - 1);
            if (!ans[now])
              que.push(now);
            ans[now] += ans[st];
            break;
          }
          nx += dx[i];
          ny += dy[i];
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

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

typedef pair<int, int> state;

int main() {
  int n, m, field[10][10];
  while (cin >> m >> n, n) {
    int cnt = 0, x[25], y[25], sx, sy, res = 0;
    map<state, int> ans;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> field[i][j];
        if (field[i][j] == 2) {
          field[i][j] = -1;
          sx = i;
          sy = j;
        } else if (field[i][j] == 1) {
          x[cnt] = i;
          y[cnt] = j;
          field[i][j] = ++cnt;
        }
      }
    }
    x[cnt] = sx;
    y[cnt] = sy;
    priority_queue<state, vector<state>, greater<state>> que;
    que.push(state(0, cnt));
    ans[state(0, cnt)] = 1;
    while (!que.empty()) {
      state st = que.top();
      que.pop();
      for (int i = 0; i < 4; i++) {
        int nx = x[st.second] + dx[i], ny = y[st.second] + dy[i];
        while (nx >= 0 && nx < n && ny >= 0 && ny < m) {
          if (st.first == (1 << cnt) - 1) {
            if (field[nx][ny] == -1) {
              res += ans[st];
              break;
            }
          } else if (field[nx][ny] > 0 &&
                     !(st.first >> (field[nx][ny] - 1) & 1)) {
            state now =
                state(st.first | (1 << (field[nx][ny] - 1)), field[nx][ny] - 1);
            if (!ans[now])
              que.push(now);
            ans[now] += ans[st];
            break;
          }
          nx += dx[i];
          ny += dy[i];
        }
      }
      ans.erase(ans.find(st));
    }
    cout << res << endl;
  }
  return 0;
}