Problem p00471 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int grid[10][10];

struct STATE {
  int done, x, y;

  bool operator<(const STATE &t) const {
    if (done == t.done) {
      if (x == t.x) {
        return y < t.y;
      }
      return x < t.x;
    }
    return done < t.done;
  }
};

int main() {
  int m, n;
  while (scanf("%d %d", &m, &n), m) {
    int num = 0;
    int sx, sy;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%d", &grid[i][j]);
        if (grid[i][j] == 0) {
          grid[i][j] = -1;
        } else if (grid[i][j] == 1) {
          grid[i][j] = num++;
        } else {
          grid[i][j] = -2;
          sx = i;
          sy = j;
        }
      }
    }

    map<STATE, int> dp;
    map<STATE, int> dp2;
    STATE tmp = {0, sx, sy};
    dp[tmp] = 1;
    int res = 0;
    while (true) {
      STATE tmp = {(1 << num) - 1, sx, sy};
      if (dp.count(tmp)) {
        res = dp[tmp];
        break;
      }

      for (map<STATE, int>::iterator ite = dp.begin(); ite != dp.end(); ++ite) {
        STATE st = (*ite).first;

        // printf("%d %d %d %d\\n", st.done, st.x, st.y, dp[st]);

        for (int dir = 0; dir < 4; dir++) {
          int i = 1;
          while (true) {
            int tx = st.x + dx[dir] * i;
            int ty = st.y + dy[dir] * i;
            if (tx < 0 || tx >= n || ty < 0 || ty >= m)
              break;
            if (grid[tx][ty] == -2) {
              if (st.done == (1 << num) - 1) {
                STATE tmp = {st.done, tx, ty};
                dp2[tmp] += dp[st];
                break;
              }
            } else if ((grid[tx][ty] != -1) &&
                       (~st.done & (1 << grid[tx][ty]))) {
              STATE tmp = {st.done | (1 << grid[tx][ty]), tx, ty};
              dp2[tmp] += dp[st];
              break;
            }
            i++;
          }
        }
      }

      dp.clear();
      dp = dp2;
      dp2.clear();
    }

    printf("%d\\n", res);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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

struct STATE {
  int grid[10][10];
  int x, y;
  int num;
};

int main() {
  int m, n;
  while (cin >> m >> n, m + n) {
    STATE start;
    memset(&start, 0, sizeof(STATE));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%d", &start.grid[i][j]);
        if (start.grid[i][j] == 2) {
          start.x = i;
          start.y = j;
        }
        start.num += !!start.grid[i][j];
      }
    }

    int res = 0;
    queue<STATE> que;
    que.push(start);
    while (!que.empty()) {
      STATE st = que.front();
      que.pop();
      if (st.num == 0) {
        res++;
        continue;
      }

      for (int dir = 0; dir < 4; dir++) {
        int i = 1;
        while (true) {
          int tx = st.x + dx[dir] * i;
          int ty = st.y + dy[dir] * i;

          if (tx < 0 || tx >= n || ty < 0 || ty >= m)
            break;
          if (st.grid[tx][ty] == 3)
            break;

          if (st.grid[tx][ty] == (1 + (st.num == 1))) {
            STATE tmp = st;
            tmp.grid[tx][ty] = 3;
            tmp.x = tx;
            tmp.y = ty;
            tmp.num--;
            que.push(tmp);
          }
          i++;
        }
      }
    }

    printf("%d\\n", res);
  }

  return 0;
}