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

#include <bits/stdc++.h>
using namespace std;
int ax[] = {1, 1, 0, -1, -1, 0};
int ay[] = {0, -1, -1, 0, 1, 1};
typedef pair<int, int> P;
typedef vector<P> VP;
#define X first
#define Y second
bool adj(P a, P b) {
  for (int i = 0; i < 6; i++) {
    if (a.X + ax[i] == b.X && a.Y + ay[i] == b.Y)
      return 1;
  }
  return 0;
}
int n, m;
VP p, r;
vector<VP> t;
P g;
void dfs(int v, int f) {
  // cout<<v<<" "<<f<<endl;
  if (v == n) {
    t.push_back(p);
    return;
  }
  {
    bool flg = (v == 0) || adj(p[v - 1], p[v]);
    for (int j = 0; j < v - 1; j++)
      flg &= !adj(p[j], p[v]);
    for (int j = 0; j < v; j++)
      flg &= (p[j] != p[v]);
    for (int j = 0; j < m; j++)
      flg &= (r[j] != p[v]);
    if (flg)
      dfs(v + 1, 0);
  }
  if (!f) {
    for (int i = 0; i < 6; i++) {
      p[v].X += ax[i];
      p[v].Y += ay[i];
      bool flg = (v == 0) || adj(p[v - 1], p[v]);
      for (int j = 0; j < v - 1; j++)
        flg &= !adj(p[j], p[v]);
      for (int j = 0; j < v; j++)
        flg &= (p[j] != p[v]);
      for (int j = 0; j < m; j++)
        flg &= (r[j] != p[v]);
      if (flg)
        dfs(v + 1, 1);
      p[v].X -= ax[i];
      p[v].Y -= ay[i];
    }
  }
}
void dfs2(int v) {
  if (v == n) {
    t.push_back(p);
    return;
  }
  if (v == 0) {
    p[0] = g;
    dfs2(v + 1);
    return;
  }
  for (int i = 0; i < 6; i++) {
    p[v].X = p[v - 1].X + ax[i];
    p[v].Y = p[v - 1].Y + ay[i];
    bool flg = (v == 0) || adj(p[v - 1], p[v]);
    for (int j = 0; j < v - 1; j++)
      flg &= !adj(p[j], p[v]);
    for (int j = 0; j < v; j++)
      flg &= (p[j] != p[v]);
    for (int j = 0; j < m; j++)
      flg &= (r[j] != p[v]);
    if (flg)
      dfs2(v + 1);
  }
}
signed main() {
  while (cin >> n, n) {
    p.resize(n);
    for (int i = 0; i < n; i++)
      cin >> p[i].X >> p[i].Y;
    cin >> m;
    r.resize(m);
    for (int i = 0; i < m; i++)
      cin >> r[i].X >> r[i].Y;
    cin >> g.X >> g.Y;
    map<VP, int> mv[2];
    queue<VP> q;
    q.push(p);
    mv[0][p] = 0;
    int ans = 20;
    while (!q.empty()) {
      p = q.front();
      q.pop();
      int d = mv[0][p];
      if (d >= 10)
        break;
      t.clear();
      dfs(0, 0);
      for (VP np : t) {
        if (!mv[0].count(np)) {
          if (max(abs(np[0].X - g.X), abs(np[0].Y - g.Y)) + d + 1 >= 20)
            continue;
          mv[0][np] = d + 1;
          q.push(np);
        }
      }
    }
  END:

    while (!q.empty())
      q.pop();
    t.clear();
    dfs2(0);
    for (VP np : t) {
      q.push(np);
      mv[1][np] = 0;
    }
    while (!q.empty()) {
      p = q.front();
      q.pop();
      int d = mv[1][p];
      if (d >= min(ans, 9))
        break;
      if (mv[0].count(p))
        ans = min(ans, mv[0][p] + mv[1][p]);
      t.clear();
      dfs(0, 0);
      for (VP np : t) {
        if (!mv[1].count(np)) {
          mv[1][np] = d + 1;
          q.push(np);
          if (mv[0].count(np))
            ans = min(ans, mv[0][np] + mv[1][np]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int ax[] = {1, 1, 0, -1, -1, 0};
int ay[] = {0, -1, -1, 0, 1, 1};
typedef pair<int, int> P;
typedef vector<P> VP;
#define X first
#define Y second
bool adj(P a, P b) {
  for (int i = 0; i < 6; i++) {
    if (a.X + ax[i] == b.X && a.Y + ay[i] == b.Y)
      return 1;
  }
  return 0;
}
int n, m;
VP p, r;
vector<VP> t;
P g;
void dfs(int v, int f) {
  // cout<<v<<" "<<f<<endl;
  if (v == n) {
    t.push_back(p);
    return;
  }
  {
    bool flg = (v == 0) || adj(p[v - 1], p[v]);
    for (int j = 0; j < v - 1; j++)
      flg &= !adj(p[j], p[v]);
    for (int j = 0; j < v; j++)
      flg &= (p[j] != p[v]);
    for (int j = 0; j < m; j++)
      flg &= (r[j] != p[v]);
    if (flg)
      dfs(v + 1, 0);
  }
  if (!f) {
    for (int i = 0; i < 6; i++) {
      p[v].X += ax[i];
      p[v].Y += ay[i];
      bool flg = (v == 0) || adj(p[v - 1], p[v]);
      for (int j = 0; j < v - 1; j++)
        flg &= !adj(p[j], p[v]);
      for (int j = 0; j < v; j++)
        flg &= (p[j] != p[v]);
      for (int j = 0; j < m; j++)
        flg &= (r[j] != p[v]);
      if (flg)
        dfs(v + 1, 1);
      p[v].X -= ax[i];
      p[v].Y -= ay[i];
    }
  }
}
void dfs2(int v) {
  if (v == n) {
    t.push_back(p);
    return;
  }
  if (v == 0) {
    p[0] = g;
    dfs2(v + 1);
    return;
  }
  for (int i = 0; i < 6; i++) {
    p[v].X = p[v - 1].X + ax[i];
    p[v].Y = p[v - 1].Y + ay[i];
    bool flg = (v == 0) || adj(p[v - 1], p[v]);
    for (int j = 0; j < v - 1; j++)
      flg &= !adj(p[j], p[v]);
    for (int j = 0; j < v; j++)
      flg &= (p[j] != p[v]);
    for (int j = 0; j < m; j++)
      flg &= (r[j] != p[v]);
    if (flg)
      dfs2(v + 1);
  }
}
signed main() {
  while (cin >> n, n) {
    p.resize(n);
    for (int i = 0; i < n; i++)
      cin >> p[i].X >> p[i].Y;
    cin >> m;
    r.resize(m);
    for (int i = 0; i < m; i++)
      cin >> r[i].X >> r[i].Y;
    cin >> g.X >> g.Y;
    map<VP, int> mv[2];
    queue<VP> q;
    q.push(p);
    mv[0][p] = 0;
    int ans = 20;
    while (!q.empty()) {
      p = q.front();
      q.pop();
      int d = mv[0][p];
      if (d >= 10)
        break;
      // cout<<"*"<<d<<endl;
      // for(P w:p) cout<<w.first<<" : "<<w.second<<endl;
      t.clear();
      dfs(0, 0);
      for (VP np : t) {
        if (!mv[0].count(np)) {
          if (max(abs(np[0].X - g.X), abs(np[0].Y - g.Y)) + d + 1 >= 20)
            continue;
          mv[0][np] = d + 1;
          q.push(np);
        }
      }
    }
  END:

    while (!q.empty())
      q.pop();
    t.clear();
    dfs2(0);
    for (VP np : t) {
      q.push(np);
      mv[1][np] = 0;
    }
    while (!q.empty()) {
      p = q.front();
      q.pop();
      int d = mv[1][p];
      if (d >= min(ans, 10LL))
        break;
      if (mv[0].count(p))
        ans = min(ans, mv[0][p] + mv[1][p]);
      t.clear();
      dfs(0, 0);
      for (VP np : t) {
        if (!mv[1].count(np)) {
          mv[1][np] = d + 1;
          q.push(np);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}