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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> pi;
typedef long long ll;
const int MAX_N = 15;

struct Dice {
  int l, r, f, b, d, u;
  int x, y;

  Dice(int _l, int _r, int _f, int _b, int _d, int _u, int _x, int _y) {
    l = _l;
    r = _r;
    f = _f;
    b = _b;
    d = _d;
    u = _u;
    x = _x;
    y = _y;
  }

  void Left() {
    int tmp = u;
    u = r;
    r = d;
    d = l;
    l = tmp;
    x -= 1;
  }

  void Right() {
    int tmp = u;
    u = l;
    l = d;
    d = r;
    r = tmp;
    x += 1;
  }

  void Front() {
    int tmp = f;
    f = u;
    u = b;
    b = d;
    d = tmp;
    y -= 1;
  }

  void Back() {
    int tmp = u;
    u = f;
    f = d;
    d = b;
    b = tmp;
    y += 1;
  }

  void Role(char c) {
    if (c == 'L')
      Left();
    else if (c == 'R')
      Right();
    else if (c == 'F')
      Front();
    else
      Back();
  }

  pi cord() { return pi(x, y); }

  void print() {
    cout << l << " " << r << " " << f << " " << b << " " << d << " " << u
         << endl;
  }
};

int N;
map<pi, int> Scores[16];
map<pi, int> Map;
int dp[1 << MAX_N];

int dfs(int S) {

  int &res = dp[S];
  if (res > 0) {
    return res;
  }

  if (S == (1 << N) - 1) {
    return 0;
  }

  for (int u = 0; u < N; ++u) {
    if (!(S >> u & 1)) {
      int score = 0;
      int nS = S | 1 << u;
      for (auto mass : Scores[u]) {
        pi cord = mass.first;
        if ((Map[cord] & nS) == Map[cord])
          score += mass.second;
      }
      res = max(res, dfs(nS) + score);
    }
  }
  return res;
}

int main() {
  while (cin >> N, N) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; i++)
      Scores[i].clear();
    Map.clear();
    for (int i = 0; i < N; i++) {
      int x, y, l, r, f, b, d, u;
      string rot;
      cin >> x >> y >> l >> r >> f >> b >> d >> u >> rot;
      Dice dice(l, r, f, b, d, u, x, y);
      pi cord = dice.cord();
      Scores[i][cord] = dice.d;
      if (Map.find(cord) == Map.end())
        Map[cord] = 0;
      Map[cord] = Map[cord] | (1 << i);
      for (auto c : rot) {
        dice.Role(c);
        //	dice.print();
        cord = dice.cord();
        Scores[i][cord] = dice.d;
        if (Map.find(cord) == Map.end())
          Map[cord] = 0;
        Map[cord] = Map[cord] | (1 << i);
      }

      //      int sum = 0;
      /*      for(auto x: Scores[i]){
        cout << x.first.first << " "<< x.first.second << " " << x.second <<
      endl;
        //	sum += x.second;
      }

      for(auto x: Map){
        cout << x.first.first << " "<< x.first.second << " " << x.second <<
      endl;
      }
      */
      //      cout << sum  << endl;
    }

    cout << dfs(0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> pi;
typedef long long ll;
const int MAX_N = 15;

struct Dice {
  int l, r, f, b, d, u;
  int x, y;

  Dice(int _l, int _r, int _f, int _b, int _d, int _u, int _x, int _y) {
    l = _l;
    r = _r;
    f = _f;
    b = _b;
    d = _d;
    u = _u;
    x = _x;
    y = _y;
  }

  void Left() {
    int tmp = u;
    u = r;
    r = d;
    d = l;
    l = tmp;
    x -= 1;
  }

  void Right() {
    int tmp = u;
    u = l;
    l = d;
    d = r;
    r = tmp;
    x += 1;
  }

  void Front() {
    int tmp = f;
    f = u;
    u = b;
    b = d;
    d = tmp;
    y -= 1;
  }

  void Back() {
    int tmp = u;
    u = f;
    f = d;
    d = b;
    b = tmp;
    y += 1;
  }

  void Role(char c) {
    if (c == 'L')
      Left();
    else if (c == 'R')
      Right();
    else if (c == 'F')
      Front();
    else
      Back();
  }

  pi cord() { return pi(x, y); }

  void print() {
    cout << l << " " << r << " " << f << " " << b << " " << d << " " << u
         << endl;
  }
};

int N;
map<pi, int> Scores[16];
map<pi, int> Map;
int dp[1 << MAX_N];

int dfs(int S) {

  if (S == (1 << N) - 1) {
    return 0;
  }

  int &res = dp[S];
  for (int u = 0; u < N; ++u) {
    if (!(S >> u & 1)) {
      int score = 0;
      int nS = S | 1 << u;
      for (auto mass : Scores[u]) {
        pi cord = mass.first;
        if ((Map[cord] & nS) == Map[cord])
          score += mass.second;
      }
      res = max(res, dfs(nS) + score);
    }
  }
  return res;
}

int main() {
  while (cin >> N, N) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; i++)
      Scores[i].clear();
    Map.clear();
    for (int i = 0; i < N; i++) {
      int x, y, l, r, f, b, d, u;
      string rot;
      cin >> x >> y >> l >> r >> f >> b >> d >> u >> rot;
      Dice dice(l, r, f, b, d, u, x, y);
      pi cord = dice.cord();
      Scores[i][cord] = dice.d;
      if (Map.find(cord) == Map.end())
        Map[cord] = 0;
      Map[cord] = Map[cord] | (1 << i);
      for (auto c : rot) {
        dice.Role(c);
        //	dice.print();
        cord = dice.cord();
        Scores[i][cord] = dice.d;
        if (Map.find(cord) == Map.end())
          Map[cord] = 0;
        Map[cord] = Map[cord] | (1 << i);
      }

      //      int sum = 0;
      /*      for(auto x: Scores[i]){
        cout << x.first.first << " "<< x.first.second << " " << x.second <<
      endl;
        //	sum += x.second;
      }

      for(auto x: Map){
        cout << x.first.first << " "<< x.first.second << " " << x.second <<
      endl;
      }
      */
      //      cout << sum  << endl;
    }

    cout << dfs(0) << endl;
  }
  return 0;
}