題目大意:輸入n個正方體,每個正方體六個面,各被塗上一種顏色,求最少需要塗改的正方體面,使得所有正方體相同的一面顏色都相同。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 30
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},{3,4,5,0,1,2},{3,5,1,4,0,2},
{3,1,0,5,4,2},{3,0,4,1,5,2},{1,3,5,0,2,4},{0,3,1,4,2,5},{4,3,0,5,2,1},{5,3,4,1,2,0}
};
char sname[MAX][MAX];
int top,n,ans;
int map[6][6],r[MAX],end[6][6];
int max(int x,int y)
{
return x>y ? x:y;
}
int min(int x,int y)
{
return x<y?x:y;
}
int set_read(char *s)
{
int i;
for (i=0;i<top;i++)
{
if (strcmp(sname[i],s)==0)
return i;
}
strcpy(sname[top++],s);
return top-1;
}
void check()
{
int i,j;
for (i=0;i<n;i++)
{
for (j=0;j<6;j++)
{
end[i][dice24[r[i]][j]]=map[i][j];
}
}
int tot=0;
for (j=0;j<6;j++)
{
int cnt[MAX];
memset(cnt,0,sizeof(cnt));
int sum=0;
for (i=0;i<n;i++)
{
sum=max(sum,++cnt[end[i][j]]);
}
tot+=n-sum;
}
ans=min(tot,ans);
}
void dfs(int x)
{
if (x==n)
{
//system("pause");
check();
}
else
for (int i=0;i<24;i++)
{
r[x]=i;
dfs(x+1);
}
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
if (n==0)
break;
int i,j;
char name[MAX];
top=0;
for (i=0;i<n;i++)
{
for (j=0;j<6;j++)
{
scanf("%s",name);
map[i][j]=set_read(name);
//printf("%d\n",map[i][j]);
}
}
//system("pause");
ans=n*6;
r[0]=0;
dfs(1);
printf("%d\n",ans);
//system("pause");
}
return 0;
}