## LA 3401 彩色的立方体（暴力+打表）

left []={4,0,2,3,5,1}; 表示编号为0的面现在被4的面占了，编号为1的面被0占了，依次类推，也就是相当于

``````#include<bits/stdc++.h>
#include<iostream>
using namespace std;

int left1[]={4,0,2,3,5,1};      //left1[q[i]]表示让编号为q[i]的面去哪个面
int up[]={2,1,5,0,4,3};

void rot(int* T,int *p){
int q[6];
memcpy(q,p,sizeof(q));
for(int i=0;i<6;i++)
p[i]=T[q[i]];
}
void enumerate_permutations(){
int p0[]={0,1,2,3,4,5};
//	printf("int dice24[24][6]={\n");

ofstream out("C://Users//asus//Desktop//算法.txt");
out<<"int dice24[24][6]={"<<endl;
for(int i=0;i<6;i++){
int p[6];
memcpy(p,p0,sizeof(p0));
if(i==0) rot(up,p);
if(i==1) {rot(left1,p);rot(up,p);}
if(i==3) {rot(up,p);rot(up,p);}
if(i==4){rot(left1,p);rot(left1,p);rot(left1,p);rot(up,p);}
if(i==5){rot(left1,p);rot(left1,p);rot(up,p);}
for(int i=0;i<4;i++){
//			printf("{%d %d %d %d %d %d},\n",p[0],p[1],p[2],p[3],p[4],p[5]);

out<<"{"<<p[0]<<", "<<p[1]<<", "<<p[2]<<", "<<p[3]<<", "<<p[4]<<", "<<p[5]<<"},"<<endl;
rot(left1,p);
}
}
//	printf("};\n");
out<<"};"<<endl;
}

int main(){

enumerate_permutations();
return 0;

}``````

``````#include<bits/stdc++.h>
using namespace std;
int dice24[24][6]={
{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},
};
vector<string> names;
const int maxn=4;
int dice[maxn][6];            //表示第几个正方体的哪个面的颜色
int r[maxn],ans,n;

int id(char *name){
string s(name);
int n=names.size();
for(int i=0;i<names.size();i++)
if(names[i]==s)
return i;
names.push_back(s);
return n;
}

int color[maxn][6];

void check(){
for(int i=0;i<n;i++)
for(int j=0;j<6;j++)
color[i][dice24[r[i]][j]]=dice[i][j]; //正方体中第几个姿态的哪个面的颜色

int tot=0;                            //用来统计需要替换的颜色
for(int j=0;j<6;j++){
int cnt[maxn*6];
memset(cnt,0,sizeof(cnt));
int maxface=0;
for(int i=0;i<n;i++)
maxface=max(maxface,++cnt[color[i][j]]);

tot+=n-maxface;
}
ans=min(ans,tot);

}

void dfs(int d){
if(d==n) check();
else{
for(int i=0;i<24;i++){
r[d]=i;             //表示第d个正方体的第i种姿态
dfs(d+1);
}
}
}

int main(){

while(scanf("%d",&n) && n){
names.clear();
for(int i=0;i<n;i++)
for(int j=0;j<6;j++){
char name[30];
scanf("%s",name);
dice[i][j]=id(name);
}
ans=n*6;
r[0]=0;
dfs(1);
printf("%d\n",ans);
}
return 0;
}``````