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

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
using namespace std;

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

class State {
public:
  int h, c, x, y;
  State(int x, int y, int h, int c) : h(h), c(c), x(x), y(y) {}
};

bool tf[5][5];

set<pair<int, int>> hs;

int ch(bool f[5][5], int N) {
  int ret = 0, cnt = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      ret |= (f[j][i] << cnt);
      cnt++;
    }

  return ret;
}

void mov(int x, int y, bool f[5][5], int N) {
  memset(tf, 0, sizeof(tf));
  f[x][y] = 1;
  int cnt[5][5] = {0};
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (!f[j][i])
        continue;
      for (int k = 0; k < 8; k++) {
        int tx = j + dx[k], ty = i + dy[k];
        if (tx < 0 || ty < 0 || tx >= N || ty >= N)
          continue;

        cnt[tx][ty]++;
      }
    }

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (f[j][i] && (cnt[j][i] == 2 || cnt[j][i] == 3)) {
        tf[j][i] = 1;
      }
      if (!f[j][i] && cnt[j][i] == 3) {
        tf[j][i] = 1;
      }
    }

  tf[x][y] = 0;
  f[x][y] = 0;
}

int main() {
  int N;
  while (cin >> N, N) {
    hs.clear();
    bool f[5][5] = {0};
    int sx, sy;

    for (int i = 0; i < N; i++) {
      cin.ignore();
      for (int j = 0; j < N; j++) {
        char c;
        cin >> c;
        if (c == '@') {
          sx = j;
          sy = i;
        }
        if (c == '#')
          f[j][i] = 1;
      }
    }

    queue<State> q;
    q.push(State(sx, sy, ch(f, N), 0));

    int ans = -1;
    while (!q.empty()) {
      State s = q.front();
      q.pop();
      if (s.h == 0) {
        ans = s.c;
        break;
      }

      bool sf[5][5];
      int tcnt = 0;
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++) {
          sf[j][i] = (s.h & (1 << tcnt));
          tcnt++;
        }

      for (int i = 0; i < 8; i++) {
        int tx = s.x + dx[i], ty = s.y + dy[i];
        if (tx < 0 || ty < 0 || tx >= N || ty >= N)
          continue;
        if (sf[tx][ty])
          continue;

        mov(tx, ty, sf, N);
        int h = ch(tf, N);
        if (hs.count(make_pair(tx * 5 + ty, h)))
          continue;
        hs.insert(make_pair(tx * 5 + ty, h));

        q.push(State(tx, ty, h, s.c + 1));
      }
    }
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

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

class State {
public:
  int h, c, x, y;

  State(int x, int y, int h, int c) : h(h), c(c), x(x), y(y) {}
};

bool tf[5][5], hs[25][(1 << 25)];

int ch(bool f[5][5], int N) {
  int ret = 0, cnt = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      ret |= (f[j][i] << cnt);
      cnt++;
    }

  return ret;
}

void mov(int x, int y, bool f[5][5], int N) {
  memset(tf, 0, sizeof(tf));
  f[x][y] = 1;
  int cnt[5][5] = {0};
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (!f[j][i])
        continue;
      for (int k = 0; k < 8; k++) {
        int tx = j + dx[k], ty = i + dy[k];
        if (tx < 0 || ty < 0 || tx >= N || ty >= N)
          continue;

        cnt[tx][ty]++;
      }
    }

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (f[j][i] && (cnt[j][i] == 2 || cnt[j][i] == 3)) {
        tf[j][i] = 1;
      }
      if (!f[j][i] && cnt[j][i] == 3) {
        tf[j][i] = 1;
      }
    }

  tf[x][y] = 0;
  f[x][y] = 0;
}

int main() {
  int N;
  while (cin >> N, N) {
    memset(hs, 0, sizeof(hs));
    bool f[5][5] = {0};
    int sx, sy;

    for (int i = 0; i < N; i++) {
      cin.ignore();
      for (int j = 0; j < N; j++) {
        char c;
        cin >> c;
        if (c == '@') {
          sx = j;
          sy = i;
        }
        if (c == '#')
          f[j][i] = 1;
      }
    }

    queue<State> q;
    q.push(State(sx, sy, ch(f, N), 0));

    int ans = -1;
    while (!q.empty()) {
      State s = q.front();
      q.pop();
      if (s.h == 0) {
        ans = s.c;
        break;
      }

      bool sf[5][5];
      int tcnt = 0;
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++) {
          sf[j][i] = (s.h & (1 << tcnt));
          tcnt++;
        }

      for (int i = 0; i < 8; i++) {
        int tx = s.x + dx[i], ty = s.y + dy[i];
        if (tx < 0 || ty < 0 || tx >= N || ty >= N)
          continue;
        if (sf[tx][ty])
          continue;

        mov(tx, ty, sf, N);
        int h = ch(tf, N);
        if (hs[tx * 5 + ty][h])
          continue;
        hs[tx * 5 + ty][h] = 1;

        q.push(State(tx, ty, h, s.c + 1));
      }
    }
    cout << ans << endl;
  }
}