uva1352 Colored Cubes LA3401

白书第一章例题8

好麻烦!

正方体每面编号为0-5,那么根据顶点和正面,就能确定形态。一共6*4=24种形态。

P[i]表示编号i所在位置。比如P[1]=3,表示第二面转到了第四面。

就可以表示出所有形态。

这时候可以手算或者写个函数找出所有形态。

注意选择函数计算,要放到main外面,方便调。

注意到每个形态都可以由基本姿态左旋上旋得到,而左上旋很接近,就可以模块化了。

然后枚举染色情况。取一个正方体不转(作为参考系,套路了),然后枚举其他三个的情况,然后分别计算6个面。

#include <iostream>
#include <cstring>
#include <vector>
#include <string>

using namespace std;

const int maxn = 4;
int N,dice[maxn][6],ans;
vector<string> names;
int dice24[24][6];
int r[maxn],color[maxn][6];

int Left[] = {4,0,2,3,5,1};
int Up[] = {2,1,5,0,4,3};
void rot(int T[],int p[])
{
    int q[6];
    memcpy(q,p,sizeof(q));
    for (int i = 0; i < 6; i++)
        p[i] = T[q[i]];
}

void enumerate()
{
    int p0[6] = {0,1,2,3,4,5};
    int times = 0;
    for (int i = 0; i < 6; i++)
    {
        int p[6];
        memcpy(p,p0,sizeof(p0));
        if (i == 0) rot(Up,p);
        if (i == 1)
        {
            rot(Left,p);
            rot(Up,p);
        }
        if (i ==3)
        {
            rot(Up,p);
            rot(Up,p);
        }
        if (i == 4)
        {
            rot(Left,p);
            rot(Left,p);
            rot(Left,p);
            rot(Up,p);
        }
        if (i == 5)
        {
            rot(Left,p);
            rot(Left,p);
            rot(Up,p);
        }
        for (int j = 0; j < 4; j++)
        {
            for (int k = 0; k < 6; k++)
            {
                dice24[times][k] = p[k];
            }
            rot(Left,p);
            times++;
        }
    }
}

int get_ID(string name)
{
    int n = names.size();
    for (int i = 0; i < n; i++)
    {
        if (names[i] == name)
            return i;
    }
    names.push_back(name);
    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[maxn*6];
        memset(cnt,0,sizeof(cnt));
        int maxface = 0 ;
        for (int i = 0; i < N; i++)
        {
            maxface = max(maxface,++cnt[color[i][j]]);
        }
        tot += N - maxface;
    }
    ans = min(ans,tot);
}

void dfs(int d)
{
    if (d == N) check();
    else
    {
        for (int i = 0 ; i < 24; i++)
        {
            r[d] = i;
            dfs(d+1);
        }
    }
}

int main()
{
    enumerate();
    while (cin>>N && N)
    {
        names.clear();
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                string name;
                cin>>name;
                dice[i][j] = get_ID(name);
            }
        }
        ans = N*6;
        r[0] = 0;
        dfs(1);
        cout<<ans<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lqerio/p/9860916.html

时间: 2024-10-29 13:58:28

uva1352 Colored Cubes LA3401的相关文章

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 Colored Cubes (暴力)

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

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>

【poj2741】 Colored Cubes

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

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

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

Codeforces Round #505 (Div 1 + Div 2 Combined) Partial Solution

从这里开始 题目列表 瞎扯 Problem A Doggo Recoloring Problem B Weakened Common Divisor Problem C Plasticine zebra Problem D Recovering BST Problem E Colored Cubes Problem F Disjoint Triangles Problem G Company Acquisitions 瞎扯 打比赛,发现自己特别菜. 居然还苟且水上紫名 这个号不敢玩了.要努力学习