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

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

struct dice {
  int l, r, f, b, d, u;
  dice() {}
  dice(int l, int r, int f, int b, int d, int u)
      : l(l), r(r), f(f), b(b), d(d), u(u) {}

  dice rot_right() { return dice(d, u, f, b, r, l); }

  dice rot_left() { return dice(u, d, f, b, l, r); }

  dice rot_front() { return dice(l, r, u, d, f, b); }

  dice rot_back() { return dice(l, r, d, u, b, f); }
};

using PR = int;
using MAP_t = map<PR, int>;

int encode(int x, int y) { return 3000 * x + y; }

int N;
MAP_t boards[1 << 15];
int scores[1 << 15];

int merge(int idx1, int idx2, MAP_t &dest) {
  dest = boards[idx1];
  int res_v = scores[idx1];

  for (const pair<PR, int> &itr : boards[idx2]) {
    if (dest[itr.first] == 0) {
      res_v += itr.second;
      dest[itr.first] = itr.second;
    }
  }

  return res_v;
}

int solve() {
  MAP_t trg;

  for (int n = 1; n < (1 << N); ++n) {
    for (int i = 0; i < N; ++i) {
      if ((n & (1 << i)) == 0)
        continue;
      int m = n ^ (1 << i);

      int sc = merge(m, (1 << i), trg);
      if (sc > scores[n]) {
        boards[n] = trg;
        scores[n] = sc;
      }
    }
  }

  return scores[(1 << N) - 1];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    fill(scores, scores + (1 << N), 0);
    for (int i = 0; i < N; ++i)
      boards[1 << i].clear();
    for (int i = 0; i < N; ++i) {
      int x, y;
      dice d;
      string p;
      cin >> x >> y >> d.l >> d.r >> d.f >> d.b >> d.d >> d.u;

      x += 1000;
      y += 1000;

      int idx = 1 << i;
      int sc = 0;

      boards[idx][encode(x, y)] = d.d;
      sc += d.d;

      cin >> p;
      for (char ch : p) {
        if (ch == 'L') {
          --x;
          d = d.rot_left();
        } else if (ch == 'R') {
          ++x;
          d = d.rot_right();
        } else if (ch == 'B') {
          ++y;
          d = d.rot_back();
        } else {
          --y;
          d = d.rot_front();
        }
        PR key = encode(x, y);
        sc += d.d - boards[idx][key];
        boards[idx][key] = d.d;
      }
      scores[idx] = sc;
    }
    cout << solve() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

struct dice {
  int l, r, f, b, d, u;
  dice() {}
  dice(int l, int r, int f, int b, int d, int u)
      : l(l), r(r), f(f), b(b), d(d), u(u) {}

  dice rot_right() { return dice(d, u, f, b, r, l); }

  dice rot_left() { return dice(u, d, f, b, l, r); }

  dice rot_front() { return dice(l, r, u, d, f, b); }

  dice rot_back() { return dice(l, r, d, u, b, f); }
};

using PR = int;
using MAP_t = map<PR, int>;

int encode(int x, int y) { return 3000 * x + y; }

int N;
MAP_t boards[1 << 15];
int scores[1 << 15];

int merge(int idx1, int idx2, MAP_t &dest) {
  dest = boards[idx1];
  int res_v = scores[idx1];

  for (const pair<PR, int> &itr : boards[idx2]) {
    res_v += itr.second - dest[itr.first];
    dest[itr.first] = itr.second;
  }

  return res_v;
}

int solve() {
  MAP_t trg;

  for (int n = 1; n < (1 << N); ++n) {
    for (int i = 0; i < N; ++i) {
      if ((n & (1 << i)) == 0)
        continue;
      int m = n ^ (1 << i);

      int sc = merge((1 << i), m, trg);
      if (sc > scores[n]) {
        boards[n] = trg;
        scores[n] = sc;
      }
    }
  }

  return scores[(1 << N) - 1];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    fill(scores, scores + (1 << N), 0);
    for (int i = 0; i < N; ++i)
      boards[1 << i].clear();
    for (int i = 0; i < N; ++i) {
      int x, y;
      dice d;
      string p;
      cin >> x >> y >> d.l >> d.r >> d.f >> d.b >> d.d >> d.u;

      x += 1000;
      y += 1000;

      int idx = 1 << i;
      int sc = 0;

      boards[idx][encode(x, y)] = d.d;
      sc += d.d;

      cin >> p;
      for (char ch : p) {
        if (ch == 'L') {
          --x;
          d = d.rot_left();
        } else if (ch == 'R') {
          ++x;
          d = d.rot_right();
        } else if (ch == 'B') {
          ++y;
          d = d.rot_back();
        } else {
          --y;
          d = d.rot_front();
        }
        PR key = encode(x, y);
        sc += d.d - boards[idx][key];
        boards[idx][key] = d.d;
      }
      scores[idx] = sc;
    }
    cout << solve() << endl;
  }
  return 0;
}