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

#include <cstdio>
#include <cstdlib>
#include <queue>
#include <set>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define QCLR(q)                                                                \\
  while (!q.empty())                                                           \\
    q.pop();

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

int N, K, rx[100], ry[100], gx, gy;

class Hex {
public:
  int px[8], py[8];
  Hex(int tpx[8], int tpy[8]) {
    REP(i, N) {
      px[i] = tpx[i];
      py[i] = tpy[i];
    }
  }

  bool operator<(const Hex &h) const {
    REP(i, N) {
      if (px[i] != h.px[i])
        return px[i] < h.px[i];
      if (py[i] != h.py[i])
        return py[i] < h.py[i];
    }
    return false;
  }
};

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

set<Hex> s;
queue<State> q;

int dist60(int x1, int y1, int x2, int y2) {
  return (abs(x1 - x2) + abs(y1 - y2) + abs((x1 + y1) - (x2 + y2))) / 2;
}

bool crash(Hex &h, int p) {
  REP(i, K) if (h.px[p] == rx[i] && h.py[p] == ry[i]) return true;

  return false;
}

bool connect(Hex &h, int j) {
  REP(i, j) {
    if (h.px[i] == h.px[j] && h.py[i] == h.py[j])
      return false;

    int cnt = 0;
    REP(k, 6) {
      int tx = h.px[i] + dx[k], ty = h.py[i] + dy[k];
      if (h.px[j] == tx && h.py[j] == ty) {
        cnt++;
        break;
      }
    }

    if (abs(i - j) == 1 && cnt != 1)
      return false;
    if (abs(i - j) != 1 && cnt == 1)
      return false;
  }

  return true;
}

bool create(int p, Hex &h, int c, bool sk) {
  if (p == N) {
    if (!s.count(h)) {
      s.insert(h);
      q.push(State(h, c + 1));
      return true;
    }
    return false;
  }

  int cnt = 0;
  if (connect(h, p))
    create(p + 1, h, c, false);
  if (!sk) {
    REP(i, 6) {
      h.px[p] += dx[i], h.py[p] += dy[i];
      if (crash(h, p) || !connect(h, p)) {
        h.px[p] -= dx[i], h.py[p] -= dy[i];
        continue;
      }

      if (create(p + 1, h, c, true))
        cnt++;
      h.px[p] -= dx[i], h.py[p] -= dy[i];

      if (p == 0 || p == N - 1) {
        if (cnt == 2)
          break;
      } else {
        if (cnt == 1)
          break;
      }
    }
  }

  return false;
}

int bfs(int ix[8], int iy[8]) {
  q.push(State(Hex(ix, iy), 0));

  while (1) {
    State s = q.front();
    q.pop();
    if (s.h.px[0] == gx && s.h.py[0] == gy)
      return s.c;
    int dist = dist60(gx, gy, s.h.px[0], s.h.py[0]);
    // if(dist>=1) dist+=(dist-1)*1.5;
    if (dist > 20 - s.c)
      continue;
    if (dist60(gx, gy, s.h.px[0], s.h.py[0]) > 20 - s.c)
      continue;
    create(0, s.h, s.c, false);
  }

  return 0;
}
int main() {
  while (scanf("%d", &N), N) {
    QCLR(q);
    s.clear();
    int px[8], py[8];
    REP(i, N) scanf("%d %d", &px[i], &py[i]);

    scanf("%d", &K);
    REP(i, K) scanf("%d %d", &rx[i], &ry[i]);

    scanf("%d%d", &gx, &gy);
    printf("%d\\n", bfs(px, py));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <set>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define QCLR(q)                                                                \\
  while (!q.empty())                                                           \\
    q.pop();

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

int N, K, rx[100], ry[100], gx, gy;

class Hex {
public:
  int px[8], py[8];
  Hex(int tpx[8], int tpy[8]) {
    REP(i, N) {
      px[i] = tpx[i];
      py[i] = tpy[i];
    }
  }

  bool operator<(const Hex &h) const {
    REP(i, N) {
      if (px[i] != h.px[i])
        return px[i] < h.px[i];
      if (py[i] != h.py[i])
        return py[i] < h.py[i];
    }
    return false;
  }
};

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

set<Hex> s;
queue<State> q;

int dist60(int x1, int y1, int x2, int y2) {
  return (abs(x1 - x2) + abs(y1 - y2) + abs((x1 + y1) - (x2 + y2))) / 2;
}

bool crash(Hex &h) {
  REP(i, N) REP(j, K) if (h.px[i] == rx[j] && h.py[i] == ry[j]) return true;
  return false;
}

bool connect(Hex &h) {
  REP(i, N) FOR(j, i + 1, N) {
    if (h.px[i] == h.px[j] && h.py[i] == h.py[j])
      return false;

    int cnt = 0;
    REP(k, 7) {
      int tx = h.px[i] + dx[k], ty = h.py[i] + dy[k];
      if (h.px[j] == tx && h.py[j] == ty) {
        cnt++;
        break;
      }
    }

    if (abs(i - j) == 1 && cnt != 1)
      return false;
    if (abs(i - j) != 1 && cnt == 1)
      return false;
  }
  return true;
}

void create(int p, Hex &h, int c) {
  if (p >= N)
    return;
  REP(i, 7) {
    h.px[p] += dx[i], h.py[p] += dy[i];
    create(p + (i == 0 ? 1 : 2), h, c);
    if (!crash(h) && connect(h) && !s.count(h)) {
      s.insert(h);
      q.push(State(h, c + 1));
    }
    h.px[p] -= dx[i], h.py[p] -= dy[i];
  }
}

int bfs(int ix[8], int iy[8]) {
  QCLR(q);
  q.push(State(Hex(ix, iy), 0));

  while (!q.empty()) {
    State s = q.front();
    q.pop();
    if (s.h.px[0] == gx && s.h.py[0] == gy)
      return s.c;
    if (dist60(gx, gy, s.h.px[0], s.h.py[0]) > 20 - s.c)
      continue;
    create(0, s.h, s.c);
  }

  return 0;
}
int main() {
  while (scanf("%d", &N), N) {
    s.clear();
    int px[8], py[8];
    REP(i, N) scanf("%d %d", &px[i], &py[i]);

    scanf("%d", &K);
    REP(i, K) scanf("%d %d", &rx[i], &ry[i]);

    scanf("%d%d", &gx, &gy);

    printf("%d\\n", bfs(px, py));
  }
}