# LA 3401 - Colored Cubes

color1 color2 color3 color4 color5 color6

corresponds to a cube colored as shown in Figure 6.

The end of the input is indicated by a line containing a single zero. It is not a dataset nor a part of a dataset.

Figure 2: Identically colored cubes

Figure 3: cubes that are not identically colored

Figure 4: An example of recoloring

Figure 5: Numbering of faces Figure 6: Coloring

## Output

For each dataset, output a line containing the minimum number of faces that need to be repainted to make the set of cub es identically colored.

## Sample Input

3
scarlet green blue yellow magenta cyan
blue pink green magenta cyan lemon
purple red blue yellow cyan green
2
red green blue yellow magenta cyan
cyan green blue yellow magenta red
2
red green gray gray magenta cyan
cyan green gray gray magenta red
2
red green blue yellow magenta cyan
magenta red blue yellow cyan green
3
red green blue yellow magenta cyan
cyan green blue yellow magenta red
magenta red blue yellow cyan green
3
blue green green green green blue
green blue blue green green green
green green green green green sea-green
3
red yellow red yellow red yellow
red red yellow yellow red yellow
red red red red red red
4
violet violet salmon salmon salmon salmon
violet salmon salmon salmon salmon violet
violet violet salmon salmon violet violet
violet violet violet violet salmon salmon
1
red green blue yellow magenta cyan
4
magenta pink red scarlet vermilion wine-red
aquamarine blue cyan indigo sky-blue turquoise-blue
blond cream chrome-yellow lemon olive yellow
chrome-green emerald-green green olive vilidian sky-blue
0


## Sample Output

4

2

0

0

2

3

4

4

0

16

#include <iostream>
#include <stdio.h>
#include <map>
#include <queue>
#include <cstring>
#include <string.h>
using namespace std;
#define FOR(i,n) for(int i=1; i<=n; i++)
int all[24][6] = {{1,2,3,4,5,6},{2,3,4,1,5,6},{3,4,1,2,5,6},{4,1,2,3,5,6},{5,1,6
,3,2,4},{1,6,3,5,2,4},{6,3,5,1,2,4},{3,5,1,6,2,4},{2,5,4,6,1,3},{5,4,6,2,1,3},{4
,6,2,5,1,3},{6,2,5,4,1,3},{1,4,3,2,6,5},{4,3,2,1,6,5},{3,2,1,4,6,5},{2,1,4,3,6,5
},{6,1,5,3,4,2},{1,5,3,6,4,2},{5,3,6,1,4,2},{3,6,1,5,4,2},{4,5,2,6,3,1},{5,2,6,4
,3,1},{2,6,4,5,3,1},{6,4,5,2,3,1},};
map<string,int>mID;
int n, id, ans;
int MoFang[5][7];
int sum[30];
int get_temp()
{
int rt = 0;
FOR(j,6)
{
int s = 0;
memset(sum,0,sizeof sum);
FOR(i,n)
{
sum[MoFang[i][j]]++;
s = max(s,sum[MoFang[i][j]]) ;
}
rt += n - s;
}
return rt;
}
void dfs(int xb)
{
if(xb==n+1)
{
int temp = get_temp();

if(temp<ans)
{
ans = temp;
}
return;
}
for(int i=0; i<24; i++)
{
int bc[7];
FOR(j,6) bc[j] = MoFang[xb][j];
FOR(j,6) MoFang[xb][j] = bc[all[i][j-1]];
dfs(xb+1);
FOR(j,6) MoFang[xb][j] = bc[j];
}
}
int shunxu[]={0,3,4,5,6,2,1};
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
char s0[10];
int cl;
id = 1;
mID.clear();
FOR(i,n) FOR(j,6)
{
scanf("%s",s0);
string s="";
int len = strlen(s0);
for(int k=0; k<len ; k++)
{
s += s0[k];
}
s[len] ='\0';

if(mID[s])
{
cl = mID[s];
}else{
cl = id++;
mID[s] = cl;
}
MoFang[i][shunxu[j]] = cl;
}

ans = 6*n;
dfs(2);
printf("%d\n",ans);
}
return 0;
}
/*map<string,bool>QuChong;
int top;
void dfs(char g[])
{
string s="";
char c;
int i;
for(i=1; i<=6; i++)
{
s += g[i];
}
s[6] ='\0';
if(QuChong[s])
{
return;
}else{

QuChong[s] = true;
printf("{");
for(i=1; i<6; i++)
printf("%c,",g[i]);
printf("%c},",g[6]);
top++;
}
char next[10];
memcpy(next, g, 8);
c = next[1];

for(i=1; i<4; i++)next[i] = next[i+1];

next[4] = c;
dfs(next);

memcpy(next, g, 8);
c = next[1];
next[1] = next[5]; next[5] = next[3]; next[3] = next[6]; next[6] = c;
dfs(next);
}
void get_all()
{
QuChong.clear();
top =0;
printf("int all[24][6] = {");
char g[10]={'0','1','2','3','4','5','6','\0'};
dfs(g);
printf("}\n");
cout<<endl<<(sizeof(g))<<endl;
cout<<endl<<top<<endl;
}*/



#### LA 3401 Colored Cubes

2016-07-05 11:42:54

#### LA 3401 Colored Cubes 暴力 -

2016-10-12 18:34:56

#### UVALive 3401 Colored Cubes

2014-06-09 00:28:52

#### UVALive - 3401 Colored Cubes

2014-08-26 17:43:38

#### 3401 - Colored Cubes

2015-07-09 15:07:42

#### uvaLive 3401 Colored Cubes 暴力

2016-07-11 16:48:19

#### UVALive - 3401 Colored Cubes 枚举

2015-02-24 12:29:12

#### UVaLive 3401 Colored Cubes 彩色立方体

2015-06-28 00:11:23

#### La 3401

2016-05-17 11:00:50