题目

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1402

题意

4个方块,每个方块每个面涂不同的颜色,问最少重涂多少面,使四个方块相同。

思路

如刘书思路,明显,方块是可以旋转的。旋转的方式不可能多。旋转的基本操作可以定为右旋和上旋,其他所有旋转都是这两种子操作的集合。

通过搜索确定所有可能的旋转方式后,接下来就是确定该如何涂色。

选定第一个方块作为参考系,对剩下的方块分别尝试一下可能的旋转方式。确定旋转方式后,答案就很简单的相加即得。

感想

1. 三倍ice cream!

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <tuple>
#include <set>
#include <map>
#include <cassert>
#define LOCAL_DEBUG
using namespace std;
const int MAXN = ;
int rightPositions[] = { , , , , , };
int topPositions[] = { , , , , , };
int n; class Status {
public:
int faces[];
Status() {
for (int i = ; i < ; i++) {
faces[i] = i;
}
}
Status(int _faces[]) {
for (int i = ; i < ; i++) {
faces[i] = _faces[i];
}
} bool operator ==(const Status & other)const {
for (int i = ; i < ; i++) {
if (faces[i] != other.faces[i])return false;
}
return true;
} bool operator <(const Status & other)const {
for (int i = ; i < ; i++) {
if (faces[i] < other.faces[i])return true;
else if (faces[i] > other.faces[i])return false;
}
return false;
} void rotate(int positions[]) {
int tmp[];
for (int i = ; i < ; i++) {
tmp[i] = faces[i];
}
for (int i = ; i < ; i++) {
faces[i] = tmp[positions[i]];
}
} void reRotate(int positions[]) {
int tmp[];
for (int i = ; i < ; i++) {
tmp[i] = faces[i];
}
for (int i = ; i < ; i++) {
faces[positions[i]] = tmp[i];
}
} Status copy() {
Status newStatus;
for (int i = ; i < ; i++) {
newStatus.faces[i] = faces[i];
}
return newStatus;
}
}; set<Status> statuses; void buildStatus() {
queue<Status> que;
Status status;
que.push(status);
statuses.insert(status);
while (!que.empty()) {
status = que.front(); que.pop();
Status newStatus = status.copy();
newStatus.rotate(rightPositions);
if (statuses.count(newStatus) == ) {
que.push(newStatus);
statuses.insert(newStatus);
}
newStatus = status.copy();
newStatus.rotate(topPositions);
if (statuses.count(newStatus) == ) {
que.push(newStatus);
statuses.insert(newStatus);
}
}
} Status cubes[MAXN];
int cnt[MAXN * ]; int dfs(int id) {
int ans = * MAXN;
if (id < n) {
for (auto status : statuses) {
cubes[id].rotate(status.faces);
ans = min(dfs(id + ), ans);
cubes[id].reRotate(status.faces);
}
}
else {
ans = ;
for (int j = ; j < ; j++) {
for (int i = ; i < n; i++) {
cnt[cubes[i].faces[j]] ++;
}
int best_color = ;
for (int i = ; i < MAXN * ; i++) {
if (cnt[i] > cnt[best_color]) {
best_color = i;
}
}
ans += n - cnt[best_color];
memset(cnt, , sizeof(cnt));
}
}
return ans;
} int main() {
#ifdef LOCAL_DEBUG
freopen("input.txt", "r", stdin);
//freopen("output2.txt", "w", stdout);
#endif // LOCAL_DEBUG
buildStatus();
for (int ti = ; scanf("%d", &n) == && n; ti++) {
map<string, int> colorNameMap;
for (int i = ; i < n; i++) {
for (int j = ; j < ; j++) {
char tmp[];
scanf("%s", tmp);
string colorName = tmp;
if (colorNameMap.count(colorName) == ) {
colorNameMap[colorName] = colorNameMap.size();
}
cubes[i].faces[j] = colorNameMap[colorName];
}
}
int ans = dfs();
printf("%d\n", ans);
} return ;
}

