uva1352 Colored Cubes
来源:互联网 发布:淘宝关键词top100 编辑:程序博客网 时间:2024/06/06 09:44
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(); }}
- UVa1352 - Colored Cubes
- uva1352 Colored Cubes
- 【UVA1352】Colored Cubes
- uva1352 - Colored Cubes 打表+暴力
- uva1352-Colored-Cubes 精选技巧2
- Colored Cubes
- Colored Cubes
- Colored Cubes
- HOJ The Colored Cubes
- uva 1352 - Colored Cubes
- 1352 - Colored Cubes
- LA 3401 - Colored Cubes
- UVALive 3401 Colored Cubes
- UVALive - 3401 Colored Cubes
- UVALive - 3401 Colored Cubes
- 【uva】1352-Colored Cubes
- LA 3401 - Colored Cubes
- 3401 - Colored Cubes
- Dijkstra(迪杰斯特拉)算法
- java集合概述
- POJ 3734 Blocks(矩阵快速幂)
- MD5加密的util
- Error: Your project contains C++ files but it is not using a supported native build system
- uva1352 Colored Cubes
- Codeforces 389D Fox and Minimal path【构造+二进制思维】好题!
- 百度地图在scrollview里面滑动冲突解决以及跳转到百度地图app的方法
- Linux字符设备驱动
- C——变量
- 启动Tomcat提示:指定的服务未安装
- Redis 集群方案介绍
- 动态规划问题CombinationSum
- Matlab匿名函数使用规则