http://poj.org/problem?id=2741 (题目链接)

题意

  给出n个骰子,每一面都有一种颜色,问最少更改多少个面的颜色可以使所有骰子通过旋转后完全相同。

solution

  迷之dfs。

  设6个面的编号为1~6,从中选一个作为顶面,再选一个作为正面,那么其它面都可以确定(因为有对面的面也确定了),因此每个骰子有6*4=24种姿态,每种姿态对应一个全排列P,P[i]表示i所在的位置。所以我们手打这24种排列。

  接下来看看如何暴力。我们考虑先枚举每个立方体的姿态(第一个作为“参考系”,不用枚举),然后对于6个面分别选一个出现次数最多的颜色作为“标准”,和它不同的颜色一律重新上色。那么姿态的组合一共有24³(注意第一个立方体不用枚举),算上常数,应该可以过。

  再有就是读入的时候,将字符串全部转换成数字,方便操作。

代码

// poj2741
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<vector>
#define LL long long
#define inf 2147483640
#define Pi 3.1415926535898
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; vector<string> names;
int dice24[24][6]= {
{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},
};//24种姿态 int n,ans,dice[4][6],r[6],color[4][6]; int ID(char* name) {
string s(name);
int n=names.size();
for (int i=0;i<n;i++) if (names[i]==s) return i;
names.push_back(s);
return n;
}
void check() {
for (int i=0;i<n;i++)
for (int j=0;j<6;j++) color[i][dice24[r[i]][j]]=dice[i][j];
int tot=0;
for (int j=0;j<6;j++) {
int cnt[24];
memset(cnt,0,sizeof(cnt));
int maxface=0;
for (int i=0;i<n;i++)
maxface=max(maxface,++cnt[color[i][j]]);//找出每一面重复次数最多的颜色
tot+=n-maxface;
}
ans=min(ans,tot);
}
void dfs(int d) {
if (d==n) check();
else for (int i=0;i<24;i++) {
r[d]=i;//第d个立方体的姿态
dfs(d+1);
}
}
int main() {
while (scanf("%d",&n)!=EOF && n) {
names.clear();
for (int i=0;i<n;i++)
for (int j=0;j<6;j++) {//将字符串转换为数字记录
char name[30];
scanf("%s",name);
dice[i][j]=ID(name);//记录第i个立方体第j个面上的颜色
}
ans=n*6;//赋初值为最大
r[0]=0;
dfs(1);
printf("%d\n",ans);
}
return 0;
}

  

【poj2741】 Colored Cubes的更多相关文章

  1. 【codeforces 1025E】Colored Cubes 【构造】

    题意 有一个n*n的棋盘和m个棋子,每个棋子有一个初始位置和一个目标位置,每次移动只能选择一个棋子移动到他相邻的格子,并且花费一秒钟.请你找出一个移动的方法,使得在10800步内将所有棋子移动到目标位 ...

  2. 【CF1068C】Colored Rooks(构造)

    题意: 思路: #include<cstdio> #include<cstring> #include<string> #include<cmath> ...

  3. 【arc062e】Building Cubes with AtCoDeer

    Description STL有n块瓷砖,编号从1到n,并且将这个编号写在瓷砖的正中央: 瓷砖的四个角上分别有四种颜色(可能相等可能不相等),并且用Ci,0,Ci,1,Ci,2,Ci,3分别表示左上. ...

  4. 【CF792E】Colored Balls(数论分块)

    题目链接 大意 有\(N\)种颜色的球,第\(i\)种球有\(Ai\)个,要求把球分成几个集合,使得: 一个集合里的球只能有一种颜色. 任意两个集合的球的数量相差不能超过1. 求这些球至少需要分几个集 ...

  5. 【CodeForces】679 B. Bear and Tower of Cubes

    [题目]B. Bear and Tower of Cubes [题意]有若干积木体积为1^3,2^3,...k^3,对于一个总体积X要求每次贪心地取<=X的最大积木拼上去(每个只能取一次)最后总 ...

  6. POJ2741 Colored Cubes

    Description There are several colored cubes. All of them are of the same size but they may be colore ...

  7. 【转】欧拉回路&特殊图下的哈密顿回路题集

    转自:http://blog.csdn.net/shahdza/article/details/7779385 欧拉回路[HDU]1878 欧拉回路 判断3018 Ant Trip 一笔画问题1116 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. 【POJ3294】 Life Forms (后缀数组+二分)

    Life Forms Description You may have wondered why most extraterrestrial life forms resemble humans, d ...

随机推荐

  1. 程序流程的控制之条件分支(Delphi)

    if语句主要来检测一个条件,并根据这个条件是True或者False来执行一段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 var   I: Integer ...

  2. BZOJ 1009 【HNOI2008】 GT考试

    Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2...Am(0< ...

  3. Linux shell循环

    条件测试 格式 test condition 或 [ condition ] 使用方括号时,要注意在条件两边加上空格,如果有操作符,运算符之间也必须有空格 测试状态:测试的结果可以用$?的值来判断,0 ...

  4. QT 对话框一

    标准文件对话框 其函数形式如下:: QString QFileDialog::getOpenFileName ( QWidget * parent=, const QString &capti ...

  5. codevs3145 汉诺塔问题

    难度等级:白银 3145 汉诺塔问题 题目描述 Description 汉诺塔问题(又称为河内塔问题),是一个大家熟知的问题.在A,B,C三根柱子上,有n个不同大小的圆盘(假设半径分别为1-n吧),一 ...

  6. 2014-2015-2 《Java程序设计》课程学生博客列表

    20135101 曹钰晶 20135103 王海宁 20135104 刘 帅 20135105 王雪铖 20135109 高艺桐 20135111 李光豫 20135114 王朝宪 20135116 ...

  7. ASP.NET Web API Help Pages using Swagger

    Understanding the various methods of an API can be a challenge for a developer when building a consu ...

  8. 写个PHP框架吧

    肯定会问:现在的PHP框架那么多了,为什么还要写一个PHP框架呢? 1.时代:PHP7来了,现在的所有框架都是基于PHP5.x的.到时候PHP7正式推广出来,现有的框架都不能发挥PHP7的最大性能优势 ...

  9. 在eclipse中使用第三方库总结

    一.建立user library 导入第三方jar文件,最简单的方式是:右键工程/属性/java build path/add external jars. 另一种方式是:window/prefren ...

  10. C++ redirect input

    #include<iostream> #include<string> #include<fstream> using namespace std; int mai ...