https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1402
http://7xjob4.com1.z0.glb.clouddn.com/53f6b2526cc5a59ec7881a8fd6d899bd
题意:将n个原有颜色的立方体涂尽量少次使立方体都相同;
思路:枚举除第一个面外每个立方体的姿态(24种),姿态由旋转方式先处理得到,再枚举每一个对应面记录要涂的数量。
处理代码:
#include <bits/stdc++.h>
using namespace std; int lleft[]={,,,,,,};
int up[]={,,,,,,}; void rot(int *T,int *p)
{
int q[];
for(int i=;i<=;i++)
{
q[i]=p[i];
} for(int i=;i<=;i++)
{
p[i]=q[T[i]];
}
} int main()
{
int p0[]={,,,,,,}; printf("int dice[25][7]= {\n{0},\n");
for(int i=;i<=;i++)
{
int p[];
for(int j=;j<=;j++)
p[j]=p0[j]; if(i==) rot(up,p);
if(i==) { rot(lleft,p); rot(up,p); } if(i==) { rot(up,p);rot(up,p); }
if(i==) { rot(lleft,p);rot(lleft,p);rot(lleft,p);rot(up,p); }
if(i==) { rot(lleft,p);rot(lleft,p);rot(up,p); } for(int j=;j<=;j++)
{
printf("{%d,%d,%d,%d,%d,%d,%d},",p[],p[],p[],p[],p[],p[],p[]);
rot(lleft,p);
}
printf("\n");
}
printf("}; \n");
}
计算代码:
#include <bits/stdc++.h>
#include <iostream>
using namespace std; int dice[][]= {
{},
{,,,,,,},{,,,,,,},{,,,,,,},{,,,,,,},
{,,,,,,},{,,,,,,},{,,,,,,},{,,,,,,},
{,,,,,,},{,,,,,,},{,,,,,,},{,,,,,,},
{,,,,,,},{,,,,,,},{,,,,,,},{,,,,,,},
{,,,,,,},{,,,,,,},{,,,,,,},{,,,,,,},
{,,,,,,},{,,,,,,},{,,,,,,},{,,,,,,},
}; int n,ans;
int color[][],state[];
vector <string> colorname; int colorid(char str[])
{
int i,j;
string s(str);
int m=colorname.size();
for(i=;i<m;i++)
{
if(colorname[i]==s)
{
return i;
}
}
colorname.push_back(s);
return m;
} void cal()
{
int i,j;
int num=,maxnum;
map <int,int> cn;
for(j=;j<=;j++)
{
cn.clear();
maxnum=;
for(i=;i<=n;i++)
{
int cnam=color[i][dice[state[i]][j]];
cn[cnam]++;
if(cn[cnam]>maxnum)
maxnum=cn[cnam];
}
num+=(n-maxnum);
}
if(ans>num)
ans=num;
} void dfs(int m)
{
int i,j;
if(m==n)
{
cal();
return;
}
for(i=;i<=;i++)
{
state[m+]=i;
dfs(m+);
}
}
int main()
{
char str[];
int i,j;
while(scanf("%d",&n)!=EOF && n!=)
{
colorname.clear();
for(i=;i<=n;i++)
{
for(j=;j<=;j++)
{
scanf("%s",str);
color[i][j]=colorid(str);
}
} if(n==)
{
ans=;
}
else
{
ans=;
state[]=;
dfs();
} printf("%d\n",ans);
}
return ;
} /*
for(i=1;i<=n;i++)
{
for(j=1;j<=6;j++)
{
printf("%s ",color[i][j]);
}
printf("\n");
}
if(strcmp(color[1][2],color[2][3])==0)
{
printf("yes\n");
}
*/