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

#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define vec vector
#define pb push_back

typedef struct {
  int l, r, f, b, d, u;
} dice;

dice rotL(dice d) {
  dice n = d;
  n.l = d.u;
  n.u = d.r;
  n.r = d.d;
  n.d = d.l;
  return n;
}

dice rotR(dice d) {
  dice n = d;
  n.u = d.l;
  n.r = d.u;
  n.d = d.r;
  n.l = d.d;
  return n;
}

dice rotF(dice d) {
  dice n = d;
  n.f = d.u;
  n.u = d.b;
  n.b = d.d;
  n.d = d.f;
  return n;
}

dice rotB(dice d) {
  dice n = d;
  n.b = d.u;
  n.u = d.f;
  n.f = d.d;
  n.d = d.b;
  return n;
}

typedef struct {
  int x, y;
  int num;
} masu;

pair<int, int> SUM = make_pair(2000, 2000);

map<pair<int, int>, int> Max(map<pair<int, int>, int> a,
                             map<pair<int, int>, int> b) {
  if (a[SUM] > b[SUM])
    return a;
  else
    return b;
}

map<pair<int, int>, int> stamp(map<pair<int, int>, int> p, vec<masu> s) {
  rep(i, s.size()) {
    if (p.find(make_pair(s[i].x, s[i].y)) != p.end())
      continue;
    p[make_pair(s[i].x, s[i].y)] = s[i].num;
    p[SUM] += s[i].num;
  }
  return p;
}

int main(void) {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;

    vec<vec<masu>> stamps(n);
    rep(t, n) {
      int x, y;
      cin >> x >> y;
      dice d;
      cin >> d.l >> d.r >> d.f >> d.b >> d.d >> d.u;
      string rot;
      cin >> rot;
      map<pair<int, int>, int> dp;

      masu s;
      s.x = x;
      s.y = y;
      s.num = d.d;
      stamps[t].pb(s);
      dp[make_pair(x, y)] = 0;
      rep(i, rot.length()) {
        if (rot[i] == 'L') {
          x--;
          d = rotL(d);
        } else if (rot[i] == 'R') {
          x++;
          d = rotR(d);
        } else if (rot[i] == 'F') {
          y--;
          d = rotF(d);
        } else {
          y++;
          d = rotB(d);
        }
        s.x = x;
        s.y = y;
        s.num = d.d;
        if (dp.find(make_pair(x, y)) == dp.end()) {
          stamps[t].pb(s);
          dp[make_pair(x, y)] = stamps[t].size() - 1;
        } else {
          stamps[t][dp[make_pair(x, y)]] = s;
        }
      }
    }

    // vec<map<pair<int, int>, int> > dp(1 << n);
    // dp[0][SUM] = 0;
    // vec<vec<vec<int> > > dp(1 << n, vec<vec<int> >(4001, vec<int>(4001, 0)));
    // dp[0][4000][4000] = 0;
    /*
    rep(i, (1 << n)) {
      rep(j, n) {
        if ((i >> j) % 2) continue;
        dp[i + (1 << j)] = Max(dp[i + (1 << j)], stamp(dp[i], stamps[j]));
      }
    }
    */
    /*
    int sum = 0;
    for (auto it = dp[(1 << n) - 1].begin(); it != dp[(1 << n) - 1].end(); it++)
    sum += it->second; cout << sum << endl;
    */
    // cout << dp[(1 << n) - 1][SUM] << endl;
    // cout << dp[(1 << n) - 1][4000][4000] << endl;

    vec<int> dp(1 << n, 0);
    rep(i, (1 << n)) {
      map<pair<int, int>, int> m;
      rep(j, n) {
        if ((i >> j) % 2 == 0)
          continue;
        rep(k, stamps[j].size()) {
          m[make_pair(stamps[j][k].x, stamps[j][k].y)] = 1;
        }
      }

      rep(j, n) {
        if ((i >> j) % 2)
          continue;
        int cost = 0;
        rep(k, stamps[j].size()) {
          pair<int, int> p = make_pair(stamps[j][k].x, stamps[j][k].y);
          if (m[p])
            continue;
          cost += stamps[j][k].num;
        }
        if (dp[i + (1 << j)] < dp[i] + cost)
          dp[i + (1 << j)] = dp[i] + cost;
      }
    }
    cout << dp[(1 << n) - 1] << endl;
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define vec vector
#define pb push_back

typedef struct {
  int l, r, f, b, d, u;
} dice;

dice rotL(dice d) {
  dice n = d;
  n.l = d.u;
  n.u = d.r;
  n.r = d.d;
  n.d = d.l;
  return n;
}

dice rotR(dice d) {
  dice n = d;
  n.u = d.l;
  n.r = d.u;
  n.d = d.r;
  n.l = d.d;
  return n;
}

dice rotF(dice d) {
  dice n = d;
  n.f = d.u;
  n.u = d.b;
  n.b = d.d;
  n.d = d.f;
  return n;
}

dice rotB(dice d) {
  dice n = d;
  n.b = d.u;
  n.u = d.f;
  n.f = d.d;
  n.d = d.b;
  return n;
}

typedef struct {
  int x, y;
  int num;
} masu;

vec<vec<int>> Max(vec<vec<int>> a, vec<vec<int>> b) {
  if (a[4000][4000] > b[4000][4000])
    return a;
  else
    return b;
}

vec<vec<int>> stamp(vec<vec<int>> p, vec<masu> s) {
  rep(i, s.size()) {
    if (p[s[i].x + 2000][s[i].y + 2000])
      continue;
    p[s[i].x + 2000][s[i].y + 2000] = s[i].num;
    p[4000][4000] += s[i].num;
  }
  return p;
}

int main(void) {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;

    vec<vec<masu>> stamps(n);
    rep(t, n) {
      int x, y;
      cin >> x >> y;
      dice d;
      cin >> d.l >> d.r >> d.f >> d.b >> d.d >> d.u;
      string rot;
      cin >> rot;
      map<pair<int, int>, int> dp;

      masu s;
      s.x = x;
      s.y = y;
      s.num = d.d;
      stamps[t].pb(s);
      dp[make_pair(x, y)] = 0;
      rep(i, rot.length()) {
        if (rot[i] == 'L') {
          x--;
          d = rotL(d);
        } else if (rot[i] == 'R') {
          x++;
          d = rotR(d);
        } else if (rot[i] == 'F') {
          y--;
          d = rotF(d);
        } else {
          y++;
          d = rotB(d);
        }
        s.x = x;
        s.y = y;
        s.num = d.d;
        if (dp.find(make_pair(x, y)) == dp.end()) {
          stamps[t].pb(s);
          dp[make_pair(x, y)] = stamps[t].size() - 1;
        } else {
          stamps[t][dp[make_pair(x, y)]] = s;
        }
      }
    }

    // vec<map<pair<int, int>, int> > dp(1 << n);
    vec<vec<vec<int>>> dp(1 << n, vec<vec<int>>(4001, vec<int>(4001, 0)));
    dp[0][4000][4000] = 0;
    rep(i, (1 << n)) {
      rep(j, n) {
        if ((i >> j) % 2)
          continue;
        dp[i + (1 << j)] = Max(dp[i + (1 << j)], stamp(dp[i], stamps[j]));
      }
    }
    /*
    int sum = 0;
    for (auto it = dp[(1 << n) - 1].begin(); it != dp[(1 << n) - 1].end(); it++)
    sum += it->second; cout << sum << endl;
    */
    // cout << dp[(1 << n) - 1][SUM] << endl;
    cout << dp[(1 << n) - 1][4000][4000] << endl;
  }

  return 0;
}