3401 - Colored Cubes

原创 2015年07月09日 15:07:42
Colored Cubes
Time limit: 3.000 seconds

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 beidentically 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 integernand the body following it consists of n lines. You can assume that1$ \le$n$ \le$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 integern 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 color1color2color3color4color5color6

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:

scarlet green blue yellow magenta cyan 
blue pink green magenta cyan lemon 
purple red blue yellow cyan green 

red green blue yellow magenta cyan 
cyan green blue yellow magenta red 

red green gray gray magenta cyan 
cyan green gray gray magenta red 

red green blue yellow magenta cyan 
magenta red blue yellow cyan green 

red green blue yellow magenta cyan 
cyan green blue yellow magenta red 
magenta red blue yellow cyan green 

blue green green green green blue 
green blue blue green green green 
green green green green green sea-green 

red yellow red yellow red yellow 
red red yellow yellow red yellow 
red red red red red red 

violet violet salmon salmon salmon salmon 
violet salmon salmon salmon salmon violet 
violet violet salmon salmon violet violet 
violet violet violet violet salmon salmon 

red green blue yellow magenta cyan 

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:









16




    题目意思是有n个带颜色的立方体,立方体的每个面都涂有一种颜色。要求重新涂尽量少的颜色使得所有立方体完全相同。也就是说将一个立方体旋转后,使得立方体对应面的颜色相同。由于给你的立方体最多只有4个,暴力枚举的方法可行。枚举则可以通过枚举每个正方体的姿态(将标准姿态定为正面为1、右面为2、顶面为3、底面为4、左面为5、右面为6)即通过旋转来改变姿态,然后对于6个面,选出一个出现次数最多的颜色作为“标准”,将和其颜色不相同的进行重新涂色。当选择一个面朝上时,朝正面的情况有4种,因此每个立方体拥有24种姿态。(立方体的各面顺序:正,右,底,左,后)

