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

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

/*
 4
 0 1 2 3
 5
 ????????????????±????????????§????????????????¨?????????§????????????
 */

bitset<4> cw(const bitset<4> &b) {
  bitset<4> nb(4);
  rep(i, 4) nb[(i + 1) % 4] = b[i];
  return nb;
}

bitset<4> ccw(const bitset<4> &b) {
  bitset<4> nb(4);
  rep(i, 4) nb[(i + 3) % 4] = b[i];
  return nb;
}

struct state {
  vector<bitset<4>> b;
  state() { b = vector<bitset<4>>(6, bitset<4>(false)); }
  state(const state &s) {
    b = s.b; // copy
  }
  state(int hash) {
    b = vector<bitset<4>>(6, bitset<4>(false));
    rep(i, 6) rep(j, 4) {
      b[5 - i][3 - j] = hash % 2;
      hash /= 2;
    }
  }
  int hash() {
    int h = 0;
    rep(i, 6) rep(j, 4) h = (h << 1) + b[i][j];
    return h;
  }
  bitset<4> &operator[](int i) { return b[i]; }

  void front() { // ???????????????
    vector<bitset<4>> nb(6);
    nb[0] = b[4];
    nb[1] = ccw(b[1]);
    nb[2] = cw(cw(b[5]));
    nb[3] = cw(b[3]);
    nb[4] = cw(cw(b[2]));
    nb[5] = b[0];
    swap(nb, b);
  }
  void right() {
    vector<bitset<4>> nb(6);
    nb[0] = cw(b[0]);
    nb[1] = cw(b[4]);
    nb[2] = ccw(b[2]);
    nb[3] = cw(b[5]);
    nb[4] = cw(b[3]);
    nb[5] = cw(b[1]);
    swap(nb, b);
  }

  bool ok() { return b[0][2] & b[2][2]; }
};

int main() {
  while (true) {
    state st;
    rep(i, 6) {
      string t[3];
      rep(j, 3) {
        cin >> t[j];
        if (t[0] == "#")
          return 0;
      }
      st[i][0] = t[0][0] == '*' || t[0][1] == '*' || t[0][2] == '*';
      st[i][1] = t[0][2] == '*' || t[1][2] == '*' || t[2][2] == '*';
      st[i][2] = t[2][0] == '*' || t[2][1] == '*' || t[2][2] == '*';
      st[i][3] = t[0][0] == '*' || t[1][0] == '*' || t[2][0] == '*';
    }

    map<int, int> d;
    queue<int> q;
    q.push(st.hash());
    d[st.hash()] = 0;
    int ans;
    bool flg = true;
    while (flg) {
      assert(!q.empty());
      int h = q.front();
      q.pop();
      state s(h);
      if (s.ok()) {
        ans = d[h];
        break;
      }
      int nd = d[h] + 1;
      auto exec = [&](state &S) {
        if (S.ok()) {
          ans = nd;
          flg = false;
          return;
        }
        int hs = S.hash();
        if (d.count(hs) == 0) {
          q.push(hs);
          d[hs] = nd;
        }
      };

      state s1(s);
      s1.right();
      exec(s1);
      s1.right();
      s1.right();
      exec(s1);
      s1.right();
      s1.front();
      exec(s1);
      s1.front();
      s1.front();
      exec(s1);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

/*
 4
 0 1 2 3
 5
 ????????????????±????????????§????????????????¨?????????§????????????
 */

bitset<4> cw(const bitset<4> &b) {
  bitset<4> nb(4);
  rep(i, 4) nb[(i + 1) % 4] = b[i];
  return nb;
}

bitset<4> ccw(const bitset<4> &b) {
  bitset<4> nb(4);
  rep(i, 4) nb[(i + 3) % 4] = b[i];
  return nb;
}

struct state {
  vector<bitset<4>> b;
  state() { b = vector<bitset<4>>(6, bitset<4>(false)); }
  state(const state &s) {
    b = s.b; // copy
  }
  state(int hash) {
    b = vector<bitset<4>>(6, bitset<4>(false));
    rep(i, 6) rep(j, 4) {
      b[5 - i][3 - j] = hash % 2;
      hash /= 2;
    }
  }
  int hash() {
    int h = 0;
    rep(i, 6) rep(j, 4) h = (h << 1) + b[i][j];
    return h;
  }
  bitset<4> &operator[](int i) { return b[i]; }

  void front() { // ???????????????
    vector<bitset<4>> nb(6);
    nb[0] = b[4];
    nb[1] = ccw(b[1]);
    nb[2] = cw(cw(b[5]));
    nb[3] = cw(b[3]);
    nb[4] = cw(cw(b[2]));
    nb[5] = b[0];
    swap(nb, b);
  }
  void right() {
    vector<bitset<4>> nb(6);
    nb[0] = cw(b[0]);
    nb[1] = cw(b[4]);
    nb[2] = ccw(b[2]);
    nb[3] = cw(b[5]);
    nb[4] = cw(b[3]);
    nb[5] = cw(b[1]);
    swap(nb, b);
  }

  bool ok() { return b[0][2] & b[2][2]; }
};

int d[1 << 24];

int main() {
  while (true) {
    state st;
    rep(i, 6) {
      string t[3];
      rep(j, 3) {
        cin >> t[j];
        if (t[0] == "#")
          return 0;
      }
      st[i][0] = t[0][0] == '*' || t[0][1] == '*' || t[0][2] == '*';
      st[i][1] = t[0][2] == '*' || t[1][2] == '*' || t[2][2] == '*';
      st[i][2] = t[2][0] == '*' || t[2][1] == '*' || t[2][2] == '*';
      st[i][3] = t[0][0] == '*' || t[1][0] == '*' || t[2][0] == '*';
    }

    fill(d, d + (1 << 24), INF);
    queue<int> q;
    q.push(st.hash());
    d[st.hash()] = 0;
    int ans;
    bool flg = true;
    while (flg) {
      assert(!q.empty());
      int h = q.front();
      q.pop();
      state s(h);
      if (s.ok()) {
        ans = d[h];
        break;
      }
      int nd = d[h] + 1;
      auto exec = [&](state &S) {
        if (S.ok()) {
          ans = nd;
          flg = false;
          return;
        }
        int hs = S.hash();
        if (d[hs] > nd) {
          q.push(hs);
          d[hs] = nd;
        }
      };

      state s1(s);
      s1.right();
      exec(s1);
      s1.right();
      s1.right();
      exec(s1);
      s1.right();
      s1.front();
      exec(s1);
      s1.front();
      s1.front();
      exec(s1);
    }
    cout << ans << endl;
  }

  return 0;
}