UVALive 3401 - Colored Cubes 旋转 难度: 1的更多相关文章

  1. UVALive - 3401 Colored Cubes

    好久没写解题回顾了.主要是没什么时间,但是还是一直在刷题,图论刷了70%的知识点,不过感觉长进不是很大,所以觉得还是得一步步来,最近还是先从刘汝佳大白书把前面基础章节刷完然后再决定以后的训练方式吧. ...

  2. UVaLive 3401 Colored Cubes (暴力)

    题意:给定n个立方体,让你重新涂尽量少的面,使得所有立方体都相同. 析:暴力求出每一种姿态,然后枚举每一种立方体的姿态,求出最少值. 代码如下: #pragma comment(linker, &qu ...

  3. LA 3401 - Colored Cubes

    解题报告:有n(1<=n<=4)个立方体,每个立方体的每一个面涂有一种颜色,现在要将这些立方体的某些面的颜色重新涂一下,使得这n个立方体旋转到某一种状态下,对应的面的颜色都相同. 这题可以 ...

  4. 1352 - Colored Cubes (枚举方法)

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

  5. UVA 10733 - The Colored Cubes(Ploya)

    UVA 10733 - The Colored Cubes 题目链接 题意:一个立方体.n种颜色,问能涂成多少不同立方体 思路:Ploya求解,正方体相应24种不同旋转一一计算出循环个数就可以.和 U ...

  6. POJ2741 Colored Cubes

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

  7. UVALive 3401 彩色立方体

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【poj2741】 Colored Cubes

    http://poj.org/problem?id=2741 (题目链接) 题意 给出n个骰子,每一面都有一种颜色,问最少更改多少个面的颜色可以使所有骰子通过旋转后完全相同. solution 迷之d ...

  9. uva 10733 The Colored Cubes<polya定理>

    链接:http://uva.onlinejudge.org/external/107/10733.pdf 题意: N 种颜色可以涂成多少种立方体~ 思路: 使正六面体保持不变的运动群总共有: 1.不变 ...

随机推荐

  1. Codeforces 813E - Army Creation

    813E - Army Creation 思路: 线段树+二分 先预处理每个点往后走k步的下标 线段树二叉树的每个节点用vector维护这些下标,给这些下标排个序 询问区间L,R,那么把下标小于等于R ...

  2. h5内容超出可以滑动展示的处理,iscroll的使用

    第一步: 引入js 第二步:页面结构 第三步:使用 dome效果:http://cubiq.org/dropbox/iscroll4/examples/simple/ 文档地址:http://iscr ...

  3. C#动态代理

    所谓代理,就是不直接访问目标对象,而是由中间对象生成一个目标代理类,由中间代理对象来代理目标对象的方法.Java里面有JDK和CGLIB代理.C#里面则使用Castle代理.nuget引用如下: &l ...

  4. ThinkPHP执行原生的SQL语句

    执行原生的SQL语句: $sql="insert select update delete...."; ①查询语句:   $model对象 -> query($sql);  ...

  5. WPF经典编程模式-MVVM示例讲解

    https://www.cnblogs.com/lvdongjie/p/5515962.html

  6. 20180831xlVBA_WorksheetsCosolidate

    Sub WorkSheetsConsolidate() Rem 设置求和区域为 单元格区域;单元格区域 Const Setting As String = "A1;B2:C4" D ...

  7. 57 ORM多表查询

    多表查询from django.db import models# Create your models here. class Author(models.Model): nid = models. ...

  8. wdcp环境redis的位置

  9. CentOS6.8逻辑卷管理实战

    CentOS6.8逻辑卷管理实战 要求:利用现有的四块磁盘,创建一个有两个PV组成的大小为80G的名为testvg的VG:要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv:挂载 ...

  10. FileZilla 客户端连接 FlieZilla 服务器 连接成功读取目录列表却失败的解决办法

    解决过程: 第一步: 第二步: