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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <utility>
// #include <memory>
// #include <functional>
// #include <deque>
// #include <cctype>
// #include <ctime>
// #include <numeric>
// #include <list>
// #include <iomanip>

// #if __cplusplus >= 201103L
// #include <array>
// #include <tuple>
// #include <initializer_list>
// #include <forward_list>
//
// #define cauto const auto&
// #else

// #endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF = 1 << 24;
int dx[] = {0, 1, 1, 0, -1, -1, 0};
int dy[] = {-1, -1, 0, 1, 1, 0, 0};
int n;
queue<vector<pii>> tmp;
vector<pii> t;
pii goal;
set<pii> rock;
typedef unsigned long long ull;
set<ull> used;
ull seed = 1145148931919;
// false conect : true no
bool check2(pii &a, pii &b) {
  if (a == b)
    return false;
  if (a.F == b.F && abs(a.S - b.S) == 1)
    return false;
  if (a.S == b.S && abs(a.F - b.F) == 1)
    return false;
  if (a.F + 1 == b.F && a.S - 1 == b.S)
    return false;
  if (a.F - 1 == b.F && a.S + 1 == b.S)
    return false;
  return true;
}
bool check3(pii &a, pii &b) {
  if (a == b)
    return false;
  if (a.F == b.F && abs(a.S - b.S) == 1)
    return true;
  if (a.S == b.S && abs(a.F - b.F) == 1)
    return true;
  if (a.F + 1 == b.F && a.S - 1 == b.S)
    return true;
  if (a.F - 1 == b.F && a.S + 1 == b.S)
    return true;
  return false;
}
bool check(vector<pii> &a, int b) {
  rep(i, b - 1) {
    if (!check2(a[i], a[b]))
      return false;
  }
  if (b) {
    if (!check3(a[b], a[b - 1]))
      return false;
  }
  if (b != n - 1) {
    if (!check3(a[b], a[b + 1]))
      return false;
  }
  // cout<<"ggg"<<endl;
  if (rock.find(a[b]) != rock.end())
    return false;
  return true;
}
int dist(pii &a, pii &b) { return abs(a.F - b.F) + abs(a.S - b.S); }
ull has(vector<pii> &a) {
  ull ret = 0;
  ull t = 1;
  rep(i, a.size()) {
    ret += a[i].F * t;
    t *= seed;
    ret += a[i].S * t;
    t *= seed;
  }
  return ret;
}
void foo(int a, int b, vector<pii> &c, int d) {
  if (a == n) {
    if (d)
      tmp.push(c);
    return;
  }
  if (b == 0) {
    if (check(c, a))
      foo(a + 1, 1, c, d);
    return;
  }
  if (check(c, a))
    foo(a + 1, 1, c, d);
  rep(i, 6) {
    c[a].F += dx[i];
    c[a].S += dy[i];
    if (check(c, a))
      foo(a + 1, 0, c, d + 1);
    c[a].F -= dx[i];
    c[a].S -= dy[i];
  }
  return;
}
void mainmain() {
  pii a(2, 0), b(3, -1);
  n = 4;
  vector<pii> v(4);
  v[0] = pii(3, -1);
  v[1] = pii(2, -1);
  v[2] = pii(2, 0);
  v[3] = pii(3, 0);
  // if(check(v,0)) cout<<0<<endl;
  // if(check(v,1)) cout<<1<<endl;
  // if(check(v,2)) cout<<2<<endl;
  if (check(v, 3))
    cout << 3 << endl;
  // if(check3(a,b)) cout<<"Yes"<<endl;
  // else cout<<"No"<<endl;
  while (cin >> n) {
    if (n == 0)
      return;
    vector<pii> in(n);
    rep(i, n) cin >> in[i].F >> in[i].S;
    int K;
    cin >> K;
    rock.clear();
    used.clear();
    rep(i, K) {
      int a, b;
      cin >> a >> b;
      rock.insert(pii(a, b));
    }
    cin >> goal.F >> goal.S;
    queue<vector<pii>> q;
    used.insert(has(in));
    q.push(in);
    int ans = 1;
    bool f = false;
    while (1) {
      int qs = q.size();
      // dump(ans);
      rep(i, qs) {
        vector<pii> top = q.front();
        // rep(i,top.size()){
        //     cout<<top[i].F<<" "<<top[i].S<<endl;
        // }cout<<endl;
        q.pop();
        foo(0, 1, top, 0);
      }
      vector<vector<pii>> vv;
      while (!tmp.empty()) {
        if (tmp.front()[0] == goal) {
          f = true;
          auto tt = tmp.front();
          // cout<<"find"<<endl;
          // rep(i,tt.size()){
          //     cout<<tt[i].F<<" "<<tt[i].S<<endl;
          // }cout<<endl;
        }
        vv.PB(tmp.front());
        tmp.pop();
      }
      sort(ALL(vv));
      vv.erase(unique(ALL(vv)), vv.end());
      rep(i, vv.size()) {
        if (dist(vv[i][0], goal) > 22 - ans)
          continue;
        if (used.find(has(vv[i])) == used.end())
          q.push(vv[i]);
        used.insert(has(vv[i]));
      }
      if (f)
        break;
      ans++;
      if (ans >= 21)
        break;
    }
    cout << ans << endl;
  }
}

