UVALIVE 3401 Colored Cubes

翻转立方体

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
int dice24[24][10] = {{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}};
const int MAXN = 4;
int N,dice[10][6],ans;
vector<string>names;
int r[MAXN + 10],color[MAXN + 10][6];
int id(const char * name)
{
        string s(name);
        int n = names.size();
        for (int i = 0 ; i < (int) n ;i++)
                if (s == names[i]) 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 tmp = 0;
                for (int i = 0; i < N ; i++)
                        tmp = max(tmp,++cnt[color[i][j]]);
                tot += N - tmp;
        }
        ans = min(ans,tot);
}
void dfs(int depth)
{
        if (depth == N)check();
        else
        {
                for (int i = 0 ; i <24; i++)
                {
                        r[depth] = i;
                        dfs(depth + 1);
                }
        }
}
int main()
{
        //freopen("sample.txt","r",stdin);
        while (scanf("%d",&N) != EOF)
        {
                if (N == 0) break;
                names.clear();
                for (int i = 0 ; i < N; i++)
                     for (int j = 0 ; j < 6; j++)
                     {
                             char tmp[30];
                             scanf("%s",tmp);
                             dice[i][j] = id(tmp);
                     }
                ans = N * 6;
                r[0] = 0;
                dfs(1);
                printf("%d\n",ans);
        }
        return 0;
}
时间: 2024-10-04 17:30:22

UVALIVE 3401 Colored Cubes的相关文章

UVaLive 3401 Colored Cubes (暴力)

题意:给定n个立方体,让你重新涂尽量少的面,使得所有立方体都相同. 析:暴力求出每一种姿态,然后枚举每一种立方体的姿态,求出最少值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostrea

UVA 10733 - The Colored Cubes(Ploya)

UVA 10733 - The Colored Cubes 题目链接 题意:一个立方体.n种颜色,问能涂成多少不同立方体 思路:Ploya求解,正方体相应24种不同旋转一一计算出循环个数就可以.和 UVA 10601 - Cubes这题类似 代码: #include <stdio.h> #include <string.h> unsigned long long n; int main() { while (~scanf("%llu", &n) &

POJ2741 Colored Cubes

Description There are several colored cubes. All of them are of the same size but they may be colored differently. Each face of these cubes has a single color. Colors of distinct faces of a cube may or may not be the same. Two cubes are said to be id

uva 1352 Colored Cubes(枚举)

uva 1352 Colored Cubes There are several colored cubes. All of them are of the same size but they may be colored differently. Each face of these cubes has a single color. Colors of distinct faces of a cube may or may not be the same. Two cubes are sa

UVA - 10733 The Colored Cubes (置换)

All 6 sides of a cube are to becoated with paint. Each side is is coated uniformly with one color. When a selectionof n different colors of paint is available, how many different cubes can youmake? Note that any two cubes are onlyto be called "differ

UVALive 3401 彩色立方体

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1402 http://7xjob4.com1.z0.glb.clouddn.com/53f6b2526cc5a59ec7881a8fd6d899bd 题意:将n个原有颜色的立方体涂尽量少次使立方体都相同: 思路:枚举除第一个面外每个立方体的姿态(24种),姿态由旋转方式先处理得

【poj2741】 Colored Cubes

http://poj.org/problem?id=2741 (题目链接) 这也是道神题.. 题意:给出n个骰子,每一面都有一种颜色,问最少更改多少个面的颜色可以使所有骰子通过旋转后完全相同. solution  设6个面的编号为1~6,从中选一个作为顶面,再选一个作为正面,那么其它面都可以确定(因为有对面的面也确定了),因此每个骰子有6*4=24种姿态,每种姿态对应一个全排列P,P[i]表示i所在的位置.所以我们手打这24种排列.  接下来看看如何暴力.我们考虑先枚举每个立方体的姿态(第一个作

uva1352 Colored Cubes LA3401

白书第一章例题8 好麻烦! 正方体每面编号为0-5,那么根据顶点和正面,就能确定形态.一共6*4=24种形态. P[i]表示编号i所在位置.比如P[1]=3,表示第二面转到了第四面. 就可以表示出所有形态. 这时候可以手算或者写个函数找出所有形态. 注意选择函数计算,要放到main外面,方便调. 注意到每个形态都可以由基本姿态左旋上旋得到,而左上旋很接近,就可以模块化了. 然后枚举染色情况.取一个正方体不转(作为参考系,套路了),然后枚举其他三个的情况,然后分别计算6个面. #include <

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y