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

#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
#define MAX 21
#define INF 10000
#define EPS 1.0e-10
using namespace std;
typedef double elem;
typedef complex<elem> point;
double dist(const point &a, const point &b) { return std::abs(b - a); }
bool vis[MAX];
bool bt(const vector<point> &v, double walk, const point &h, const point &d) {
  bool bCont = false;

  for (unsigned int i = 0; i < v.size(); ++i) {
    if (!vis[i])
      bCont = true;
  }
  if (!bCont)
    return true;

  for (unsigned int i = 0; i < v.size(); ++i) {
    if (!vis[i]) {
      double t = walk + dist(h, v[i]);
      if (t < dist(d, v[i])) {
        vis[i] = true;
        if (bt(v, t, v[i], d))
          return true;
        vis[i] = false;
      } else {
        return false;
      }
    }
  }
  return false;
}
int main() {
  while (true) {
    bool bImpossible = false;
    int n, hx, hy, dx, dy;
    point hero, daemon;
    vector<point> vp;

    scanf("%d%d%d%d%d", &n, &hx, &hy, &dx, &dy);
    if (!(n || hx || hy || dx || dy))
      break;

    hero = complex<elem>(hx, hy);
    daemon = complex<elem>(dx, dy);

    for (int i = 0; i < n; ++i) {
      bool bInserted = false;
      int cx, cy;
      scanf("%d%d", &cx, &cy);
      point p(cx, cy);
      if (cx == hx && cy == hy)
        continue;
      for (unsigned int j = 0; j < vp.size() && !bInserted; ++j) {
        if (dist(daemon, vp[j]) > dist(daemon, p)) {
          vp.insert(vp.begin() + j, p);
          bInserted = true;
        }
      }
      if (!bInserted) {
        vp.push_back(p);
      }
      if (dist(daemon, p) <= dist(hero, p))
        bImpossible = true;
      vis[i] = false;
    }
    /*for(unsigned int i = 0; i < vp.size(); ++i){
            cout<<vp[i]<< ' ';
    }
    cout << endl;*/

    if (!bImpossible && bt(vp, 0.0, hero, daemon))
      printf("YES\\n");
    else
      printf("NO\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
#define MAX 21
#define INF 10000
#define EPS 1.0e-10
using namespace std;
typedef double elem;
typedef complex<elem> point;
double dist(const point &a, const point &b) { return std::abs(b - a); }
bool vis[MAX];
bool bt(const vector<point> &v, double walk, const point &h, const point &d) {
  bool bCont = false;

  for (unsigned int i = 0; i < v.size(); ++i) {
    if (!vis[i])
      bCont = true;
  }
  if (!bCont)
    return true;

  for (unsigned int i = 0; i < v.size(); ++i) {
    if (!vis[i]) {
      double t = walk + dist(h, v[i]);
      if (t < dist(d, v[i])) {
        vis[i] = true;
        if (bt(v, t, v[i], d))
          return true;
        vis[i] = false;
      } else {
        return false;
      }
    }
  }
  return false;
}
int main() {
  while (true) {
    bool bImpossible = false;
    int n, hx, hy, dx, dy;
    point hero, daemon;
    vector<point> vp;

    scanf("%d%d%d%d%d", &n, &hx, &hy, &dx, &dy);
    if (!(n || hx || hy || dx || dy))
      break;

    hero = complex<elem>(hx, hy);
    daemon = complex<elem>(dx, dy);

    for (int i = 0; i < n; ++i) {
      int cx, cy;
      scanf("%d%d", &cx, &cy);
      point p(cx, cy);
      if (cx == hx && cy == hy)
        continue;
      vp.push_back(p);
      if (dist(daemon, p) <= dist(hero, p))
        bImpossible = true;
      vis[i] = false;
    }

    if (!bImpossible && bt(vp, 0.0, hero, daemon))
      printf("YES\\n");
    else
      printf("NO\\n");
  }
  return 0;
}