signed main() { mainmain(); }

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <utility>
// #include <memory>
// #include <functional>
// #include <deque>
// #include <cctype>
// #include <ctime>
// #include <numeric>
// #include <list>
// #include <iomanip>

// #if __cplusplus >= 201103L
// #include <array>
// #include <tuple>
// #include <initializer_list>
// #include <forward_list>
//
// #define cauto const auto&
// #else

// #endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF = 1 << 24;
int dx[] = {0, 1, 1, 0, -1, -1, 0};
int dy[] = {-1, -1, 0, 1, 1, 0, 0};
int n;
queue<vector<pii>> tmp;
vector<pii> t;
pii goal;
set<pii> rock;
typedef unsigned long long ull;
set<ull> used;
ull seed = 1145148931919;
// false conect : true no
bool check2(pii &a, pii &b) {
  if (a == b)
    return false;
  if (a.F == b.F && abs(a.S - b.S) == 1)
    return false;
  if (a.S == b.S && abs(a.F - b.F) == 1)
    return false;
  if (a.F + 1 == b.F && a.S - 1 == b.S)
    return false;
  if (a.F - 1 == b.F && a.S + 1 == b.S)
    return false;
  return true;
}
bool check3(pii &a, pii &b) {
  if (a == b)
    return false;
  if (a.F == b.F && abs(a.S - b.S) == 1)
    return true;
  if (a.S == b.S && abs(a.F - b.F) == 1)
    return true;
  if (a.F + 1 == b.F && a.S - 1 == b.S)
    return true;
  if (a.F - 1 == b.F && a.S + 1 == b.S)
    return true;
  return false;
}
bool check(vector<pii> &a, int b) {
  rep(i, b - 1) {
    if (!check2(a[i], a[b]))
      return false;
  }
  if (b) {
    if (!check3(a[b], a[b - 1]))
      return false;
  }
  if (b != n - 1) {
    if (!check3(a[b], a[b + 1]))
      return false;
  }
  // cout<<"ggg"<<endl;
  if (rock.find(a[b]) != rock.end())
    return false;
  return true;
}
ull has(vector<pii> &a) {
  ull ret = 0;
  ull t = 1;
  rep(i, a.size()) {
    ret += a[i].F * t;
    t *= seed;
    ret += a[i].S * t;
    t *= seed;
  }
  return ret;
}
void foo(int a, int b, vector<pii> &c, int d) {
  if (a == n) {
    if (d)
      tmp.push(c);
    return;
  }
  if (b == 0) {
    if (check(c, a))
      foo(a + 1, 1, c, d);
    return;
  }
  if (check(c, a))
    foo(a + 1, 1, c, d);
  rep(i, 6) {
    c[a].F += dx[i];
    c[a].S += dy[i];
    if (check(c, a))
      foo(a + 1, 0, c, d + 1);
    c[a].F -= dx[i];
    c[a].S -= dy[i];
  }
  return;
}
void mainmain() {
  pii a(2, 0), b(3, -1);
  n = 4;
  vector<pii> v(4);
  v[0] = pii(3, -1);
  v[1] = pii(2, -1);
  v[2] = pii(2, 0);
  v[3] = pii(3, 0);
  // if(check(v,0)) cout<<0<<endl;
  // if(check(v,1)) cout<<1<<endl;
  // if(check(v,2)) cout<<2<<endl;
  if (check(v, 3))
    cout << 3 << endl;
  // if(check3(a,b)) cout<<"Yes"<<endl;
  // else cout<<"No"<<endl;
  while (cin >> n) {
    if (n == 0)
      return;
    vector<pii> in(n);
    rep(i, n) cin >> in[i].F >> in[i].S;
    int K;
    cin >> K;
    rock.clear();
    used.clear();
    rep(i, K) {
      int a, b;
      cin >> a >> b;
      rock.insert(pii(a, b));
    }
    cin >> goal.F >> goal.S;
    queue<vector<pii>> q;
    used.insert(has(in));
    q.push(in);
    int ans = 1;
    bool f = false;
    while (1) {
      int qs = q.size();
      // dump(ans);
      rep(i, qs) {
        vector<pii> top = q.front();
        // rep(i,top.size()){
        //     cout<<top[i].F<<" "<<top[i].S<<endl;
        // }cout<<endl;
        q.pop();
        foo(0, 1, top, 0);
      }
      vector<vector<pii>> vv;
      while (!tmp.empty()) {
        if (tmp.front()[0] == goal) {
          f = true;
          auto tt = tmp.front();
          // cout<<"find"<<endl;
          // rep(i,tt.size()){
          //     cout<<tt[i].F<<" "<<tt[i].S<<endl;
          // }cout<<endl;
        }
        vv.PB(tmp.front());
        tmp.pop();
      }
      sort(ALL(vv));
      vv.erase(unique(ALL(vv)), vv.end());
      rep(i, vv.size()) {
        if (used.find(has(vv[i])) == used.end())
          q.push(vv[i]);
        used.insert(has(vv[i]));
      }
      if (f)
        break;
      ans++;
      if (ans >= 21)
        break;
    }
    cout << ans << endl;
  }
}

signed main() { mainmain(); }