Poj 2741Colored Cubes(贪心&枚举)

本题属于贪心+枚举;

最多有4个立方体,所以可以枚举出所有与的立方体摆放的排列组合。

先打表,每个立方体通过两种基本旋转,能达到所有旋转可能,向左转、向右转,只要记录一次基本旋转后,上、下、左、右、前、后分别是哪些面即可。

然后就是贪心,考虑一种排列组合,每一个面,选取相同的颜色最多的作为最终要涂成的颜色,则最终6个面要涂的面的和就是,当前组合需要涂的最小值。取所有排列组合的最小值就是最终答案!

 

在用到string时,首先include<string>,不同oj的编译器不同,有些不需要include,有些需要include否则进行串比较会报错!



#include <cstdlib>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <memory.h>
#include <algorithm>
#include <string>

using namespace std;

const int INF = 24;

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

void rot(int* d,int* cubic)//打表用的函数
{
    int tmp[6];
    //memcpy(tmp,cubic,6);
    for(int j = 0; j < 6; j++)tmp[j] = cubic[j];
    for(int i = 0; i < 6; i++)cubic[i] = tmp[d[i]];
}

void get_roats()//打表用的函数
{
    int cubic[] = {0,1,2,3,4,5};
    int aftrot[6];
    int left[] = {1,5,2,3,0,4};
    int up[] = {3,1,0,5,4,2};
    printf("{\n");
    for(int i = 0; i < 6; i++)
    {
        //memcpy(aftrot,cubic,6);
        for(int j = 0; j < 6; j++)aftrot[j] = cubic[j];
        switch(i)
        {
            case 0: rot(up,aftrot);break;
            case 1: rot(left,aftrot);rot(up,aftrot);break;
            case 3: rot(up,aftrot);rot(up,aftrot);break;
            case 4: rot(left,aftrot);rot(left,aftrot);rot(left,aftrot);rot(up,aftrot);break;
            case 5: rot(left,aftrot);rot(left,aftrot);rot(up,aftrot);break;
        }
        
        for(int i = 0; i < 4; i++)
        {
            printf("{%d,%d,%d,%d,%d,%d},\n",aftrot[0],aftrot[1],aftrot[2],aftrot[3],aftrot[4],aftrot[5]);
            rot(left,aftrot);
        }
        
        
    }
    printf("}\n");
    
    
}

vector<string> colors;
int mmin;
int n;
int tmp[4][6];
int getId(const char* color)
{
    int i = 0,s = colors.size();
    string c(color);
     for(; i < s; i++)
         if(c == colors[i])return i;
     colors.push_back(c);
     return i;
}

void caculate()
{
    int sum = 0;
    int color_count[30];
    
    for(int i = 0; i < 6; i++)
    {
        memset(color_count,0,sizeof(color_count));
        int m = 0,color_size = colors.size();
        for(int c_num = 0; c_num  < n; c_num++)
        {
            color_count[tmp[c_num][i]]++;
        }
        
        for(int p = 0; p < color_size; p++)
        m = max(m,color_count[p]);
        sum += n - m;
    }
    
    mmin = min(mmin,sum);
    
}

void get_min(int face_color[][6],int level)
{
     //printf("%d\n",level);
    if(level == n)
    {
        caculate();
        return;
    }
    for(int i = 0; i < 24; i++)
    {
        for(int face = 0; face < 6; face++)
        tmp[level][face] = face_color[level][all_rots[i][face]];
        get_min(face_color,level+1);
    }
} 

int main(int argc, char *argv[])
{   //get_roats();

    
    int face_color[4][6];
    char str[30];
    while(scanf("%d",&n) != -1 && n)
    {
        colors.clear(); 
        for(int i = 0; i < n; i++)
        {
            for(int face = 0; face < 6; face++)
            {
                scanf("%s",str);   
                face_color[i][face] = getId(str);
            }
            
        }
        
        mmin = INF;
        for(int i = 0; i < 6; i++)tmp[0][i] = face_color[0][i];
        get_min(face_color,1);
        printf("%d\n",mmin);
        //system("PAUSE");
        
    } 
    //system("PAUSE");
    return EXIT_SUCCESS;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值