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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int N;

vector<int> t[15];
string u[15];
int sx[15], sy[15];
int dp[(1 << 15)];
map<P, int> mp;

void ry(vector<int> &v) {
  int tmp = v[2];
  v[2] = v[4];
  v[4] = v[3];
  v[3] = v[5];
  v[5] = tmp;
}

void rx(vector<int> &v) {
  int tmp = v[0];
  v[0] = v[4];
  v[4] = v[1];
  v[1] = v[5];
  v[5] = tmp;
}

int main() {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    mp.clear();
    for (int i = 0; i < N; i++) {
      t[i].clear();
      t[i].resize(6);
      cin >> sx[i] >> sy[i];
      for (int j = 0; j < 6; j++)
        cin >> t[i][j];
      cin >> u[i];
    }

    for (int i = 0; i < N; i++) {
      P p = P(sx[i], sy[i]);
      mp[p] |= (1 << i);
      for (int j = 0; j < (int)u[i].size(); j++) {
        if (u[i][j] == 'L')
          p.first--;
        if (u[i][j] == 'R')
          p.first++;
        if (u[i][j] == 'F')
          p.second--;
        if (u[i][j] == 'B')
          p.second++;
        mp[p] |= (1 << i);
      }
    }

    memset(dp, 0, sizeof(dp));

    for (int S = 0; S < (1 << N); S++) {
      for (int i = 0; i < N; i++) {
        if (S >> i & 1)
          continue;
        map<P, int> w;
        vector<int> v = t[i];
        P p = P(sx[i], sy[i]);
        int cost = 0;

        if ((mp[p] & S) == 0) {
          cost -= w[p];
          w[p] = v[4];
          cost += v[4];
        }

        for (int j = 0; j < (int)u[i].size(); j++) {
          if (u[i][j] == 'L') {
            p.first--;
            rx(v);
            rx(v);
            rx(v);
          }
          if (u[i][j] == 'R') {
            p.first++;
            rx(v);
          }
          if (u[i][j] == 'F') {
            p.second--;
            ry(v);
            ry(v);
            ry(v);
          }
          if (u[i][j] == 'B') {
            p.second++;
            ry(v);
          }
          if ((mp[p] & S) == 0) {
            cost -= w[p];
            w[p] = v[4];
            cost += v[4];
          }
        }
        dp[S | (1 << i)] = max(dp[S | (1 << i)], dp[S] + cost);
      }
    }
    cout << dp[(1 << N) - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int N;

vector<int> t[15];
string u[15];
int sx[15], sy[15];
int dp[(1 << 15)];

void ry(vector<int> &v) {
  int tmp = v[2];
  v[2] = v[4];
  v[4] = v[3];
  v[3] = v[5];
  v[5] = tmp;
}

void rx(vector<int> &v) {
  int tmp = v[0];
  v[0] = v[4];
  v[4] = v[1];
  v[1] = v[5];
  v[5] = tmp;
}

int main() {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      t[i].clear();
      t[i].resize(6);
      cin >> sx[i] >> sy[i];
      for (int j = 0; j < 6; j++)
        cin >> t[i][j];
      cin >> u[i];
    }
    memset(dp, 0, sizeof(dp));
    map<P, int> w, mp;
    for (int S = 0; S < (1 << N); S++) {
      mp.clear();

      for (int i = 0; i < N; i++) {
        if ((S >> i & 1) == 0)
          continue;
        P p = P(sx[i], sy[i]);
        mp[p] = 1;
        for (int j = 0; j < (int)u[i].size(); j++) {
          if (u[i][j] == 'L')
            p.first--;
          if (u[i][j] == 'R')
            p.first++;
          if (u[i][j] == 'F')
            p.second--;
          if (u[i][j] == 'B')
            p.second++;
          mp[p] = 1;
        }
      }

      for (int i = 0; i < N; i++) {
        if (S >> i & 1)
          continue;
        vector<int> v = t[i];
        P p = P(sx[i], sy[i]);
        int cost = 0;
        w.clear();
        if (!mp[p]) {
          cost -= w[p];
          w[p] = v[4];
          cost += v[4];
        }

        for (int j = 0; j < (int)u[i].size(); j++) {
          if (u[i][j] == 'L') {
            p.first--;
            rx(v);
            rx(v);
            rx(v);
          }
          if (u[i][j] == 'R') {
            p.first++;
            rx(v);
          }
          if (u[i][j] == 'F') {
            p.second--;
            ry(v);
            ry(v);
            ry(v);
          }
          if (u[i][j] == 'B') {
            p.second++;
            ry(v);
          }

          if (!mp[p]) {
            cost -= w[p];
            w[p] = v[4];
            cost += v[4];
          }
        }
        dp[S + (1 << i)] = max(dp[S + (1 << i)], dp[S] + cost);
      }
    }
    cout << dp[(1 << N) - 1] << endl;
  }
  return 0;
}