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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define USE_MATH_DEFINES
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

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

int sz;
map<vector<P>, int> memo;
map<vector<P>, vector<vector<P>>> pat;
bool rocks[200][200];

bool ok(vector<P> s) {
  for (int i = 0; i < (int)s.size(); i++) {
    if (rocks[s[i].first + 100][s[i].second + 100])
      return false;
  }
  return true;
}

bool touch(const P p1, const P p2) {
  int a = p1.first - p2.first;
  int b = p1.second - p2.second;
  if ((a == 0 && b == 0) || (a == 1 && b == -1) || (a == 1 && b == 0) ||
      (a == 0 && b == 1) || (a == 0 && b == -1) || (a == -1 && b == 0) ||
      (a == -1 && b == 1))
    return true;
  else
    return false;
}

bool touch2(const P p1, const P p2) {
  int a = p1.first - p2.first;
  int b = p1.second - p2.second;
  if ((a == 1 && b == -1) || (a == 1 && b == 0) || (a == 0 && b == 1) ||
      (a == 0 && b == -1) || (a == -1 && b == 0) || (a == -1 && b == 1))
    return true;
  else
    return false;
}

bool valid(const vector<P> &s, int n) {
  for (int i = n - 2; i >= 0; i--) {
    if (touch(s[i], s[n]))
      return false;
  }
  return true;
}

void make_pat(const vector<P> &s, vector<P> pattern, int n) {
  if (n >= sz) {
    pat[s].push_back(pattern);
  } else {
    if (valid(pattern, n))
      make_pat(s, pattern, n + 1);
    for (int i = 0; i < 6; i++) {
      pattern[n].first = s[n].first + dx[i];
      pattern[n].second = s[n].second + dy[i];
      if (n == sz - 1) {
        if (valid(pattern, n) && touch2(pattern[n], s[n - 1]))
          make_pat(s, pattern, n + 1);
      } else if (n == 0) {
        if (touch2(pattern[0], s[n + 1]))
          make_pat(s, pattern, n + 2);
      } else {
        if (valid(pattern, n) && touch2(pattern[n], s[n - 1]) &&
            touch2(pattern[n], s[n + 1])) {
          make_pat(s, pattern, n + 2);
        }
      }
    }
  }
}

void generate(int n, vector<P> &s) {
  if (n == sz) {
    make_pat(s, s, 0);
  } else {
    for (int i = 0; i < 6; i++) {
      s[n].first = s[n - 1].first + dx[i];
      s[n].second = s[n - 1].second + dy[i];
      if (valid(s, n))
        generate(n + 1, s);
    }
  }
}

