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

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

using namespace std;

typedef vector<vector<int>> VVI;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int ans;

class Dice {
public:
  int t, s, e, n, w, b; // t=top,s=south,e=east,n=north,w=west,b=bottom
  Dice() {}
  Dice(int _t, int _s, int _e, int _n, int _w, int _b) {
    t = _t, s = _s, e = _e, n = _n, w = _w, b = _b;
  }

  Dice rotate_front() { return Dice(s, b, e, t, w, n); }

  Dice rotate_left() { return Dice(e, s, b, n, t, w); }

  Dice rotate_right() { return Dice(w, s, t, n, b, e); }

  Dice rotate_back() { return Dice(n, t, e, b, w, s); }

  Dice move_right() { return Dice(t, e, n, w, s, b); }

  Dice move_left() { return Dice(t, w, s, e, n, b); }

  void print() {
    cout << "t=" << t << "s=" << s << "e=" << e << "n=" << n << "w=" << w
         << "b=" << b << endl;
  }
};

vector<Dice> all;

Dice getDice(int top, int south) {
  for (int i = 0; i < 24; i++) {
    if (all[i].t == top && all[i].s == south) {
      return all[i];
    }
  }
}

void allState(int a, int b, int c, int d, int e, int f) {
  all.clear();
  Dice dice(a, b, c, d, e, f);
  for (int i = 0; i < 6; i++) {
    for (int j = 0; j < 4; j++) {
      all.push_back(dice);
      dice = dice.move_right();
    }
    if (i < 4)
      dice = dice.rotate_front();
    if (i == 3)
      dice = dice.rotate_right();
    if (i == 4)
      dice = dice.rotate_right().rotate_right();
  }
}

int N;
vector<vector<vector<int>>> v;

int check(vector<int> vv) {
  int res = 0;
  vector<vector<int>> d;
  d.resize(N);

  for (int i = 0; i < vv.size(); i++) {
    for (int j = 0; j < 6; j++) {
      d[i].push_back(v[i][vv[i]][j]);
    }
  }

  for (int i = 0; i < 6; i++) {
    map<int, int> m;
    for (int j = 0; j < N; j++) {
      m[d[j][i]]++;
    }

    int Max = 0;
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
      Max = max(Max, it->second);
    }
    res += N - Max;
    if (res >= ans)
      return 1 << 29;
  }
  return res;
}

void dfs(int cnt, vector<int> vv) {
  if (ans == 0)
    return;

  if (cnt == N) {
    ans = min(ans, check(vv));
    return;
  }

  for (int i = 0; i < v[cnt].size(); i++) {
    vector<int> tmp = vv;
    tmp.push_back(i);
    dfs(cnt + 1, tmp);
  }
}

int main() {

  while (cin >> N, N) {
    set<string> S;

    string in[4][6];

    string s;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 6; j++) {
        cin >> s;
        in[i][j] = s;
        S.insert(s);
      }
    }

    map<string, int> mp;

    int cnt = 1;
    for (set<string>::iterator it = S.begin(); it != S.end(); it++) {
      mp[*it] = cnt++;
    }

    int dice[4][6];
    memset(dice, 0, sizeof(dice));
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 6; j++) {
        dice[i][j] = mp[in[i][j]];
      }
    }

    //   int t,s,e,n,w,b; //t=top,s=south,e=east,n=north,w=west,b=bottom

    v.clear();

    for (int i = 0; i < N; i++) {
      allState(dice[i][2], dice[i][0], dice[i][1], dice[i][5], dice[i][4],
               dice[i][3]);

      vector<vector<int>> tmp2;

      for (int j = 0; j < all.size(); j++) {
        vector<int> tmp;
        tmp.push_back(all[j].t);
        tmp.push_back(all[j].s);
        tmp.push_back(all[j].e);
        tmp.push_back(all[j].n);
        tmp.push_back(all[j].w);
        tmp.push_back(all[j].b);
        tmp2.push_back(tmp);
      }
      sort(tmp2.begin(), tmp2.end());
      tmp2.erase(unique(tmp2.begin(), tmp2.end()), tmp2.end());
      v.push_back(tmp2);
    }

    ans = 1 << 29;
    vector<int> e;
    dfs(0, e);

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef vector<vector<int>> VVI;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int ans;

