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.不变 ...
随机推荐
- 使用“rz -be”命令上传文件至服务器;使用“sz 文件名”从服务器下载文件到本地
注意:需要事先安装lrzsz服务 yum install -y lrzsz 因为服务器没有安装sftp服务,无法使用FileZilla.Xftp等连接服务器上传文件,这种情况可以利用rz命令上传文件. ...
- jquery如何获取checkbox的值
jquery如何获取checkbox的值 一.总结 一句话总结:就是通过jquery获取哪些对应name的checkbox,然后找出:check(被选中的),然后通过jquery的each遍历获取这些 ...
- ubuntu解压和压缩文件
.tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gun ...
- spring boot+freemarker+spring security标签权限判断
spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...
- 管家基因 | Human housekeeping genes
管家基因就是在细胞里稳定表达的基因,及时在胁迫状态下,表达的差异也不大. 以前做实验的时候就经常听说管家基因,因为在做RT-PCR的时候需要同时检测管家基因,这样可以用于矫正我们不同批次的结果. Li ...
- Hibernate二级缓存 --Hibernate框架
Hibernate本身只提供了二级缓存的规范,但并未实现,所以需要第三方缓存插件的支持.常用的二级缓存第三方插件有:EHCache.Memcached.OSCache.SwarmCache.JBoss ...
- pytorch backward问题
pytorch中关于backward的很有意思的一个问题 <https://blog.csdn.net/shiheyingzhe/article/details/83054238> 但是我 ...
- Python while循环实现重试
try: pass#要执行的代码 except: 状态=True while 状态==True: try: winsound.Beep(800, 1000)#报警提示音 循环=300 while 循环 ...
- 【oauth2.0】【1】简单介绍
含义: OAuth是一个关于授权(authorization)的开放网络标准,2.0是当前版本.不是技术,而是一项资源授权协议. OAuth在"客户端"与"服务提供商&q ...
- 【MySQL】【1】表中存在重复记录,删除保留其中一条
--删除题库(TABLE_Q )中,标题(TITLE )和类型(TYPE )都相同的数据,仅保留ID最小的一条 DELETE TABLE_Q FROM TABLE_Q, ( ) T2 WHERE TA ...