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. C# 发送邮件,QQ企业邮箱测试成功

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  2. Windows 8.1 新增控件之 Flyout

    本篇为大家介绍Flyout 控件,Flyout 属于一种轻量级交互控件,可以支持信息提示或用户交互.与传统Dialog 控件不同的是Flyout 控件可通过直接点击对话框外部区域忽略. Flyout ...

  3. Centos6.4安装erlang并配置mysql数据库

    在安装时,一定要使用Centos6.4光盘为yum源,否则可能使用了版本有问题的openssl 1.首先要先安装GCC GCC-C++ Openssl等依赖模块: yum -y install mak ...

  4. mybatis3.2.8 与 hibernate4.3.6 混用

    mybatis.hibernate这二个框架各有特色,对于复杂的查询,利用mybatis直接手写sql控制起来更灵活,而一般的insert/update,hibernate比较方便.同一个项目中,这二 ...

  5. TinyFrame升级之七:重构Repository和Unit Of Work

    首先,重构的想法来源于以下文章:Correct use of Repository and Unit Of Work patterns in ASP.NET MVC,因为我发现在我的框架中,对Unit ...

  6. SQL基础之数据库快照

    1.认识快照 如名字一样,数据库快照就可以理解为数据库某一时刻的照片,它记录了此时数据库的数据信息.如果要认识快照的本质,那就要了解快照的工作原理.当我们执行t-sql创建快照后,此时就会创建一个或多 ...

  7. JavaScript学习笔记-商品管理新增/删除/修改功能

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  8. vs2012 发布网站,

    如图这样选择就没有可以得到一个不包括 *.aspx.cs 的网站了.

  9. MyEclipse10连接数据库

    连接oracle数据库 DB窗口>>右键:新建

  10. Mybatis学习--Mapper.xml映射文件

    简介 Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 映射文件中有很多属性,常用的就是parameterType(输入类型 ...