Problem p00724 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define x first
#define y second

int mx[] = {0, 1, 1, 0, -1, -1};
int my[] = {-1, -1, 0, 1, 1, 0};

class C {
public:
  int t;
  vector<P> h;
  C() {}
  C(int _t, vector<P> _h) {
    t = _t;
    h = _h;
  }
};

bool adj(P &a, P &b) {
  REP(i, 6) {
    P np = P(b.x + mx[i], b.y + my[i]);
    if (np == a)
      return true;
  }
  return false;
}

void dfs(vector<P> &H, int n, set<vector<P>> &S, vector<vector<P>> &ans,
         vector<P> &R, queue<C> &open, int &t) {
  REP(i, 6) {
    P tmp = H[n];
    H[n] = P(H[n].x + mx[i], H[n].y + my[i]);
    bool f = true, ff = true;
    REP(j, R.size()) REP(k, H.size()) if (R[j] == H[k]) f = false;
    REP(j, H.size()) {
      FOR(k, j + 1, H.size()) {
        if (H[k] == H[j])
          ff = false;
        if ((j == k + 1 || j == k - 1) && (H[k] == H[j]))
          f = false;
        if (j == k + 1 || j == k - 1) {
          if (!adj(H[k], H[j]))
            f = false;
        } else {
          if (adj(H[k], H[j]))
            ff = false;
        }
      }
    }
    if (f) {
      if (ff && S.find(H) == S.end()) {
        S.insert(H);
        open.push(C(t + 1, H));
      }
      if (n + 2 < (int)H.size())
        dfs(H, n + 2, S, ans, R, open, t);
    }
    H[n] = tmp;
  }
  if (n + 2 < (int)H.size())
    dfs(H, n + 2, S, ans, R, open, t);
}

int dist(P &a, P &b) {
  return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

int bfs(vector<P> &H, vector<P> &R, P &G) {
  queue<C> open;
  open.push(C(0, H));
  set<vector<P>> closed;
  closed.insert(H);
  while (!open.empty()) {
    C c = open.front();
    open.pop();
    if (G == c.h[0])
      return c.t;
    if (c.t >= 19)
      continue;
    if (dist(c.h[0], G) >= (20 - c.t) * (20 - c.t))
      continue;
    vector<vector<P>> next;
    dfs(c.h, 0, closed, next, R, open, c.t);
    dfs(c.h, 1, closed, next, R, open, c.t);
  }
  return 20;
}

int main() {
  int N, K;
  P G;
  while (cin >> N && N) {
    vector<P> H(N);
    REP(i, N) scanf("%d %d", &H[i].x, &H[i].y);
    scanf("%d", &K);
    vector<P> R(K);
    REP(i, K) scanf("%d %d", &R[i].x, &R[i].y);
    scanf("%d %d", &G.x, &G.y);
    printf("%d\\n", bfs(H, R, G));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define x first
#define y second

int mx[] = {0, 1, 1, 0, -1, -1};
int my[] = {-1, -1, 0, 1, 1, 0};

class C {
public:
  int t;
  vector<P> h;
  C() {}
  C(int _t, vector<P> _h) {
    t = _t;
    h = _h;
  }
};

bool adj(P &a, P &b) {
  REP(i, 6) {
    P np = P(b.x + mx[i], b.y + my[i]);
    if (np == a)
      return true;
  }
  return false;
}

void dfs(vector<P> &H, int n, set<vector<P>> &S, vector<vector<P>> &ans,
         vector<P> &R, queue<C> &open, int &t) {
  REP(i, 6) {
    P tmp = H[n];
    H[n] = P(H[n].x + mx[i], H[n].y + my[i]);
    bool f = true, ff = true;
    REP(j, R.size()) REP(k, H.size()) if (R[j] == H[k]) f = false;
    REP(j, H.size()) {
      FOR(k, j + 1, H.size()) {
        if (H[k] == H[j])
          ff = false;
        if ((j == k + 1 || j == k - 1) && (H[k] == H[j]))
          f = false;
        if (j == k + 1 || j == k - 1) {
          if (!adj(H[k], H[j]))
            f = false;
        } else {
          if (adj(H[k], H[j]))
            ff = false;
        }
      }
    }
    if (f) {
      if (ff && S.find(H) == S.end()) {
        S.insert(H);
        open.push(C(t + 1, H));
      }
      if (n + 2 < (int)H.size())
        dfs(H, n + 2, S, ans, R, open, t);
    }
    H[n] = tmp;
  }
  if (n + 2 < (int)H.size())
    dfs(H, n + 2, S, ans, R, open, t);
}

int bfs(vector<P> &H, vector<P> &R, P &G) {
  queue<C> open;
  open.push(C(0, H));
  set<vector<P>> closed;
  closed.insert(H);
  while (!open.empty()) {
    C c = open.front();
    open.pop();
    if (G == c.h[0])
      return c.t;
    if (c.t >= 19)
      continue;
    vector<vector<P>> next;
    dfs(c.h, 0, closed, next, R, open, c.t);
    dfs(c.h, 1, closed, next, R, open, c.t);
  }
  return 20;
}

int main() {
  int N, K;
  P G;
  while (cin >> N && N) {
    vector<P> H(N);
    REP(i, N) scanf("%d %d", &H[i].x, &H[i].y);
    scanf("%d", &K);
    vector<P> R(K);
    REP(i, K) scanf("%d %d", &R[i].x, &R[i].y);
    scanf("%d %d", &G.x, &G.y);
    printf("%d\\n", bfs(H, R, G));
  }
  return 0;
}