题目地址:http://vjudge.net/problem/UVALive-3401
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a);i<=(b);++i)
const int maxn=5;
int dice24[24][6]= { //这24种都是相同的
{2,1,5,0,4,3},{2,0,1,4,5,3},{2,4,0,5,1,3},{2,5,4,1,0,3},{4,2,5,0,3,1},{5,2,1,4,3,0},{1,2,0,5,3,4},{0,2,4,1,3,5},{0,1,2,3,4,5},{4,0,2,3,5,1},{5,4,2,3,1,0},{1,5,2,3,0,4},{5,1,3,2,4,0},{1,0,3,2,5,4},{0,4,3,2,1,5},{4,5,3,2,0,1},{1,3,5,0,2,4},{0,3,1,4,2,5},{4,3,0,5,2,1},{5,3,4,1,2,0},{3,4,5,0,1,2},{3,5,1,4,0,2},{3,1,0,5,4,2},{3,0,4,1,5,2}
};
int color[maxn][6],dice[maxn][6],r[maxn],n,ans;
map<string, int> ID;
void Check(){REP(i,0,n-1) REP(j,0,5) color[i][dice24[r[i]][j]]=dice[i][j];int tot=0;REP(j,0,5){int MaxColor=0; //找出该面同种颜色最多出现次数int cnt[maxn*6]={0};REP(i,0,n-1) MaxColor=max(MaxColor,++cnt[color[i][j]]);tot+=n-MaxColor; //要涂n-MaxCloor个}ans=min(ans,tot);
}
void DFS(int cur){if(cur==n) Check();else REP(i,0,23) {r[cur]=i;DFS(cur+1);}
}
int main(int argc, char const *argv[])
{// init();while(scanf("%d",&n)==1&&n){ID.clear();REP(i,0,n-1) REP(j,0,5){string name;cin>>name;int id;if(ID.count(name)) id=ID[name];else id=ID.size(),ID[name]=id;dice[i][j]=id;}ans=n*6;r[0]=0; //固定一个立方体不旋转DFS(1);printf("%d\n", ans);}return 0;
}
/*
const int Left[]={4,0,2,3,5,1}; //左旋转
const int up[]={2,1,5,0,4,3}; //上旋转
void rot(const int* T,int* p){ int q[6];memcpy(q,p,sizeof(q));REP(i,0,5) p[i]=T[q[i]];
}
void enumerate_permutation()
{ int p0[6]={0,1,2,3,4,5};printf("int dice24[24][6] = {\n");REP(i,0,5) {int p[6]; //p[i] 代表i数字出现在p[i]面memcpy(p,p0,sizeof(p0));if(i==0) rot(up,p);if(i==1) rot(Left,p),rot(up,p);if(i==3) rot(up,p),rot(up,p);if(i==4) rot(Left,p),rot(Left,p),rot(Left,p),rot(up,p);if(i==5) rot(Left,p),rot(Left,p),rot(up,p);REP(j,0,3) {printf("{%d,%d,%d,%d,%d,%d},\n",p[0],p[1],p[2],p[3],p[4],p[5]);rot(Left,p);}}printf("};\n");
}
void init(){enumerate_permutation();
}
*/