题目

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. 力扣(LeetCode)69. x 的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  2. js 高级知识点

    在JS中统计函数执行次数与执行时间 详解JS中统计函数执行次数与执行时间 JavaScript是如何工作的:深入类和继承内部原理 + Babel和TypeScript 之间转换 javascript设 ...

  3. HTML 标记 3 —— CSS

    <style type="text/css">body { background-color: #F00;} p{ color:#0F0; } .自己定义 { colo ...

  4. Java se基础(类的属性及关键字)

    public:说明该类的访问类型是公有的,它生成的对象能被其他的对象调用! abstract:用来声明抽象类! final;如果一个类被声明成final类型,那么就不能再由它派生出子类. 可以简单的看 ...

  5. feature map 大小以及反卷积的理解

    (1)边长的计算公式是:  output_h =(originalSize_h+padding*2-kernelSize_h)/stride +1 输入图片大小为200×200,依次经过一层卷积(ke ...

  6. Anaconda 简单介绍 -- 环境管理

    前面介绍了 Anaconda 的安装,接下来介绍一下 简单使用,后续并实时更新. 常用操作命令: 环境操作 1.查看环境管理的全部命令帮助: conda env -h 2.查看当前系统下的环境: co ...

  7. Genome-wide gene-environment analyses of depression and reported lifetime traumatic experiences in UK Biobank

    Genome-wide gene-environment analyses of depression and reported lifetime traumatic experiences in U ...

  8. SSH免密钥登陆

    local ipaddress:10.47.39.7:remote ipaddress:10.47.39.8 1.生成公钥和私钥 [root@local ~]# ssh-keygen -t rsa  ...

  9. 20170907wdVBA_ImportPicturesBaseOnExcel

    Public Sub ImportPicturesBaseOnExcel() Dim shp As Object Dim xlApp As Object Dim Wb As Object Dim Rn ...

  10. 关于OkHttp同步请求的小错误

    今天进行OkHttp的同步请求 写的都是按照官方的去写的 但是返回的东西却不是我想要的 原因是我直接拿到Response后,直接Response.toString,想要拿到返回值 但是这样是错误的,正 ...