`
simohayha
  • 浏览: 1385653 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

解决Colored Cubes问题

    博客分类:
  • ruby
阅读更多

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 "********************************************************************"
  end
end

#构造每个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.rotate
end

puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
puts "Number of found results: " + result_numbers.to_s

分享到:
评论
1 楼 wushaoen 2008-10-29  
这个rb文件编译有问题.

相关推荐

Global site tag (gtag.js) - Google Analytics