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

#include <bits/stdc++.h>
#define N 16
using namespace std;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }

const int TOP = 0, FRONT = 1, LEFT = 2, RIGHT = 3, BACK = 4, BOTTOM = 5;
class Dice {
public:
  int val[6];
  Dice() {
    for (int i = 0; i < 6; i++)
      val[i] = i;
  }
  Dice(int val[6]) {
    for (int i = 0; i < 6; i++)
      this->val[i] = val[i];
  }
  Dice(int l, int r, int f, int b, int d, int u) {
    val[LEFT] = l, val[RIGHT] = r;
    val[FRONT] = f, val[BACK] = b;
    val[BOTTOM] = d, val[TOP] = u;
  }
  void rot(string s) {
    if (s == "N" || s == "B")
      rotN();
    else if (s == "S" || s == "F")
      rotS();
    else if (s == "E" || s == "R")
      rotE();
    else if (s == "W" || s == "L")
      rotW();
    else if (s == "CW")
      rotCW();
    else if (s == "CCW")
      rotCCW();
    else
      assert(!"rot error");
  }

  //?\\\\?????¢??????
  void rotN() {
    swap(val[TOP], val[FRONT]);
    swap(val[FRONT], val[BOTTOM]);
    swap(val[BOTTOM], val[BACK]);
  }

  //???????????¢??????
  void rotS() {
    // rotN(),rotN(),rotN();
    swap(val[TOP], val[BACK]);
    swap(val[BACK], val[BOTTOM]);
    swap(val[BOTTOM], val[FRONT]);
  }

  //????????¢??????
  void rotE() {
    swap(val[TOP], val[LEFT]);
    swap(val[LEFT], val[BOTTOM]);
    swap(val[BOTTOM], val[RIGHT]);
  }

  //????????¢??????
  void rotW() {
    // rotE(),rotE(),rotE();
    swap(val[TOP], val[RIGHT]);
    swap(val[RIGHT], val[BOTTOM]);
    swap(val[BOTTOM], val[LEFT]);
  }

  //???????????????????¨????????????????
  void rotCW() {
    swap(val[FRONT], val[RIGHT]);
    swap(val[RIGHT], val[BACK]);
    swap(val[BACK], val[LEFT]);
  }

  //??????????????????????¨???????????????????
  void rotCCW() {
    // rotCW(),rotCW(),rotCW();
    swap(val[FRONT], val[LEFT]);
    swap(val[LEFT], val[BACK]);
    swap(val[BACK], val[RIGHT]);
  }
};

int n;
struct dat {
  int x, y;
  Dice d;
};
typedef pair<short, short> P;
typedef map<P, int> M;
typedef set<P> S;

M dice[N];
M getMove(int x, int y, Dice d, string rot) {
  static int dx[1 << 8] = {}, dy[1 << 8] = {};
  dx['L'] = -1, dx['R'] = 1, dy['F'] = -1, dy['B'] = 1;
  M res;
  res[P(x, y)] = d.val[BOTTOM];
  for (char ch : rot) {
    x += dx[(int)ch], y += dy[(int)ch];
    d.rot(string(1, ch));
    res[P(x, y)] = d.val[BOTTOM];
  }
  return res;
}

int getScore(int idx, S &used) {
  int res = 0;
  for (pair<P, int> const &p : dice[idx])
    if (!used.count(p.first))
      res += p.second;
  return res;
}

S getUsed(int idx, S used) {
  for (pair<P, int> const &p : dice[idx])
    used.insert(p.first);
  return used;
}

int DP() {
  S used[1 << N];
  int dp[1 << N] = {};
  for (int bit = 0; bit < (1 << n); bit++)
    for (int i = 0; i < n; i++) {
      if (bit >> i & 1)
        continue;
      int nbit = bit | (1 << i);
      int nscore = dp[bit] + getScore(i, used[bit]);
      Max(dp[nbit], nscore);
      if (used[nbit].empty())
        used[nbit] = getUsed(i, used[bit]);
    }
  return dp[(1 << n) - 1];
}

signed main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      int x, y;
      int l, r, f, b, d, u;
      string rot;
      cin >> x >> y >> l >> r >> f >> b >> d >> u >> rot;
      dice[i] = getMove(x, y, Dice(l, r, f, b, d, u), rot);
    }
    cout << DP() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define N 16
using namespace std;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }

