六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

解决Colored Cubes问题

[复制链接]

升级  98%

57

主题

57

主题

57

主题

秀才

Rank: 2

积分
197
 楼主| 发表于 2013-1-27 05:22:32 | 显示全部楼层 |阅读模式
Engineering Puzzle

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:

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.


#cube对象,其中包含一个Color属性,此属性是一个Struct对象,依次为所有边的颜色class Cube  attr_accessor :color  def initialize(color)    @color=color    @rotation      = 0    @times_rotated = 0  end#这里我们可以设置一个三维坐标,每个盒子的24中摆放方式就是通过这个3为坐标的不同轴的旋转得到。  def rotate    @times_rotated = @times_rotated + 1            #y轴的旋转    tmp_top = @color.Top    @color.Top=@color.Left    @color.Left=@color.Bottom    @color.Bottom=@color.Right    @color.Right=tmp_top#当为4的倍数时意味着已经回到开始的位置,因此需要变换坐标轴    if @times_rotated % 4 == 0      tmp_front = @color.Front#当为2的倍数时意味着又一次要回到刚才已经变换过得位置,因此未免重复,需要再次变换坐标轴      if @rotation % 2 == 0        #x轴的旋转        @color.Front=@color.Bottom        @color.Bottom=@color.Back        @color.Back=@color.Top        @color.Top=tmp_front      else        #z轴的旋转        @color.Front=@color.Right        @color.Right=@color.Back        @color.Back=@color.Left        @color.Left=tmp_front      end      @rotation = @rotation + 1    end  end  #打印出cube  def show(name = "")    puts name        puts  <<-EOF     Front :   #{@color[:Front]}  Back  :   #{@color[:Back]}  Left   :   #{@color[:Left]}  Right :   #{@color[:Right]} Top   :   #{@color[:Top]} Bottom:  #{@color[:Bottom]}    EOF      end  end#将4个cube组合为一个cube_box对象class Cube_Box  def initialize(cube_a, cube_b, cube_c, cube_d)    @cube_a = cube_a    @cube_b = cube_b    @cube_c = cube_c    @cube_d = cube_d  end      #判断是否符合条件  def valid?    side_valid?(:Front)&&side_valid?(:Back)&&side_valid?(:Left)&&side_valid?(:Right)  end    def side_valid? side    (@cube_a.color[side] != @cube_b.color[side]) && (@cube_a.color[side] != @cube_c.color[side]) &&(@cube_a.color[side] != @cube_d.color[side])&& (@cube_b.color[side] != @cube_c.color[side])&& (@cube_b.color[side]!= @cube_d.color[side])&& (@cube_c.color[side] != @cube_d.color[side])  end    #打印出结果  def show_box    p "********************************************************************"    @cube_a.show("cube_a")    @cube_b.show("cube_b")    @cube_c.show("cube_c")    @cube_d.show("cube_d")    p "********************************************************************"  endend#构造每个cube的color对象Color=Struct.new("Color",:Front,:Back,:Left,:Right,:Top,:Bottom)color_a=Color.new("r","b","g","y","b","y")color_b=Color.new("r","g","g","y","b","b")color_c=Color.new("y","b","r","g","y","r")color_d=Color.new("y","g","b","r","r","r")#通过color对象构造cube对象cube_a=Cube.new(color_a)cube_b=Cube.new(color_b)cube_c=Cube.new(color_c)cube_d=Cube.new(color_d)#符合结果的组合的数目result_numbers=0#由于每个cube有p3,3 * 4=24种因此这边要进行24^4次循环,找到合适的后调用show_box打印出来。24.times do  24.times do    24.times do      24.times do        cube_box_temp=Cube_Box.new(cube_a,cube_b,cube_c,cube_d)        if cube_box_temp.valid?          cube_box_temp.show_box          result_numbers = result_numbers + 1        end        cube_d.rotate      end      cube_c.rotate     end    cube_b.rotate  end  cube_a.rotateendputs "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"puts "Number of found results: " + result_numbers.to_s
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表