当1朝向正面时,立方体的其他面的情况为:{0,2,4,1,3,5}, {0,1,2,3,4,5}, {0,3,1,4,2,5}, {0,4,3,2,1,5};
当2朝向正面时,立方体的其他面的情况为:{1,2,0,5,3,4}, {1,5,2,3,0,4}, {1,3,5,0,2,4}, {1,0,3,2,5,4};
当4朝向正面时,立方体的其他面的情况为:{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朝向正面时,立方体的其他面的情况为:{3,4,5,0,1,2}, {3,5,1,4,0,2}, {3,1,0,5,4,2}, {3,0,4,1,5,2};
当5朝向正面时,立方体的其他面的情况为:{4,0,2,3,5,1}, {4,2,5,0,3,1}, {4,5,3,2,0,1}, {4,3,0,5,2,1};
当6朝向正面时,立方体的其他面的情况为:{5,2,1,4,3,0}, {5,4,2,3,1,0}, {5,1,3,2,4,0}, {5,3,4,1,2,0};
然后将读入的颜色不变,将面的正面朝向情况取匹配颜色(第一组数据:1朝上时,
scarlet面为1,二朝上时,scarlet面为2;)


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

#define fors(i,n) for(int i = 0; i < n; ++i)///将for循环进行宏定义,减少for语句的出现使得代码更加简洁
char maps[4][6][50];///用来存储每个立方体的6个面的颜色数据
int cases[24][6] = {
{1,2,0,5,3,4}, {1,5,2,3,0,4}, {1,3,5,0,2,4}, {1,0,3,2,5,4},
{2,1,5,0,4,3}, {2,0,1,4,5,3}, {2,4,0,5,1,3}, {2,5,4,1,0,3},
{3,4,5,0,1,2}, {3,5,1,4,0,2}, {3,1,0,5,4,2}, {3,0,4,1,5,2},
{4,0,2,3,5,1}, {4,2,5,0,3,1}, {4,5,3,2,0,1}, {4,3,0,5,2,1},
{5,2,1,4,3,0}, {5,4,2,3,1,0}, {5,1,3,2,4,0}, {5,3,4,1,2,0},
{0,2,4,1,3,5}, {0,1,2,3,4,5}, {0,3,1,4,2,5}, {0,4,3,2,1,5}
};///枚举出一个立方体的24种姿态

const int MAX = 1000000;
int Min = MAX, n, data[4];

int color(char ch[5][50])///判断ch的4个面需要涂色的个数
{
    int counts[5] = {0};
    fors(i,n) fors(j,n) ///对两个立方体进行旋转并统计颜色相同的面的数目
        if(!strcmp(ch[i],ch[j]))
            counts[i]++;
    int Max = 0;
    fors(c,n)
        if(counts[c] > counts[Max])///找出一个面使得其与另外一个正方体颜色相同的面的数目最多
            Max = c;
    int flag = 0;
    fors(i,n)
        if(strcmp(ch[i],ch[Max]))///与出现次数最多的颜色进行比较,不同则对重新涂色
            flag++;
    return flag;
}

void mem()///统计所需涂色的最少次数
{
    int sum  = 0;
    fors(i,6)
    {
        char bj[5][50];
        fors(j,n)///将正方体的24种姿态与给出的颜色进行匹配
            strcpy(bj[j],maps[j][cases[data[j]][i]]);
        sum += color(bj);///统计出需要涂色的数目
    }
    if(sum < Min)
        Min = sum;
}

void dfs(int deep)
{
    if(deep == n - 1)
        mem();
    else
    {
        fors(i,24)
        {
            data[deep] = i;
            dfs(deep+1);
        }
    }
}

int main()
{
    while(scanf("%d",&n)&&n!=0)
    {
        fors(i,n) fors(j,6)
            scanf("%s",maps[i][j]);
        Min= MAX;
        dfs(0);
        printf("%d\n",Min);
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

Hoj 2084 The Colored Cubes (polya计数)

第一道 polya计数 的题,最基础的入门题。 题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2084 题意:用n种颜色为正六面体染色,求有多少种不...
  • whyorwhnt
  • whyorwhnt
  • 2013年10月22日 23:16
  • 904

LA 3401 - Colored Cubes

训练指南上的一道例题,看了好长时间才看懂,又是半抄半写的把这道题给写出来了。思路倒是特别简单,但是由于是空间几何,对空间想象力的要求就特别高,正方体在旋转的时候怎么表示的让我看了好长时间。...
  • u010734277
  • u010734277
  • 2013年12月27日 21:00
  • 548

UVALive - 3401 Colored Cubes

好久没写解题回顾了。主要是没什么时间,但是还是一直在刷题,图论刷了70%的知识点,不过感觉长进不是很大,所以觉得还是得一步步来,最近还是先从刘汝佳大白书把前面基础章节刷完然后再决定以后的训练方式吧。 ...
  • rootial
  • rootial
  • 2014年08月26日 17:43
  • 345

UVALive 3401 Colored Cubes

刘汝佳大神书上的题目,每一个方框有24中不同的姿态,第一个方块的姿态不变,然后去枚举后面...
  • xiaohaowudi
  • xiaohaowudi
  • 2014年06月09日 00:28
  • 341

LA 3401 Colored Cubes

题目http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=30485题解 本来这道题一开始分析的时候有两种想法,第一是枚举每个立方...
  • QWsin
  • QWsin
  • 2016年07月05日 11:42
  • 243

uvaLive 3401 Colored Cubes 暴力

题意:给出n个立方体,(n 解法:暴力枚举,难在编写程序。 一个立方体有24个姿势,因为上面有6中可能,难后前面又有4中可能,故有24种可能。 现在让第一个立方体任选一种姿势,然后枚举其他立方体...
  • yskyskyer123
  • yskyskyer123
  • 2016年07月11日 16:48
  • 160

LA 3401 Colored Cubes 暴力 -

题目地址:http://vjudge.net/problem/UVALive-3401 #include using namespace std; #define REP(i,a,b) for(i...
  • qq_34446253
  • qq_34446253
  • 2016年10月12日 18:34
  • 104

UVALive - 3401 Colored Cubes 枚举

题目大意:有n个带颜色的立方体,每个面都涂有一种颜色,要求重新涂尽量少的面,使得所有的立方体都相同,两个立方体相同的含义是:存在一种旋转方式,使得两个立方体对应的颜色相同 解题思路:枚举n个立方体的...
  • L123012013048
  • L123012013048
  • 2015年02月24日 12:29
  • 273

UVaLive 3401 Colored Cubes 彩色立方体

题意:有n个带颜色的立方体,每个立方体每个面都涂有一种颜色。要求重新涂尽量少的面,使得所有立方体完全相同。两个立方体完全相同是指存在一种旋转方式,使得这两个立方体对应面颜色完全一样。 ...
  • FirstLucker
  • FirstLucker
  • 2015年06月28日 00:11
  • 396

Colored Cubes

Description There are several colored cubes. All of them are of the same size but they may be color...
  • DoriLeigh
  • DoriLeigh
  • 2015年02月06日 16:09
  • 183
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:3401 - Colored Cubes
举报原因:
原因补充:

(最多只允许输入30个字)