解决Colored Cubes问题
Engineering PuzzleYou 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:
FrontBack 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 Cubeattr_accessor :colordef initialize(color) @color=color @rotation = 0 @times_rotated = 0end#这里我们可以设置一个三维坐标,每个盒子的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 endend#打印出cubedef 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 endend#将4个cube组合为一个cube_box对象class Cube_Boxdef initialize(cube_a, cube_b, cube_c, cube_d) @cube_a = cube_a @cube_b = cube_b @cube_c = cube_c @cube_d = cube_dend #判断是否符合条件def valid? side_valid?(:Front)&&side_valid?(:Back)&&side_valid?(:Left)&&side_valid?(:Right)end def side_valid? side (@cube_a.color != @cube_b.color) && (@cube_a.color != @cube_c.color) &&(@cube_a.color != @cube_d.color)&& (@cube_b.color != @cube_c.color)&& (@cube_b.color!= @cube_d.color)&& (@cube_c.color != @cube_d.color)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 do24.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.rotateendcube_a.rotateendputs "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"puts "Number of found results: " + result_numbers.to_s
页:
[1]