题意:
给你几个正方体,每个正方体的六个面都涂有颜色,现在你要把这些正方体重新涂色,使得所有正方体都一样(六个面颜色相同,正方体可以旋转),问最少重涂几个面;
思路:
首先,如果正方体不能旋转,那么要怎么涂.很显然,每一面都找出一样的颜色最多的那个颜色,把其他的都涂成这个颜色,那么就是最少的.
但是正方体可以旋转,通过计算我们可以知道,每一个正方体有24种旋转方式,而这个道题正方体最多4个,而且第一个不需要旋转.那么就是总共有24^3种可能性.
那么我们把每种可能性都通过上面那种方法算出最少涂的面,找出最小值:
AC:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 30;
const int INF = 0x3f3f3f3f;
const int dice[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},
};
int n,cnt,ans,cube[N][N],turn[N];
char color[N][N];
int color_to_num(char* str) {
for(int i = 0 ; i < cnt ; i++) {
if(strcmp(color[i], str) == 0)
return i;
}
strcpy(color[cnt] , str);
return cnt++;
}
void judge() {
int total[N];
int sum = 0;
int m;
for(int i = 0 ; i < 6 ;i++) {
memset(total , 0 ,sizeof(total));
m = 0;
for(int j = 0 ; j < n ; j++) {
int temp = dice[turn[j]][i];
total[cube[j][temp]]++;
m = max(m , total[cube[j][temp]]);
}
sum += n - m;
}
ans = min(sum , ans);
}
void dfs(int cur) {
if(cur == n) {
judge();
return;
}
for(turn[cur] = 0 ; turn[cur] < 24 ;turn[cur]++) {
dfs(cur + 1);
}
}
int main() {
while(scanf("%d",&n) && n) {
char str[N];
cnt = 0;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < 6 ; j++) {
scanf("%s",str);
int num = color_to_num(str);
cube[i][j] = num;
}
}
ans = INF;
dfs(1);
printf("%d\n",ans);
}
}