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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

struct Oct {
  int c[8];
  bool operator<(const Oct &rhs) const {
    for (int j = 0; j < 8; ++j) {
      if (c[j] < rhs.c[j])
        return true;
      else if (c[j] > rhs.c[j])
        return false;
    }
    return false;
  }
};

string color[8];
int col[8];
map<string, int> mp;

Oct x_rot(Oct ot) {
  Oct rt;
  rt.c[0] = ot.c[3];
  rt.c[1] = ot.c[2];
  rt.c[2] = ot.c[6];
  rt.c[3] = ot.c[7];
  rt.c[4] = ot.c[0];
  rt.c[5] = ot.c[1];
  rt.c[6] = ot.c[5];
  rt.c[7] = ot.c[4];
  return rt;
}

Oct y_rot(Oct ot) {
  Oct rt;
  rt.c[0] = ot.c[4];
  rt.c[1] = ot.c[0];
  rt.c[2] = ot.c[3];
  rt.c[3] = ot.c[7];
  rt.c[4] = ot.c[5];
  rt.c[5] = ot.c[1];
  rt.c[6] = ot.c[2];
  rt.c[7] = ot.c[6];
  return rt;
}

Oct z_rot(Oct ot) {
  Oct rt;
  rt.c[0] = ot.c[3];
  rt.c[1] = ot.c[0];
  rt.c[2] = ot.c[1];
  rt.c[3] = ot.c[2];
  rt.c[4] = ot.c[7];
  rt.c[5] = ot.c[4];
  rt.c[6] = ot.c[5];
  rt.c[7] = ot.c[6];
  return rt;
}

Oct gOct;
Oct sortOct[64];
set<Oct> ans;

void calc(int depth, int pos) {
  if (depth == 8) {
    Oct temp = gOct;
    for (int x = 0; x < 4; x++) {
      for (int y = 0; y < 4; y++) {
        for (int z = 0; z < 4; z++) {
          sortOct[x + y * 4 + z * 16] = temp;
          temp = z_rot(temp);
        }
        temp = y_rot(temp);
      }
      temp = x_rot(temp);
    }
    sort(sortOct, sortOct + 64);
    ans.insert(sortOct[0]);
    return;
  }
  for (int i = 0; i < 8; i++) {
    if ((pos >> i) & 1) {
      continue;
    }
    gOct.c[depth] = col[i];
    calc(depth + 1, pos | (1 << i));
  }
}

int main() {
  while (true) {
    ans.clear();
    mp.clear();
    for (int j = 0; j < 8; ++j)
      if (!(cin >> color[j])) {
        return 0;
      }
    for (int j = 0; j < 8; ++j) {
      if (mp.find(color[j]) != mp.end())
        col[j] = mp[color[j]];
      else {
        col[j] = mp.size();
        mp[color[j]] = col[j];
      }
    }

    calc(0, 0);
    cout << ans.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

struct Oct {
  int c[8];
  bool operator<(const Oct &rhs) const {
    for (int j = 0; j < 8; ++j) {
      if (c[j] < rhs.c[j])
        return true;
      else if (c[j] > rhs.c[j])
        return false;
    }
    return false;
  }
};

string color[8];
int col[8];
map<string, int> mp;

Oct x_rot(Oct ot) {
  Oct rt;
  rt.c[0] = ot.c[3];
  rt.c[1] = ot.c[2];
  rt.c[2] = ot.c[6];
  rt.c[3] = ot.c[7];
  rt.c[4] = ot.c[0];
  rt.c[5] = ot.c[1];
  rt.c[6] = ot.c[5];
  rt.c[7] = ot.c[4];
  return rt;
}

Oct y_rot(Oct ot) {
  Oct rt;
  rt.c[0] = ot.c[4];
  rt.c[1] = ot.c[0];
  rt.c[2] = ot.c[3];
  rt.c[3] = ot.c[7];
  rt.c[4] = ot.c[5];
  rt.c[5] = ot.c[1];
  rt.c[6] = ot.c[2];
  rt.c[7] = ot.c[6];
  return rt;
}

Oct z_rot(Oct ot) {
  Oct rt;
  rt.c[0] = ot.c[3];
  rt.c[1] = ot.c[0];
  rt.c[2] = ot.c[1];
  rt.c[3] = ot.c[2];
  rt.c[4] = ot.c[7];
  rt.c[5] = ot.c[4];
  rt.c[6] = ot.c[5];
  rt.c[7] = ot.c[6];
  return rt;
}

Oct gOct;
Oct sortOct[64];
set<Oct> ans;

void calc(int depth, int pos) {
  if (depth == 8) {
    Oct temp = gOct;
    for (int x = 0; x < 4; x++) {
      for (int y = 0; y < 4; y++) {
        for (int z = 0; z < 4; z++) {
          sortOct[x + y * 4 + z * 16] = temp;
          temp = z_rot(temp);
        }
        temp = y_rot(temp);
      }
      temp = x_rot(temp);
    }
    sort(sortOct, sortOct + 64);
    ans.insert(sortOct[0]);
    return;
  }
  for (int i = 0; i < 8; i++) {
    if ((pos >> i) & 1) {
      continue;
    }
    gOct.c[depth] = col[i];
    calc(depth + 1, pos | (1 << i));
  }
}

int main() {
  while (true) {
    for (int j = 0; j < 8; ++j)
      if (!(cin >> color[j])) {
        break;
      }
    for (int j = 0; j < 8; ++j) {
      if (mp.find(color[j]) != mp.end())
        col[j] = mp[color[j]];
      else {
        col[j] = mp.size();
        mp[color[j]] = col[j];
      }
    }

    calc(0, 0);
    cout << ans.size() << endl;
  }
  return 0;
}