叠盒子的问题

  • 题目
http://community.csdn.net/Expert/TopicView3.asp?id=5623795
You have four colored cubes. Each side of each cube is a single color,
and there are four colors: blue (B), red (R), green (G) and yellow (Y).
Describing the six faces as front, back, left, right, top, bottom,
the cube colors are:

Cube Front Back Left Right Top Bottom
1 R B G Y B Y
2 R G G Y B B
3 Y B R G Y R
4 Y G B R R R

The objective is to find ways to stack the four cubes as a vertical column
so that each side of the column is showing all four colors. In a programming
language of your choice, write a program to find all successful permutations.

Please submit the following:
1. Initial estimate of time required for the task
2. Actual time taken for the task
3. Any assumptions you made
4. Source code of solution (preferably with Unit Test code)

  • 解答
#include  < iostream >
#include 
< vector >
#include 
< map >
#include 
< algorithm >
#include 
< iterator >
using   namespace  std;

typedef vector
< int >  Cuba; // 盒子
typedef vector < Cuba >  Cuba_Copy; // 盒子的各种状态,用类似“前左后右上下”的方式表示
typedef vector < Cuba >  Stack; // 堆起来的盒子

void  init_stack(Stack &  _stack);
void  get_all_copies( const  Stack &  _stack, Cuba_Copy &  cc); // 获取盒子的各种状态
void  test_all_possibilities( const  Stack &  _stack); // 测试各种状态的盒子堆起来的是否满足要求
bool  valid_stack( const  Stack &  _stack); // 测试某一种可能的状态是否满足要求,下同
bool  valid_stack( const  Cuba &  c1,  const  Cuba &  c2,  const  Cuba &  c3,  const  Cuba &  c4);
ostream
&   operator << (ostream &  os,  const  Cuba &  cuba); // 打印盒子状态

int  main( void )
{
    Stack _stack;
    init_stack(_stack);
    test_all_possibilities(_stack);
    
return 0;
}



