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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

// 28ñÅ³ÉàÇé
class Dice {
private:
  int yoko;
  int tate;
  int round;

private:
  int face[3][4];

public:
  int getFace(int i, int j) const { return face[i][j]; }
  Dice(vector<int> &v) {
    init();
    face[1][1] = v[0];
    face[2][1] = v[1];
    face[1][2] = v[2];
    face[1][0] = v[3];
    face[0][1] = v[4];
    face[1][3] = v[5];
  }

private:
  // _CXÌú»
  void init() {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 4; j++) {
        face[i][j] = -1;
      }
    }
    yoko = 0;
    tate = 0;
    round = 0;
  }

public:
  // _CXÌñ]
  void rotate() {
    if (round < 4) {
      int tmp = face[2][1];
      face[2][1] = face[1][2];
      face[1][2] = face[0][1];
      face[0][1] = face[1][0];
      face[1][0] = tmp;
      round++;
    }
    if (round >= 4) {
      round = 0;
      // àÇÁÄ«½çA¡ÌXbgðñ]³¹é
      if (yoko < 4) {
        int tmp = face[1][3];
        for (int i = 2; i >= 0; i--) {
          face[1][i + 1] = face[1][i];
        }
        face[1][0] = tmp;
        yoko++;
      }
      // àÇÁÄ«½çAcÌXbgðñ]³¹é
      if (yoko >= 4) {
        if (tate < 4) {
          tate++;
          int tmp = face[1][3];
          face[1][3] = face[0][1];
          for (int i = 0; i < 2; i++) {
            face[i][1] = face[i + 1][1];
          }
          face[2][1] = tmp;
        }
        if (tate >= 4) {
          round = 0;
          tate = 0;
          yoko = 0;
        }
      }
    }
  };
};

const int INF = 1000000000;
int n;
vector<Dice> dices;
int minCost;
int msi[1001];

void dfs(int depth) {
  if (depth == n) {
    int sum = 0;
    // Ê²ÆÉf[^ðûW
    for (int i = 0; i < 6; i++) {
      memset(msi, 0, sizeof(msi));
      int maxNum = 0;
      for (int j = 0; j < n; j++) {
        if (i == 0) {
          msi[dices[j].getFace(1, 1)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(1, 1)]);
        } else if (i == 1) {
          msi[dices[j].getFace(2, 1)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(2, 1)]);
        } else if (i == 2) {
          msi[dices[j].getFace(1, 2)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(1, 2)]);
        } else if (i == 3) {
          msi[dices[j].getFace(1, 0)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(1, 0)]);
        } else if (i == 4) {
          msi[dices[j].getFace(0, 1)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(0, 1)]);
        } else {
          msi[dices[j].getFace(1, 3)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(1, 3)]);
        }
      }
      sum += n - maxNum;
    }
    minCost = min(sum, minCost);
  } else {
    // ñ]Èµ
    dfs(depth + 1);
    for (int i = 0; i < 20; i++) {
      dices[depth].rotate();
      dfs(depth + 1);
    }
  }
}

int main() {

  while (cin >> n && n != 0) {
    minCost = INF;
    int idx = 0;
    map<string, int> idxs;
    for (int i = 0; i < n; i++) {
      vector<int> v;
      for (int j = 0; j < 6; j++) {
        string s;
        cin >> s;
        if (idxs.find(s) == idxs.end())
          idxs[s] = idx++;
        v.push_back(idxs[s]);
      }
      dices.push_back(Dice(v));
    }
    dfs(0);
    dices.clear();
    cout << minCost << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

// 28ñÅ³ÉàÇé
class Dice {
private:
  int yoko;
  int tate;
  int round;

private:
  int face[3][4];

public:
  int getFace(int i, int j) const { return face[i][j]; }
  Dice(vector<int> &v) {
    init();
    face[1][1] = v[0];
    face[2][1] = v[1];
    face[1][2] = v[2];
    face[1][0] = v[3];
    face[0][1] = v[4];
    face[1][3] = v[5];
  }

private:
  // _CXÌú»
  void init() {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 4; j++) {
        face[i][j] = -1;
      }
    }
    yoko = 0;
    tate = 0;
    round = 0;
  }

public:
  // _CXÌñ]
  void rotate() {
    if (round < 4) {
      int tmp = face[2][1];
      face[2][1] = face[1][2];
      face[1][2] = face[0][1];
      face[0][1] = face[1][0];
      face[1][0] = tmp;
      round++;
    }
    if (round >= 4) {
      round = 0;
      // àÇÁÄ«½çA¡ÌXbgðñ]³¹é
      if (yoko < 4) {
        int tmp = face[1][3];
        for (int i = 2; i >= 0; i--) {
          face[1][i + 1] = face[1][i];
        }
        face[1][0] = tmp;
        yoko++;
      }
      // àÇÁÄ«½çAcÌXbgðñ]³¹é
      if (yoko >= 4) {
        if (tate < 4) {
          tate++;
          int tmp = face[1][3];
          face[1][3] = face[0][1];
          for (int i = 0; i < 2; i++) {
            face[i][1] = face[i + 1][1];
          }
          face[2][1] = tmp;
        }
        if (tate >= 4) {
          round = 0;
          tate = 0;
          yoko = 0;
        }
      }
    }
  };
};

const int INF = 1000000000;
int n;
vector<Dice> dices;
int minCost;
int msi[1001];

void dfs(int depth) {
  if (depth == n) {
    int sum = 0;
    // Ê²ÆÉf[^ðûW
    for (int i = 0; i < 6; i++) {
      memset(msi, 0, sizeof(msi));
      int maxNum = 0;
      for (int j = 0; j < n; j++) {
        if (i == 0) {
          msi[dices[j].getFace(1, 1)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(1, 1)]);
        } else if (i == 1) {
          msi[dices[j].getFace(2, 1)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(2, 1)]);
        } else if (i == 2) {
          msi[dices[j].getFace(1, 2)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(1, 2)]);
        } else if (i == 3) {
          msi[dices[j].getFace(1, 0)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(1, 0)]);
        } else if (i == 4) {
          msi[dices[j].getFace(0, 1)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(0, 1)]);
        } else {
          msi[dices[j].getFace(1, 3)]++;
          maxNum = max(maxNum, msi[dices[j].getFace(1, 3)]);
        }
      }
      sum += n - maxNum;
    }
    minCost = min(sum, minCost);
  } else {
    // ñ]Èµ
    dfs(depth + 1);
    for (int i = 0; i < 24; i++) {
      dices[depth].rotate();
      dfs(depth + 1);
    }
  }
}

int main() {

  while (cin >> n && n != 0) {
    minCost = INF;
    int idx = 0;
    map<string, int> idxs;
    for (int i = 0; i < n; i++) {
      vector<int> v;
      for (int j = 0; j < 6; j++) {
        string s;
        cin >> s;
        if (idxs.find(s) == idxs.end())
          idxs[s] = idx++;
        v.push_back(idxs[s]);
      }
      dices.push_back(Dice(v));
    }
    dfs(0);
    dices.clear();
    cout << minCost << endl;
  }

  return 0;
}