叠盒子的问题

  • 题目
http://community.youkuaiyun.com/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;                                                                              
}
                                                                                           
您的问题是关于在 Dreamweaver(DW)中实现盒子盒子”的布局效果。这是一个网页排版中的常见需求,属于前端开发中的CSS定位技术范畴。 该问题不涉及具体题目或代码文件,因此判定为**普通文本型**问题,直接进行回答如下: --- ### 在 Dreamweaver 中如何让盒子盒子? 要实现两个盒子(`div` 元素)之间的重,核心是使用 **CSS 定位(positioning)** 技术。以下是具体方法和示例代码: #### HTML 结构: ```html <div class="box box1">盒子1</div> <div class="box box2">盒子2</div> ``` #### CSS 样式: ```css .box { width: 200px; height: 100px; position: absolute; /* 使用绝对定位 */ } .box1 { background-color: lightblue; top: 50px; left: 50px; z-index: 2; /* 层级更高,显示在上方 */ } .box2 { background-color: lightcoral; top: 80px; left: 80px; z-index: 1; /* 层级较低,被部分遮挡 */ } ``` #### 实现要点: - 设置 `position: absolute;` 让盒子脱离文档流,可通过 `top`、`left` 精确定位; - 使用 `z-index` 控制堆顺序:数值大的盒子会覆盖数值小的盒子; - 父容器建议设置 `position: relative;` 作为参考点,避免全局错位。 #### 在 Dreamweaver 中的操作步骤: 1. 打开设计视图或代码视图; 2. 插入两个 `<div>` 并赋予 class; 3. 在 CSS 规则中为其添加 `position` 和 `z-index`; 4. 预览页面即可看到重效果。 --- ### 知识点 - **position 定位机制**:`absolute` 使元素相对于最近已定位祖先偏实现自由布局。 - **z-index 堆层级**:控制元素在 Z 轴上的显示顺序,数值越大越靠前。 - **盒模型基础结构**:每个 `div` 是一个矩形盒子,由宽高、边距、边框等组成,可被样式控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值