void  init_stack(Stack &  _stack)
{
    Cuba cuba1(
6);
// BRGY 1,2,4,8表示
    cuba1[0= 2;
    cuba1[
1= 1;
    cuba1[
2= 4;
    cuba1[
3= 8;
    cuba1[
4= 1;
    cuba1[
5= 8;
    _stack.push_back(cuba1);
    cuba1[
0= 2;
    cuba1[
1= 4;                                                                           
    cuba1[
2= 4;                                                                           
    cuba1[
3= 8;                                                                           
    cuba1[
4= 1;                                                                           
    cuba1[
5= 1;                                                                           
    _stack.push_back(cuba1);                                                                
    cuba1[
0= 8;                                                                           
    cuba1[
1= 1;                                                                           
    cuba1[
2= 2;                                                                           
    cuba1[
3= 4;                                                                           
    cuba1[
4= 8;                                                                           
    cuba1[
5= 2;                                                                           
    _stack.push_back(cuba1);                                                                
    cuba1[
0= 8;                                                                           
    cuba1[
1= 4;                                                                           
    cuba1[
2= 1;                                                                           
    cuba1[
3= 2;                                                                           
    cuba1[
4= 2;                                                                           
    cuba1[
5= 2;                                                                           
    _stack.push_back(cuba1);                                                                
}
                                                                                           
                                                                                            
void  get_all_copies( const  Cuba &  cuba, Cuba_Copy &  cc)                                        
{                                                                                           
    cout    
<< "get all copies of Cuba " << cuba << endl                                    
            
<< "F,L,B,R,T,B," << endl                                                       
            
<< "------------" << endl;                                                      
    
for (int i = 0; i < 3++i)                                                             
    
{                                                                                       
        Cuba temp(cuba);                                                                    
        rotate(temp.begin(), temp.begin() 
+ 2*i, temp.end());                               
        swap(temp[
1], temp[2]); 
// 比如前后左右,必须搞成前左后右才能用rotate的方法获得4种不同的状态。                                                           
        
for (int j = 0; j < 4++j)                                                         
        
{                                                                                   
            rotate(temp.begin(), temp.begin() 
+ 1, temp.begin() + 4);                       
            cc.push_back(temp);                                                             
            cout 
<< temp << endl;                                                           
            swap(temp[
1], temp[2]);
// 上下颠倒后,左右也颠倒了                                                        
            swap(temp[
4], temp[5]);
// 上下颠倒                                                         
            cc.push_back(temp);                                                             
            cout 
<< temp << endl;                                                           
        }
                                                                                   
    }
                                                                                       
}
                                                                                           
                                                                                            
void  test_all_possibilities( const  Stack &  _stack)                                            
{                                                                                           
    
int count = 0;                                                                          
    vector
<Cuba_Copy> v_cc(4);                                                              
    
for (int i = 0; i < 4++i)                                                             
    
{                                                                                       
        get_all_copies(_stack[i], v_cc[i]);                                                 
    }
                                                                                       
    
for (int i1 = 0; i1 < v_cc[0].size(); ++i1)                                             
    
{                                                                                       
        
for (int i2 = 0; i2 < v_cc[1].size(); ++i2)                                         
        
{                                                                                   
            
for (int i3 = 0; i3 < v_cc[2].size(); ++i3)                                     
            
{                                                                               
                
for (int i4 = 0; i4 < v_cc[3].size(); ++i4)                                 
                
{                                                                           
                    
if (valid_stack(v_cc[0][i1], v_cc[1][i2], v_cc[2][i3], v_cc[3][i4]))    
                    
{                                                                       
                        cout    
<< "validating " << endl                                    
                                
<< v_cc[0][i1] << endl                                      
                                
<< v_cc[1][i2] << endl                                      
                                
<< v_cc[2][i3] << endl                                      
                                
<< v_cc[3][i4] << endl;                                     
                        
++count;                                                            
                    }
                                                                       
                }
                                                                           
            }
                                                                               
        }
                                                                                   
    }
                                                                                       
    cout 
<< "There are " << count << " successful cases." << endl;                          
}
                                                                                           
// 每面和为15才能保证该面有4色。                                                                                           
bool  valid_stack( const  Cuba &  c1,  const  Cuba &  c2,  const  Cuba &  c3,  const  Cuba &  c4)            
{                                                                                           
    
return      (c1[0+ c2[0+ c3[0+ c4[0== 15)                                       
            
&&  (c1[1+ c2[1+ c3[1+ c4[1== 15)                                       
            
&&  (c1[2+ c2[2+ c3[2+ c4[2== 15)                                       
            
&&  (c1[3+ c2[3+ c3[3+ c4[3== 15);                                      
}
                                                                                           
                                                                                            
bool  valid_stack( const  Stack &  _stack)                                                       
{                                                                                           
    
return      (_stack[0][0+ _stack[1][0+ _stack[2][0+ _stack[3][0== 15)           
            
&&  (_stack[0][1+ _stack[1][1+ _stack[2][1+ _stack[3][1== 15)           
            
&&  (_stack[0][2+ _stack[1][2+ _stack[2][2+ _stack[3][2== 15)           
            
&&  (_stack[0][3+ _stack[1][3+ _stack[2][3+ _stack[3][3== 15);          
}
                                                                                           
                                                                                            
ostream
&   operator << (ostream &  os,  const  Cuba &  cuba)                                          
{                                                                                           
    
static map<intchar> map_color;                                                        
    
static bool init = false;                                                               
    
if (!init)                                                                              
    
{                                                                                       
        map_color[
1= 'B';                                                                 
        map_color[
2= 'R';                                                                 
        map_color[
4= 'G';                                                                 
        map_color[
8= 'Y';                                                                 
    }
                                                                                       
    cout 
<< map_color[cuba[0]] << ","                                                       
        
<< map_color[cuba[1]] << ","                                                        
        
<< map_color[cuba[2]] << ","                                                        
        
<< map_color[cuba[3]] << ","                                                        
        
<< map_color[cuba[4]] << ","                                                        
        
<< map_color[cuba[5]] << ",";                                                       
    
return os;                                                                              
}
                                                                                           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值