Description
Input
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
0
2
red green gray gray magenta cyan
cyan green gray gray magenta red
3
red green blue yellow magenta cyan
cyan green blue yellow magenta red
magenta red blue yellow cyan green
0
Sample Output
4
2
0
2
analysis
这是一道搜索题(再次暴露了我搜索技巧的极度不成熟)
一开始写的是枚举哪些面该被重新染色,结果死活调不出来。
其实只需要枚举正方体的放置方法
然后一比对就好了
要用哈希来搞一搞颜色。
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
char s[500],s1;
int n,i,l,j,ans,h,k,t,cnt,d[50],q[150][3];
ll a[5][10];
const int b[24][6]=
{{1,2,3,4,5,6},
{4,2,1,6,5,3},
{6,2,4,3,5,1},
{3,2,6,1,5,4},
{5,3,6,1,4,2},
{1,3,5,2,4,6},
{2,3,1,6,4,5},
{6,3,2,5,4,1},
{4,6,2,5,1,3},
{5,6,4,3,1,2},
{3,6,5,2,1,4},
{2,6,3,4,1,5},
{1,5,4,3,2,6},
{3,5,1,6,2,4},
{6,5,3,4,2,1},
{4,5,6,1,2,3},
{2,4,6,1,3,5},
{1,4,2,5,3,6},
{5,4,1,6,3,2},
{6,4,5,2,3,1},
{3,1,2,5,6,4},
{5,1,3,4,6,2},
{4,1,5,2,6,3},
{2,1,4,3,6,5}};
const int mo1=1000000007;
const int mo2=1000000003;
const int mo=97;
void dfs(int x){
int i,j,k;
if (x<=n) {
fo(i,1,24) {
d[x]=i;
dfs(x+1);}
} else {
int t,s;
t=0;
fo(i,0,5) {
s=0;
fo(j,1,n-1) {
h=0;
fo(k,j+1,n) {
if (a[j][b[d[j]-1][i]]==a[k][b[d[k]-1][i]]) h++;
}
s=max(s,h);
}
t+=(n-s-1);
}
ans=min(ans,t);
}
}
int get(ll x,ll y){
int t=x&mo;
while (!(q[t][1]==-1||(q[t][1]==x&q[t][2]==y))) t=(t+1)%mo;
if (q[t][1]==-1)
q[t][1]=x,q[t][2]=y;
return t;
}
ll hash(){
int i; ll h1,h2;
h1=0; h2=0;
fo(i,1,l) {
h1=(h1*26-'a'+1+s[i])%mo1;
h2=(h2*26-'a'+1+s[i])%mo2;}
return get(h1,h2);
}
int main(){
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
scanf("%d",&n);
while (n){
memset(q,255,sizeof(q));
memset(d,0,sizeof(d));
fo(i,1,n){
fo(h,1,6){
scanf("%s",s+1);
l=strlen(s+1);
a[i][h]=hash();
}}
ans=10000000;
d[1]=1;
dfs(1);
printf("%d\n",ans);
scanf("%d",&n);
}
}