POJ2741 Colored Cubes

Description

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

    color1 color2 color3 color4 color5 color6

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.

Output

For
each dataset, output a line containing the minimum number of faces that
need to be repainted to make the set of cubes identically colored.

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
2
red green gray gray magenta cyan
cyan green gray gray magenta red
2
red green blue yellow magenta cyan
magenta red blue yellow cyan green
3
red green blue yellow magenta cyan
cyan green blue yellow magenta red
magenta red blue yellow cyan green
3
blue green green green green blue
green blue blue green green green
green green green green green sea-green
3
red yellow red yellow red yellow
red red yellow yellow red yellow
red red red red red red
4
violet violet salmon salmon salmon salmon
violet salmon salmon salmon salmon violet
violet violet salmon salmon violet violet
violet violet violet violet salmon salmon
1
red green blue yellow magenta cyan
4
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

4
2
0
0
2
3
4
4
0
16

正解:搜索

解题报告:

  今天考试考了这道题,考场上调试了一会儿,毕竟是半码农题。。。

  考虑对于每一个立方体,如果我确立了一个面为顶面,则还可选择相邻的四个面中的一个,则可唯一确定一个立方体,那么一共有6*4=24种状态。一共有4个立方体,那么显然我只需要保持第一个不动,剩下三个枚举哪种状态就可以了。然后我们再对于四个确立好状态的立方体每个面贪心地染色,可以得出答案。

  对于题目给的颜色名称我直接用map映射到int上去了,然后可以直接编号。

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 #ifdef WIN32
14 #define OT "%I64d"
15 #else
16 #define OT "%lld"
17 #endif
18 using namespace std;
19 typedef long long LL;
20 const int MAXN = 70;
21 int biao[250][60]={
22     {0,1,2,3,4,5},{0,2,4,1,3,5},{0,4,3,2,1,5},{0,3,1,4,2,5},
23     {1,2,0,5,3,4},{1,5,2,3,0,4},{1,0,3,2,5,4},{1,3,5,0,2,4},
24     {2,1,5,0,4,3},{2,0,1,4,5,3},{2,4,0,5,1,3},{2,5,4,1,0,3},
25     {3,4,5,0,1,2},{3,5,1,4,0,2},{3,1,0,5,4,2},{3,0,4,1,5,2},
26     {4,0,2,3,5,1},{4,2,5,0,3,1},{4,5,3,2,0,1},{4,3,0,5,2,1},
27     {5,2,1,4,3,0},{5,1,3,2,4,0},{5,4,2,3,1,0},{5,3,4,1,2,0},
28 };
29 int n;
30 int paint[MAXN][60];
31 int ans,ecnt;
32 int rotat[MAXN],color[MAXN][60];
33 string ch;
34 int col_cnt[MAXN*6];//每种颜色
35 map<string,int>mp;
36
37 inline int getint()
38 {
39        int w=0,q=0;
40        char c=getchar();
41        while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar();
42        if (c==‘-‘)  q=1, c=getchar();
43        while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar();
44        return q ? -w : w;
45 }
46
47 inline void dfs(int d){
48     if(d==n){
49     for(int i=0;i<n;i++) for(int j=0;j<6;j++) color[i][ biao[ rotat[i] ][j] ]=paint[i][j];
50
51     int tot=0;
52     for(int j=0;j<6;j++) {//枚举每个面
53         memset(col_cnt,0,sizeof(col_cnt));
54         int now=0;
55         for(int i=0;i<n;i++){//考虑每个立方体
56         col_cnt[ color[i][j] ]++;
57         now=max(now,col_cnt[color[i][j]]);
58         }
59         tot+=n-now;
60     }
61     ans=min(ans,tot);
62
63     return ;
64     }
65     for(int i=0;i<24;i++) rotat[d]=i,dfs(d+1);
66 }
67
68 inline void work(){
69     while(1) {
70     n=getint(); if(n==0) break;
71     for(int i=0;i<n;i++)
72         for(int j=0;j<6;j++) {
73         cin>>ch;
74         if(mp[ch]!=0) paint[i][j]=mp[ch];
75         else { mp[ch]=++ecnt; paint[i][j]=mp[ch]; }
76         }
77     ans=n*6;rotat[0]=0;//第一个立方体固定不动
78     dfs(1);    printf("%d\n",ans);
79     }
80 }
81
82 int main()
83 {
84   work();
85   return 0;
86 }
时间: 2024-10-20 07:12:25

POJ2741 Colored Cubes的相关文章

UVA 10733 - The Colored Cubes(Ploya)

UVA 10733 - The Colored Cubes 题目链接 题意:一个立方体.n种颜色,问能涂成多少不同立方体 思路:Ploya求解,正方体相应24种不同旋转一一计算出循环个数就可以.和 UVA 10601 - Cubes这题类似 代码: #include <stdio.h> #include <string.h> unsigned long long n; int main() { while (~scanf("%llu", &n) &

uva 1352 Colored Cubes(枚举)

uva 1352 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 sa

【poj2741】 Colored Cubes

http://poj.org/problem?id=2741 (题目链接) 这也是道神题.. 题意:给出n个骰子,每一面都有一种颜色,问最少更改多少个面的颜色可以使所有骰子通过旋转后完全相同. solution  设6个面的编号为1~6,从中选一个作为顶面,再选一个作为正面,那么其它面都可以确定(因为有对面的面也确定了),因此每个骰子有6*4=24种姿态,每种姿态对应一个全排列P,P[i]表示i所在的位置.所以我们手打这24种排列.  接下来看看如何暴力.我们考虑先枚举每个立方体的姿态(第一个作

UVA - 10733 The Colored Cubes (置换)

All 6 sides of a cube are to becoated with paint. Each side is is coated uniformly with one color. When a selectionof n different colors of paint is available, how many different cubes can youmake? Note that any two cubes are onlyto be called "differ

UVaLive 3401 Colored Cubes (暴力)

题意:给定n个立方体,让你重新涂尽量少的面,使得所有立方体都相同. 析:暴力求出每一种姿态,然后枚举每一种立方体的姿态,求出最少值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostrea

UVALIVE 3401 Colored Cubes

翻转立方体 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string>

uva1352 Colored Cubes LA3401

白书第一章例题8 好麻烦! 正方体每面编号为0-5,那么根据顶点和正面,就能确定形态.一共6*4=24种形态. P[i]表示编号i所在位置.比如P[1]=3,表示第二面转到了第四面. 就可以表示出所有形态. 这时候可以手算或者写个函数找出所有形态. 注意选择函数计算,要放到main外面,方便调. 注意到每个形态都可以由基本姿态左旋上旋得到,而左上旋很接近,就可以模块化了. 然后枚举染色情况.取一个正方体不转(作为参考系,套路了),然后枚举其他三个的情况,然后分别计算6个面. #include <

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

Codeforces Round #505 (Div 1 + Div 2 Combined) Partial Solution

从这里开始 题目列表 瞎扯 Problem A Doggo Recoloring Problem B Weakened Common Divisor Problem C Plasticine zebra Problem D Recovering BST Problem E Colored Cubes Problem F Disjoint Triangles Problem G Company Acquisitions 瞎扯 打比赛,发现自己特别菜. 居然还苟且水上紫名 这个号不敢玩了.要努力学习