const int TOP = 0, FRONT = 1, LEFT = 2, RIGHT = 3, BACK = 4, BOTTOM = 5;
class Dice {
public:
  int val[6];
  Dice() {
    for (int i = 0; i < 6; i++)
      val[i] = i;
  }
  Dice(int val[6]) {
    for (int i = 0; i < 6; i++)
      this->val[i] = val[i];
  }
  Dice(int l, int r, int f, int b, int d, int u) {
    val[LEFT] = l, val[RIGHT] = r;
    val[FRONT] = f, val[BACK] = b;
    val[BOTTOM] = d, val[TOP] = u;
  }
  void rot(string s) {
    if (s == "N" || s == "B")
      rotN();
    else if (s == "S" || s == "F")
      rotS();
    else if (s == "E" || s == "R")
      rotE();
    else if (s == "W" || s == "L")
      rotW();
    else if (s == "CW")
      rotCW();
    else if (s == "CCW")
      rotCCW();
    else
      assert(!"rot error");
  }

  //?\\\\?????¢??????
  void rotN() {
    swap(val[TOP], val[FRONT]);
    swap(val[FRONT], val[BOTTOM]);
    swap(val[BOTTOM], val[BACK]);
  }

  //???????????¢??????
  void rotS() {
    // rotN(),rotN(),rotN();
    swap(val[TOP], val[BACK]);
    swap(val[BACK], val[BOTTOM]);
    swap(val[BOTTOM], val[FRONT]);
  }

  //????????¢??????
  void rotE() {
    swap(val[TOP], val[LEFT]);
    swap(val[LEFT], val[BOTTOM]);
    swap(val[BOTTOM], val[RIGHT]);
  }

  //????????¢??????
  void rotW() {
    // rotE(),rotE(),rotE();
    swap(val[TOP], val[RIGHT]);
    swap(val[RIGHT], val[BOTTOM]);
    swap(val[BOTTOM], val[LEFT]);
  }

  //???????????????????¨????????????????
  void rotCW() {
    swap(val[FRONT], val[RIGHT]);
    swap(val[RIGHT], val[BACK]);
    swap(val[BACK], val[LEFT]);
  }

  //??????????????????????¨???????????????????
  void rotCCW() {
    // rotCW(),rotCW(),rotCW();
    swap(val[FRONT], val[LEFT]);
    swap(val[LEFT], val[BACK]);
    swap(val[BACK], val[RIGHT]);
  }
};

int n;
struct dat {
  int x, y;
  Dice d;
};
dat A[N];
string rot[N];
typedef pair<int, int> P;
typedef map<P, int> M;
M dice[N];

int getScore(M &mp) {
  int res = 0;
  for (pair<P, int> p : mp)
    res += p.second;
  return res;
}

int dx[1 << 8] = {}, dy[1 << 8] = {};
M getNx(int idx, M &mp, int &score, bool flg = 0) {
  M tmp;
  for (pair<P, int> const &p : dice[idx]) {
    tmp[p.first] = p.second;
    score += p.second - mp[p.first];
    mp[p.first] = p.second;
  }
  M res = flg ? mp : M();
  for (pair<P, int> const &p : tmp)
    mp[p.first] = p.second;
  return res;
}

int DP() {
  M dp[1 << N];
  int score[1 << N] = {};
  for (int bit; bit < (1 << n); bit++) {
    for (int i = 0; i < n; i++) {
      if (bit >> i & 1)
        continue;
      int nbit = bit | (1 << i);
      int nscore = score[bit];
      getNx(i, dp[bit], nscore);
      if (score[nbit] < nscore)
        score[nbit] = nscore, dp[nbit] = getNx(i, dp[bit], nscore, 1);
    }
    dp[bit].clear();
  }
  return score[(1 << n) - 1];
}

M getMove(int x, int y, Dice d, string rot) {
  M res;
  res[P(x, y)] = d.val[BOTTOM];
  for (char ch : rot) {
    x += dx[(int)ch], y += dy[(int)ch];
    d.rot(string(1, ch));
    res[P(x, y)] = d.val[BOTTOM];
  }
  return res;
}

signed main() {
  dx['L'] = -1, dx['R'] = 1;
  dy['F'] = -1, dy['B'] = 1;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      int x, y;
      int l, r, f, b, d, u;
      cin >> x >> y >> l >> r >> f >> b >> d >> u >> rot[i];
      A[i] = (dat){x, y, Dice(l, r, f, b, d, u)};
      dice[i] = getMove(x, y, Dice(l, r, f, b, d, u), rot[i]);
    }
    cout << DP() << endl;
  }
  return 0;
}