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

#ifndef _WIN32
#include <iostream>
#endif // !_WIN32

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> bool chmin(T &l, T r) {
  bool res = l > r;
  if (res)
    l = r;
  return res;
}
template <typename T> bool chmax(T &l, T r) {
  bool res = l < r;
  if (res)
    l = r;
  return res;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
typedef vector<int> V;
int f(int x, int y, int z) { return x + y + z; }
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {1, -1, 0, 0};
#define BBB 0
#define FFF 1
#define LLL 2
#define RRR 3
const int hiro[4][6] = {{0, 1, 5, 4, 2, 3},
                        {0, 1, 4, 5, 3, 2},
                        {4, 5, 2, 3, 1, 0},
                        {5, 4, 2, 3, 0, 1}};
typedef vector<int> V;
typedef vector<V> VV;
typedef pair<int, int> P;
void rot_move(vector<int> &state, int r, int &x, int &y) {
  x += dx[r];
  y += dy[r];
  vector<int> nxt(6);
  REP(i, 6) nxt[hiro[r][i]] = state[i];
  REP(i, 6) state[i] = nxt[i];
}
map<int, int> memo;
inline int ch(int x, int y) {
  x += 1000;
  y += 1000;
  return x * 2123 + y;
}
int main() {
  int N;
  while (cin >> N, N) {
    V x(N), y(N);
    VV dice(N, V(6));
    vector<string> rot(N);
    REP(i, N) {
      cin >> x[i] >> y[i];
      cin >> dice[i];
      cin >> rot[i];
      for (auto &it : rot[i]) {
        if (it == 'B')
          it = BBB;
        if (it == 'F')
          it = FFF;
        if (it == 'L')
          it = LLL;
        if (it == 'R')
          it = RRR;
      }
    }
    vector<set<int>> fut(1 << N);
    REP(bit, 1 << N) {
      REP(i, N) if ((bit >> i) & 1) {
        int xx = x[i];
        int yy = y[i];
        fut[bit].insert(ch(xx, yy));
        auto v = dice[i];
        for (auto &it : rot[i]) {
          rot_move(v, it, xx, yy);
          fut[bit].insert(ch(xx, yy));
        }
      }
    }
    V dp(1 << N, -1);
    dp[0] = 0;
    REC(int, int)
    dfs = [&](int bit) {
      if (dp[bit] >= 0)
        return dp[bit];
      dp[bit] = 0;
      REP(i, N) if ((bit >> i) & 1) {
        auto v = dice[i];
        int nxtbit = bit - (1 << i);
        int cost = dfs(nxtbit);
        int xx = x[i];
        int yy = y[i];
        memo.clear();
        if (!fut[nxtbit].count(ch(xx, yy)))
          memo[ch(xx, yy)] = v[4];
        for (auto &it : rot[i]) {
          rot_move(v, it, xx, yy);
          if (!fut[nxtbit].count(ch(xx, yy))) {
            memo[ch(xx, yy)] = v[4];
          }
        }
        for (auto &it : memo) {
          cost += it.second;
        }
        chmax(dp[bit], cost);
      }
      return dp[bit];
    };
    cout << dfs((1 << N) - 1) << endl;
  }
}

Error: TLE

Fixed submission:

#ifndef _WIN32
#include <iostream>
#endif // !_WIN32

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> bool chmin(T &l, T r) {
  bool res = l > r;
  if (res)
    l = r;
  return res;
}
template <typename T> bool chmax(T &l, T r) {
  bool res = l < r;
  if (res)
    l = r;
  return res;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
typedef vector<int> V;
int f(int x, int y, int z) { return x + y + z; }
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {1, -1, 0, 0};
#define BBB 0
#define FFF 1
#define LLL 2
#define RRR 3
const int hiro[4][6] = {{0, 1, 5, 4, 2, 3},
                        {0, 1, 4, 5, 3, 2},
                        {4, 5, 2, 3, 1, 0},
                        {5, 4, 2, 3, 0, 1}};
typedef vector<int> V;
typedef vector<V> VV;
typedef pair<int, int> P;
void rot_move(vector<int> &state, int r, int &x, int &y) {
  x += dx[r];
  y += dy[r];
  vector<int> nxt(6);
  REP(i, 6) nxt[hiro[r][i]] = state[i];
  REP(i, 6) state[i] = nxt[i];
}
map<P, int> memo;
int main() {
  int N;
  while (cin >> N, N) {
    V x(N), y(N);
    VV dice(N, V(6));
    vector<string> rot(N);
    REP(i, N) {
      cin >> x[i] >> y[i];
      cin >> dice[i];
      cin >> rot[i];
      for (auto &it : rot[i]) {
        if (it == 'B')
          it = BBB;
        if (it == 'F')
          it = FFF;
        if (it == 'L')
          it = LLL;
        if (it == 'R')
          it = RRR;
      }
    }
    vector<set<P>> fut(1 << N);
    REP(bit, 1 << N) {
      REP(i, N) if ((bit >> i) & 1) {
        int xx = x[i];
        int yy = y[i];
        fut[bit].insert(P(xx, yy));
        auto v = dice[i];
        for (auto &it : rot[i]) {
          rot_move(v, it, xx, yy);
          fut[bit].insert(P(xx, yy));
        }
      }
    }
    V dp(1 << N, -1);
    dp[0] = 0;
    REC(int, int)
    dfs = [&](int bit) {
      if (dp[bit] >= 0)
        return dp[bit];
      dp[bit] = 0;
      REP(i, N) if ((bit >> i) & 1) {
        auto v = dice[i];
        int nxtbit = bit - (1 << i);
        int cost = dfs(nxtbit);
        int xx = x[i];
        int yy = y[i];
        memo.clear();
        if (!fut[nxtbit].count(P(xx, yy)))
          memo[P(xx, yy)] = v[4];
        for (auto &it : rot[i]) {
          rot_move(v, it, xx, yy);
          if (!fut[nxtbit].count(P(xx, yy))) {
            memo[P(xx, yy)] = v[4];
          }
        }
        for (auto &it : memo) {
          cost += it.second;
        }
        chmax(dp[bit], cost);
      }
      return dp[bit];
    };
    cout << dfs((1 << N) - 1) << endl;
  }
}