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.


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


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


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 

Sample Output:



当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};

#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}

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) ///对两个立方体进行旋转并统计颜色相同的面的数目
    int Max = 0;
        if(counts[c] > counts[Max])///找出一个面使得其与另外一个正方体颜色相同的面的数目最多
            Max = c;
    int flag = 0;
    return flag;

void mem()///统计所需涂色的最少次数
    int sum  = 0;
        char bj[5][50];
        sum += color(bj);///统计出需要涂色的数目
    if(sum < Min)
        Min = sum;

void dfs(int deep)
    if(deep == n - 1)
            data[deep] = i;

int main()
        fors(i,n) fors(j,6)
        Min= MAX;
    return 0;



UVALive - 3401 Colored Cubes

Colored Cubes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu...

UVALive 3401 Colored Cubes


UVa-3401 Colored Cubes

题目概述: 有n个带颜色的立方体,每个面都涂有一种颜色。要求重新涂尽量少的面,使得所有立方体完全相同。两个立方体相同的含义是:存在一种旋转方式,是的两个立方体对应面的颜色相同。 思路: 每一个立...

Tokyo 2005 / UVa 1352 Colored Cubes (部分枚举&贪心)

1352 - Colored Cubes Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlin...

uva1352 Colored Cubes

  • sdfzyhx
  • sdfzyhx
  • 2016年11月07日 21:44
  • 70

HOJ The Colored Cubes

The Colored Cubes My Tags   (Edit)   Source : UVA   ...

【Jason's_ACM_解题报告】 Colored Cubes

Colored Cubes 解题报告

【poj2741】 Colored Cubes

http://poj.org/problem?id=2741 (题目链接)这也是道神题。。题意:给出n个骰子,每一面都有一种颜色,问最少更改多少个面的颜色可以使所有骰子通过旋转后完全相同。soluti...

数据结构实验之排序四:寻找大富翁 sdut oj (3401)

您举报文章:3401 - Colored Cubes