uva1352 Colored Cubes

There are several colored cubes. All of them are of the same size but
they may be colored differently. Each face of these cubes has a single
color. Colors of distinct faces of a cube may or may not be the same.
Two cubes are said to be identically colored if some suitable
rotations of one of the cubes give identical looks to both of the
cubes. For example, two cubes shown in Figure 2 are identically
colored. A set of cubes is said to be identically colored if every
pair of them are identically colored. A cube and its mirror image are
not necessarily identically colored. For example, two cubes shown in
Figure 3 are not identically colored. You can make a given set of
cubes identically colored by repainting some of the faces, whatever
colors the faces may have. In Figure 4, repainting four faces makes
the three cubes identically colored and repainting fewer faces will
never do. Your task is to write a program to calculate the minimum
number of faces that needs to be repainted for a given set of cubes to
become identically colored. Input The input is a sequence of datasets.
A dataset consists of a header and a body appearing in this order. A
header is a line containing one positive integer n and the body
following it consists of n lines. You can assume that 1 n
4. Each line in a body contains six color names separated by a space. A color name consists of a word or words connected with a hyphen (-).
A word consists of one or more lowercase letters. You can assume that
a color name is at most 24-characters long including hyphens. A
dataset corresponds to a set of colored cubes. The integer n
corresponds to the number of cubes. Each line of the body corresponds
to a cube and describes the colors of its faces. Color names in a line
is ordered in accordance with the numbering of faces shown in Figure
5. A line color 1 color 2 color 3 color 4 color 5 color 6 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.

先预处理出各种旋转的方法【顶上有6种选法,前面在剩下的4个中选一个,共24种转法】,然后保持第一个不转,枚举后几个的转法。复杂度O(3^24)。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
#include<iostream>
using namespace std;
const int oo=0x3f3f3f3f;
int up[]={2,1,5,0,4,3};
int lft[]={4,0,2,3,5,1};
int pos[24][6],n,clr[4][6],ans,num[24];
void rot(int *to,int *p)
{
    int q[6],i;
    memcpy(q,p,sizeof(q));
    for (i=0;i<6;i++)
      p[i]=to[q[i]];
}
void init()
{
    int p0[]={0,1,2,3,4,5},p[6],i,j,k,cnt=0;
    for (i=0;i<6;i++)
    {
        memcpy(p,p0,sizeof(p));
        switch (i)
        {
            case 0:
                rot(up,p);
                break;
            case 1:
                rot(lft,p);
                rot(up,p);
                break;
            case 3:
                rot(up,p);
                rot(up,p);
                break;
            case 4:
                rot(lft,p);
                rot(lft,p);
                rot(lft,p);
                rot(up,p);
                break;
            case 5:
                rot(lft,p);
                rot(lft,p);
                rot(up,p);
                break;
        }
        for (j=0;j<4;j++)
        {
            for (k=0;k<6;k++)
              pos[cnt][k]=p[k];
            rot(lft,p);
            cnt++;
        }
    }
}
void read()
{
    int cnt=0,i,j;
    string s;
    map<string,int> mp;
    for (i=0;i<n;i++)
      for (j=0;j<6;j++)
      {
        cin>>s;
        if (!mp.count(s)) mp[s]=cnt++;
        clr[i][j]=mp[s];
      }
}
void check()
{
    int mx,i,j,ret=0;
    for (i=0;i<6;i++)
    {
        mx=0;
        memset(num,0,sizeof(num));
        for (j=0;j<n;j++)
        {
            num[clr[j][i]]++;
            mx=max(mx,num[clr[j][i]]);
        }
        ret+=n-mx;
    }
    ans=min(ans,ret);
}
void dfs(int p)
{
    if (p==n)
    {
        check();
        return;
    }
    int tem[6],i,j;
    memcpy(tem,clr[p],sizeof(tem));
    for (i=0;i<24;i++)
    {
        for (j=0;j<6;j++)
          clr[p][pos[i][j]]=tem[j];
        dfs(p+1);
        memcpy(clr[p],tem,sizeof(tem));
    }
}
void solve()
{
    ans=oo;
    dfs(1);
    printf("%d\n",ans);
}
int main()
{
    init();
    while (scanf("%d",&n)&&n)
    {
        read();
        solve();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值