UVALive 3401 - Colored Cubes 旋转 难度: 1
题目
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的更多相关文章
- UVALive - 3401 Colored Cubes
好久没写解题回顾了.主要是没什么时间,但是还是一直在刷题,图论刷了70%的知识点,不过感觉长进不是很大,所以觉得还是得一步步来,最近还是先从刘汝佳大白书把前面基础章节刷完然后再决定以后的训练方式吧. ...
- UVaLive 3401 Colored Cubes (暴力)
题意:给定n个立方体,让你重新涂尽量少的面,使得所有立方体都相同. 析:暴力求出每一种姿态,然后枚举每一种立方体的姿态,求出最少值. 代码如下: #pragma comment(linker, &qu ...
- LA 3401 - Colored Cubes
解题报告:有n(1<=n<=4)个立方体,每个立方体的每一个面涂有一种颜色,现在要将这些立方体的某些面的颜色重新涂一下,使得这n个立方体旋转到某一种状态下,对应的面的颜色都相同. 这题可以 ...
- 1352 - Colored Cubes (枚举方法)
There are several colored cubes. All of them are of the same size but they may be colored differentl ...
- UVA 10733 - The Colored Cubes(Ploya)
UVA 10733 - The Colored Cubes 题目链接 题意:一个立方体.n种颜色,问能涂成多少不同立方体 思路:Ploya求解,正方体相应24种不同旋转一一计算出循环个数就可以.和 U ...
- POJ2741 Colored Cubes
Description There are several colored cubes. All of them are of the same size but they may be colore ...
- UVALive 3401 彩色立方体
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【poj2741】 Colored Cubes
http://poj.org/problem?id=2741 (题目链接) 题意 给出n个骰子,每一面都有一种颜色,问最少更改多少个面的颜色可以使所有骰子通过旋转后完全相同. solution 迷之d ...
- uva 10733 The Colored Cubes<polya定理>
链接:http://uva.onlinejudge.org/external/107/10733.pdf 题意: N 种颜色可以涂成多少种立方体~ 思路: 使正六面体保持不变的运动群总共有: 1.不变 ...
随机推荐
- 常见字符集&乱码问题
字符集 常用字符集分类 ASCII及其扩展字符集 作用:表语英语及西欧语言. 位数:ASCII是用7位表示的,能表示128个字符:其扩展使用8位表示,表示256个字符. 范围:ASCII从00到7F, ...
- Qt基本布局(QLayout)
概述 Qt提供了QHBoxLayout类(水平排列布局),QVBoxLayout类(垂直排列布局),QGridLayout类(网格排列布局)等基本布局管理.它们之间的继承关系如下图 布局中常用的方法有 ...
- (转)c# String与StringBuilder
阅读目录 1.什么时候用String?什么时候用StringBuilder? 2.String与StringBuilder的区别 总结 1.什么时候用String?什么时候用StringBuild ...
- GrindEQ Math Utilities 2015破解版 图文安装和序列号补丁激活教程
GrindEQ Math Utilities 2015破解版 图文安装和序列号补丁激活教程 https://www.sdbeta.com/mf/2018/1002/226048.html 软件下载: ...
- spring cloud: zuul: 微网关-简单使用与路由配置
spring cloud: zuul: 微网关-简单使用与路由配置 首先引入依赖 <dependency> <groupId>org.springframework.cloud ...
- idea ----> 使用idea工具整合mybaiti时出现的问题总结
使用idea测试mabtis实例时出现 java.lang.IllegalArgumentException: Mapped Statements collection does not conta ...
- gcc -02引起内存溢出'unsigned i'应修订为'volatile unsigned i'
2017-12-13 10:44:19gcc -02引起内存溢出'unsigned i'应修订为'volatile unsigned i' 1.3.100 driver/char/random.cst ...
- Tensor RT使用记录
Tensor RT的介绍在此不做赘述. 自己在服务器上本打算装Tensor RT来着,不过过程很艰辛,最后发现服务器的cudnn版本偏低了,还需要升级cudnn的版本.故,在自己的电脑上了装了下Ten ...
- php &符的写法
foreach ($expert as &$value) { $value['z_thumbs'] = $_W['attachurl'].$value['z_thumbs']; } forea ...
- 原生JS实现瀑布流布局
瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部. 1.首先瀑布流所有的图片应该保持宽度一致,高 ...