int main() {
  int n, k;
  int dis;
  int x, y, X, Y;
  while (cin >> n && n) {
    memo.clear();
    pat.clear();
    memset(rocks, false, sizeof(rocks));
    sz = n;
    vector<P> s(n);
    s[0].first = 0;
    s[0].second = 0;
    generate(1, s);

    for (int i = 0; i < n; i++) {
      cin >> s[i].first >> s[i].second;
    }
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> x >> y;
      x -= s[0].first;
      y -= s[0].second;
      if (abs(x) < 100 && abs(y) < 100)
        rocks[x + 100][y + 100] = true;
    }
    cin >> X >> Y;
    X -= s[0].first;
    Y -= s[0].second;
    for (int i = n - 1; i >= 0; i--) {
      s[i].first -= s[0].first;
      s[i].second -= s[0].second;
    }

    memo[s] = 1;
    queue<vector<P>> que;
    que.push(s);
    dis = abs(s[0].first - X) + abs(s[0].second - Y);
    while (!que.empty()) {
      vector<P> sw = que.front();
      s = sw;
      for (int i = n - 1; i >= 0; i--) {
        s[i].first -= s[0].first;
        s[i].second -= s[0].second;
      }
      que.pop();
      //  cout << sw[0].first << " " << sw[0].second << " " << X << " " << Y <<
      //  endl;
      if (sw[0].first == X && sw[0].second == Y) {
        cout << memo[sw] - 1 << endl;
        /*for(int i = 0; i < (int)sw.size(); i++){
          cout << sw[i].first << " " << sw[i].second << endl;
          }*/
        break;
      }
      int d = abs(sw[0].first - X) + abs(sw[0].second - Y);
      if (d > dis + 7)
        continue;
      dis = min(dis, d);
      vector<vector<P>> vvp = pat[s];
      for (int i = 0; i < (int)vvp.size(); i++) {
        for (int j = 0; j < (int)vvp[i].size(); j++) {
          vvp[i][j].first += sw[0].first;
          vvp[i][j].second += sw[0].second;
        }
        if (ok(vvp[i]) && memo[vvp[i]] == 0) {
          memo[vvp[i]] = memo[sw] + 1;
          que.push(vvp[i]);
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define USE_MATH_DEFINES
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

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

int sz;
map<vector<P>, int> memo;
map<vector<P>, vector<vector<P>>> pat;
bool rocks[200][200];

bool ok(vector<P> s) {
  for (int i = 0; i < (int)s.size(); i++) {
    if (rocks[s[i].first + 100][s[i].second + 100])
      return false;
  }
  return true;
}

bool touch(const P p1, const P p2) {
  int a = p1.first - p2.first;
  int b = p1.second - p2.second;
  if ((a == 0 && b == 0) || (a == 1 && b == -1) || (a == 1 && b == 0) ||
      (a == 0 && b == 1) || (a == 0 && b == -1) || (a == -1 && b == 0) ||
      (a == -1 && b == 1))
    return true;
  else
    return false;
}

bool touch2(const P p1, const P p2) {
  int a = p1.first - p2.first;
  int b = p1.second - p2.second;
  if ((a == 1 && b == -1) || (a == 1 && b == 0) || (a == 0 && b == 1) ||
      (a == 0 && b == -1) || (a == -1 && b == 0) || (a == -1 && b == 1))
    return true;
  else
    return false;
}

bool valid(const vector<P> &s, int n) {
  for (int i = n - 2; i >= 0; i--) {
    if (touch(s[i], s[n]))
      return false;
  }
  return true;
}

void make_pat(const vector<P> &s, vector<P> pattern, int n) {
  if (n >= sz) {
    pat[s].push_back(pattern);
  } else {
    make_pat(s, pattern, n + 1);
    for (int i = 0; i < 6; i++) {
      pattern[n].first = s[n].first + dx[i];
      pattern[n].second = s[n].second + dy[i];
      if (n == sz - 1) {
        if (valid(pattern, n) && touch2(pattern[n], s[n - 1]))
          make_pat(s, pattern, n + 1);
      } else if (n == 0) {
        if (touch2(pattern[0], s[n + 1]))
          make_pat(s, pattern, n + 2);
      } else {
        if (valid(pattern, n) && touch2(pattern[n], s[n - 1]) &&
            touch2(pattern[n], s[n + 1])) {
          make_pat(s, pattern, n + 2);
        }
      }
    }
  }
}

void generate(int n, vector<P> &s) {
  if (n == sz) {
    make_pat(s, s, 0);
  } else {
    for (int i = 0; i < 6; i++) {
      s[n].first = s[n - 1].first + dx[i];
      s[n].second = s[n - 1].second + dy[i];
      if (valid(s, n))
        generate(n + 1, s);
    }
  }
}

int main() {
  int n, k;
  int x, y, X, Y;
  while (cin >> n && n) {
    memo.clear();
    pat.clear();
    memset(rocks, false, sizeof(rocks));
    sz = n;
    vector<P> s(n);
    s[0].first = 0;
    s[0].second = 0;
    generate(1, s);

    for (int i = 0; i < n; i++) {
      cin >> s[i].first >> s[i].second;
    }
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> x >> y;
      x -= s[0].first;
      y -= s[0].second;
      if (abs(x) < 100 && abs(y) < 100)
        rocks[x + 100][y + 100] = true;
    }
    cin >> X >> Y;
    X -= s[0].first;
    Y -= s[0].second;
    for (int i = n - 1; i >= 0; i--) {
      s[i].first -= s[0].first;
      s[i].second -= s[0].second;
    }

    memo[s] = 1;
    queue<vector<P>> que;
    que.push(s);
    while (!que.empty()) {
      vector<P> sw = que.front();
      s = sw;
      for (int i = n - 1; i >= 0; i--) {
        s[i].first -= s[0].first;
        s[i].second -= s[0].second;
      }
      que.pop();
      //  cout << sw[0].first << " " << sw[0].second << " " << X << " " << Y <<
      //  endl;
      if (sw[0].first == X && sw[0].second == Y) {
        cout << memo[sw] - 1 << endl;
        break;
      }
      vector<vector<P>> vvp = pat[s];
      for (int i = 0; i < (int)vvp.size(); i++) {
        for (int j = 0; j < (int)vvp[i].size(); j++) {
          vvp[i][j].first += sw[0].first;
          vvp[i][j].second += sw[0].second;
        }
        if (ok(vvp[i]) && memo[vvp[i]] == 0) {
          memo[vvp[i]] = memo[sw] + 1;
          que.push(vvp[i]);
        }
      }
    }
  }
  return 0;
}