ACM: 暴力题 poj 2741

来源:互联网 发布:电脑网络连接显示未知 编辑:程序博客网 时间:2024/06/06 00:41
Colored Cubes

Description
There are several colored cubes.All of them are of the same size but they may be coloreddifferently. Each face of these cubes has a single color. Colors ofdistinct faces of a cube may or may not be the same.
Two cubes are said to be identically colored if some suitablerotations of one of the cubes give identical looks to both of thecubes. For example, two cubes shown in Figure 2 are identicallycolored. A set of cubes is said to be identically colored if everypair of them are identically colored.
A cube and its mirror image are not necessarily identicallycolored. For example, two cubes shown in Figure 3 are notidentically colored.
You can make a given set of cubes identically colored by repaintingsome of the faces, whatever colors the faces may have. In Figure 4,repainting four faces makes the three cubes identically colored andrepainting fewer faces will never do.
Your task is to write a program to calculate the minimum number offaces that needs to be repainted for a given set of cubes to becomeidentically colored.

Input

The input is a sequence ofdatasets. A dataset consists of a header and a body appearing inthis order. A header is a line containing one positive integer nand the body following it consists of n lines. You can assume that1 <= n <= 4. Each line in a bodycontains six color names separated by a space. A color nameconsists of a word or words connected with a hyphen (-). A wordconsists of one or more lowercase letters. You can assume that acolor name is at most 24-characters long including hyphens.
A dataset corresponds to a set of colored cubes. The integer ncorresponds to the number of cubes. Each line of the bodycorresponds to a cube and describes the colors of its faces. Colornames in a line is ordered in accordance with the numbering offaces shown in Figure 5. A line
color1 color2 color3 color4 color5color6

corresponds to a cube colored as shown in Figure 6.
The end of the input is indicated by a line containing a singlezero. It is not a dataset nor a part of a dataset.
ACM: <wbr>暴力题 <wbr>poj <wbr>2741
ACM: <wbr>暴力题 <wbr>poj <wbr>2741
ACM: <wbr>暴力题 <wbr>poj <wbr>2741

Output

For each dataset, output a linecontaining the minimum number of faces that need to be repainted tomake the set of cubes identically colored.

Sample Input

3
scarlet green blue yellow magenta cyan
blue pink green magenta cyan lemon
purple red blue yellow cyan green
2
red green blue yellow magenta cyan
cyan green blue yellow magenta red
2
red green gray gray magenta cyan
cyan green gray gray magenta red
2
red green blue yellow magenta cyan
magenta red blue yellow cyan green
3
red green blue yellow magenta cyan
cyan green blue yellow magenta red
magenta red blue yellow cyan green
3
blue green green green green blue
green blue blue green green green
green green green green green sea-green
3
red yellow red yellow red yellow
red red yellow yellow red yellow
red red red red red red
4
violet violet salmon salmon salmon salmon
violet salmon salmon salmon salmon violet
violet violet salmon salmon violet violet
violet violet violet violet salmon salmon
1
red green blue yellow magenta cyan
4
magenta pink red scarlet vermilion wine-red
aquamarine blue cyan indigo sky-blue turquoise-blue
blond cream chrome-yellow lemon olive yellow
chrome-green emerald-green green olive vilidian sky-blue
0

Sample Output

4
2
0
0
2
3
4
4
0
16

题意: n个立方体, 每个面都涂有一种颜色, 现在要你重新为它们涂色, 用最少的涂色次数, 将全部
     立方体变成一样. 2个立方体相同, 存在一种旋转使得每个对应面的颜色相同.

解题思路:
      1. 范围小(最多4个立方体), 只要确定2个面就可以确定整个立方体的摆放了. 6*4=24种摆放
         方法. 打个表吧, 其实也挺快的敲个数组. 例如01,02,03,04最为最初2个面其余可以确定.
         后面以此类推.
      2. 最后枚举每个正方体的24种摆放, 计算最少涂改的次数即可.

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
#define MAX 5

int dire[24][6] = {
    { 0, 1, 2, 3, 4, 5 }, { 0, 2, 4, 1, 3, 5 }, { 0, 4, 3, 2, 1, 5 }, { 0, 3, 1, 4, 2, 5 }, { 3, 1, 0, 5,4, 2 },
    { 3, 0, 4, 1, 5, 2 }, { 3, 4, 5, 0, 1, 2 }, { 3, 5, 1, 4, 0, 2 }, { 5, 1, 3, 2, 4, 0 }, { 5, 3, 4, 1,2, 0 },
    { 5, 4, 2, 3, 1, 0 }, { 5, 2, 1, 4, 3, 0 }, { 2, 1, 5, 0, 4, 3 }, { 2, 5, 4, 1, 0, 3 }, { 2, 4, 0, 5,1, 3 },
    { 2, 0, 1, 4, 5, 3 }, { 4, 0, 2, 3, 5, 1 }, { 4, 2, 5, 0, 3, 1 }, { 4, 5, 3, 2, 0, 1 }, { 4, 3, 0, 5,2, 1 },
    { 1, 0, 3, 2, 5, 4 }, { 1, 3, 5, 0, 2, 4 }, { 1, 5, 2, 3, 0, 4 }, { 1, 2, 0, 5, 3, 4 }
};

int n;
int color[MAX][6], r[MAX];
int d[MAX][6];
int result;
vector name;

inline int max(int a, int b)
{
    return a > b ? a : b;
}

inline int min(int a, int b)
{
    return a < b ? a : b;
}

int find(char *str)
{
    string na = string(str);
    int num = name.size();
    for(int i = 0; i < num; ++i)
        if(name[i] == na) return i;
    name.push_back(na);
    return num;
}

void solve()
{
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < 6; ++j)
            color[i][ dire[ r[i] ][j] ] = d[i][j];
   
    int count = 0;
    for(int j = 0; j < 6; ++j)
    {
        int cnt[MAX*6];
        memset(cnt, 0, sizeof(cnt));
        int maxsize = 0;
        for(int i = 0; i < n; ++i)
            maxsize = max(maxsize, ++cnt[ color[i][j] ]);
        count += (n-maxsize);
    }
    result = min(result, count);
}

void dfs(int cur)
{
    if(cur == n) solve();
    else
    {
        for(int i = 0; i < 24; ++i)
        {
            r[cur] = i;
            dfs(cur+1);
        }
     
}

int main()
{
//    freopen("input.txt", "r", stdin);
    while(scanf("%d", &n) != EOF)
    {
        if(n == 0) break;
        name.clear();
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < 6; ++j)
            {
                char temp[26];
                scanf("%s", temp);
                d[i][j] = find(temp);
            }
        }
       
        result = n*6;
        r[0] = 0;
        dfs(1);
        printf("%d\n", result);
    }
    return 0;
}
0 0
原创粉丝点击