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 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 1n4
. 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.

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

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

题目大意:给出n个正方体,然后n行表示每个正方体6个面的上色,问涂最少的面使得n正方体都相同(注意正方体是可以旋转的)。

解题思路:首先写出正方体有24个旋转方式,然后以第一个正方体为标准,枚举剩下n - 1个正方体的状态,然后计算最小值。具体见代码注释。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int dice[24][6] = {  //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},
	{4, 2, 5, 0, 3, 1}, {5, 2, 1, 4, 3, 0}, {1, 2, 0, 5, 3, 4}, {0, 2, 4, 1, 3, 5},
	{0, 1, 2, 3, 4, 5}, {4, 0, 2, 3, 5, 1}, {5, 4, 2, 3, 1, 0}, {1, 5, 2, 3, 0, 4},
	{5, 1, 3, 2, 4, 0}, {1, 0, 3, 2, 5, 4}, {0, 4, 3, 2, 1, 5}, {4, 5, 3, 2, 0, 1},
	{1, 3, 5, 0, 2, 4}, {0, 3, 1, 4, 2, 5}, {4, 3, 0, 5, 2, 1}, {5, 3, 4, 1, 2, 0},
	{3, 4, 5, 0, 1, 2}, {3, 5, 1, 4, 0, 2}, {3, 1, 0, 5, 4, 2}, {3, 0, 4, 1, 5, 2},
};
char color[30][30];
int cnt, site[30][30], c[30], ans, n;
int ID(char *str) {
	for (int i = 0; i < cnt; i++) {
		if (strcmp(color[i], str) == 0) return i; //与之前的颜色序号进行对比,若有相同颜色,返回该颜色的颜色序号
	}
	strcpy(color[cnt], str);
	return cnt++;  //若第一方块中不存在该颜色或本身是第一方块,创建并返回新的颜色序号
}
void check() {
	int v[30], sum = 0;
	for (int i = 0; i < 6; i++) {
		memset(v, 0, sizeof(v));
		int Max = 0;
		for (int j = 0; j < n; j++) {
			int temp = dice[c[j]][i]; //获取旋转过后处于第i序号面的面序号
			v[site[j][temp]]++; //统计各方块各面相同颜色数
			Max = max(Max, v[site[j][temp]]); //Max为各方块在第i序号面中相同颜色的个数
		}
		sum += n - Max; //sum为该旋转方法的颜色更新数
	}
	ans = min(sum, ans); //统计24种旋转方法中最小的颜色更新数
}
void DFS(int a) {
	if (a >= n) {
		check();
		return;
	}
	for (c[a] = 0; c[a] < 24; c[a]++) {  //枚举24种旋转情况
		DFS(a + 1);
	}
}
int main() {
	while (scanf("%d", &n) == 1, n) {
		cnt = 0;
		ans = 9999;
		memset(color, 0, sizeof(color));
		memset(c, 0, sizeof(c));
		char str[30];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 6; j++) {
				scanf("%s", str);
				int num = ID(str);
				site[i][j] = num; //该数组存储的是第i个方块第j个序号面的颜色序号
			}
		}
		DFS(1);
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-11-13 20:16:07

uva 1352 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) &

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 id

UVa 725 Division --- 简单枚举

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666 /* UVa 725 Division --- 简单枚举 */ #include <cstdio> #include <cstring> bool used[10]; /* 判断传进来的两个数是否满足条件 */ bool judge(int a, i

uva Fire Station(FLODY+枚举)(挺不错的简单题)

消防站 题目链接:Click Here~ 题意分析: 就是给你f个消防站,n个路口.要你求出在已有消防站的基础上在n个路口的哪个路口上在建立一个消防站,使得n个路口的到离自己最近的消防站最近的距离中最大的一个值最小.即:求n个最近路口中最大的一个,使其改最大值最小.详细的要求自己看题目吧~ 算法分析: 因为,是n个路口到每个消防站的距离.所以,我们可以想到先用一次Flody算法.把每两点的最近距离给算出来.之后在枚举N个路口,进行判断比较得出答案. #include <iostream> #i

UVA - 10051Tower of Cubes(递推)

题目: UVA - 10051Tower of Cubes(递推) 题目大意:给出N个正方体1-N,只有序号小的正方体可以放在序号大的正方体的上面,并且除了最底下的那个正方体,其他的正方体的底面要和它下面的正方体的上面颜色相同.问怎样组合才能使得用的正方体个数越多.并且输出其中的一种堆放方式. 解题思路:一开始觉得是用DAG上的DP来做,结果状态开太多dp[N][N][M](N代表正方体个数, M代表六个面),最后输出路径的时候超时了.后面看了别人的题解,发现状态只需要开dp[N][M]就足够了

UVA 10574 - Counting Rectangles(枚举+计数)

10574 - Counting Rectangles 题目链接 题意:给定一些点,求能够成几个矩形 思路:先把点按x排序,再按y排序,然后用O(n^2)的方法找出每条垂直x轴的边,保存这些边两点的y坐标y1, y2.之后把这些边按y1排序,再按y2排序,用O(n)的方法找出有几个连续的y1, y2都相等,那么这些边两两是能构成矩形的,为C2cnt种,然后累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <

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

UVa 725 - Division(枚举简单题)

今日无事,写篇日记.这是我写的第一道uva题(uva是算法竞赛入门经典里使用例题的题目来源),正值暑假,前几天看了书上中文题目和解析,玩了几日san11,又重拾起acm,今天晚上写了一下还是花了点时间. 题目:给你一个数字n,用0~9,10个数字组成两个五位数,使得他们的商为n,按顺序输出所有结果. 1.这里的除法不是c中的除(/)而是整除,一开始以为是整除,但那样79546 / 01283 = 62是一组解,79546+1 / 01283 = 62也是一组解了. 2.复杂度:枚举分子然后分子乘

uva 1508 Equipment(暴力+枚举子集)

uva 1508 Equipment 题目大意:给出n个5元组,要求从中选取k个,要求5个位置上的数的最大值的和尽量大. 解题思路:一开始没思路,看别人题解过的.5位可以组成32种情况,在DFS前预处理,找出32种情况在n组数据中的最大值,然后将这组数据带入DFS中枚举计算. #include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> using namespace