class Dice {
public:
  int t, s, e, n, w, b; // t=top,s=south,e=east,n=north,w=west,b=bottom
  Dice() {}
  Dice(int _t, int _s, int _e, int _n, int _w, int _b) {
    t = _t, s = _s, e = _e, n = _n, w = _w, b = _b;
  }

  Dice rotate_front() { return Dice(s, b, e, t, w, n); }

  Dice rotate_left() { return Dice(e, s, b, n, t, w); }

  Dice rotate_right() { return Dice(w, s, t, n, b, e); }

  Dice rotate_back() { return Dice(n, t, e, b, w, s); }

  Dice move_right() { return Dice(t, e, n, w, s, b); }

  Dice move_left() { return Dice(t, w, s, e, n, b); }

  void print() {
    cout << "t=" << t << "s=" << s << "e=" << e << "n=" << n << "w=" << w
         << "b=" << b << endl;
  }
};

vector<Dice> all;

Dice getDice(int top, int south) {
  for (int i = 0; i < 24; i++) {
    if (all[i].t == top && all[i].s == south) {
      return all[i];
    }
  }
}

void allState(int a, int b, int c, int d, int e, int f) {
  all.clear();
  Dice dice(a, b, c, d, e, f);
  for (int i = 0; i < 6; i++) {
    for (int j = 0; j < 4; j++) {
      all.push_back(dice);
      dice = dice.move_right();
    }
    if (i < 4)
      dice = dice.rotate_front();
    if (i == 3)
      dice = dice.rotate_right();
    if (i == 4)
      dice = dice.rotate_right().rotate_right();
  }
}

int N;
vector<vector<vector<int>>> v;

int check(vector<int> vv) {
  int res = 0;
  vector<vector<int>> d;
  d.resize(N);

  for (int i = 0; i < vv.size(); i++) {
    for (int j = 0; j < 6; j++) {
      d[i].push_back(v[i][vv[i]][j]);
    }
  }

  for (int i = 0; i < 6; i++) {
    map<int, int> m;
    for (int j = 0; j < N; j++) {
      m[d[j][i]]++;
    }

    int Max = 0;
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
      Max = max(Max, it->second);
    }
    res += N - Max;
    if (res >= ans)
      return 1 << 29;
  }
  return res;
}

void dfs(int cnt, vector<int> vv) {
  if (ans == 0)
    return;

  if (cnt == N) {
    ans = min(ans, check(vv));
    return;
  }

  for (int i = 0; i < v[cnt].size(); i++) {
    vector<int> tmp = vv;
    tmp.push_back(i);
    dfs(cnt + 1, tmp);
  }
}

int main() {

  while (cin >> N, N) {
    set<string> S;

    string in[4][6];

    string s;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 6; j++) {
        cin >> s;
        in[i][j] = s;
        S.insert(s);
      }
    }

    map<string, int> mp;

    int cnt = 1;
    for (set<string>::iterator it = S.begin(); it != S.end(); it++) {
      mp[*it] = cnt++;
    }

    int dice[4][6];
    memset(dice, 0, sizeof(dice));
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 6; j++) {
        dice[i][j] = mp[in[i][j]];
      }
    }

    //   int t,s,e,n,w,b; //t=top,s=south,e=east,n=north,w=west,b=bottom

    v.clear();

    for (int i = 0; i < N; i++) {
      allState(dice[i][2], dice[i][0], dice[i][1], dice[i][5], dice[i][4],
               dice[i][3]);

      vector<vector<int>> tmp2;

      for (int j = 0; j < all.size(); j++) {
        vector<int> tmp;
        tmp.push_back(all[j].t);
        tmp.push_back(all[j].s);
        tmp.push_back(all[j].e);
        tmp.push_back(all[j].n);
        tmp.push_back(all[j].w);
        tmp.push_back(all[j].b);
        tmp2.push_back(tmp);
      }
      v.push_back(tmp2);
    }

    //    sort(v.begin(),v.end());
    //    v.erase(unique(v.begin(),v.end()),v.end());

    ans = 1 << 29;
    vector<int> e;
    dfs(0, e);

    cout << ans << endl;
  }

  return 0;
}