29
Jun
Posted by liubin as Ruby
| 1,230 Views
题目:有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
用Ruby来解,不知道结果对不对。
- #define the Ant class
- class Ant
- attr_accessor :direction,:pos,:id
-
- def veer # turn back when run into other ants
- @direction = 1 - @direction
- end
-
- def arrived?
- return true if @pos == 0 or @pos == 27
- return false
- end
-
- def step
- if @direction == 1 then
- @pos = @pos + 1
- else
- @pos = @pos - 1
- end
- end
-
- def run_into(a) # if run into other ant,return true if so
- return false if a.direction == self.direction
- return true if a.pos == self.pos
- return false
- end
-
- def to_s
- "id:#{@id}.direction:#{@direction}.pos:#{@pos}"
- end
-
- end # end of Ant
-
- class Hash
- def keys
- a = Array.new
- self.each_key do |k|
- a.push k
- end
- a
- end
- end
-
- #initialize the ant[5] array
- def create_ant(d1,d2,d3,d4,d5)
- a1,a2,a3,a4,a5=nil,nil,nil,nil,nil
- (1..5).each do |i|
- eval " a#{i} = Ant.new;a#{i}.id = #{i};a#{i}.direction = d#{i};"
- end
-
- a1.pos = 3
- a2.pos = 7
- a3.pos = 11
- a4.pos = 17
- a5.pos = 23
- return [a1,a2,a3,a4,a5]
-
- end
-
- #if all ant has arrived ,return true
- def all_arrived a
- a.each do |a1|
- return false unless a1.arrived?
- end
- return true
- end
-
- # get one plan's cost
- # p is for puts the ant's progress if it is set to true
- def travel(d1,d2,d3,d4,d5,p=false)
- c = 0 #total cost
- x = 0 #loop variables
- onway = create_ant(d1,d2,d3,d4,d5)
- while true
- break if all_arrived(onway)
- x = 0
- while (x < onway.length-1) do
- (x=x+1 ; next) if onway[x].arrived?
- if onway[x].run_into onway[x+1] then
- onway[x].veer
- onway[x+1].veer
- x = x + 2
- else
- x = x + 1
- end
- end
- onway.each do |ai|
- ai.step unless ai.arrived?
- end
- c = c + 1
- print " step#{format("%2d",c)} : #{format("%-2d",onway[0].pos)},#{format("%-2d",onway[1].pos)}," if p
- print "#{format("%-2d",onway[2].pos)},#{format("%-2d",onway[3].pos)},#{format("%-2d",onway[4].pos)} \n" if p
-
- end
- c
- end
-
- #path = {path length=><Array of direction info>}
- path = Hash.new
-
- [1,0].each do |d1|
- [1,0].each do |d2|
- [1,0].each do |d3|
- [1,0].each do |d4|
- [1,0].each do |d5|
- cost = travel(d1,d2,d3,d4,d5)
- (path[cost]||=Array.new).push binding
- end
- end
- end
- end
- end
-
- puts "short path length : #{path.keys.sort.first},path count :#{path[path.keys.sort.first].length}"
-
- path[path.keys.sort.first].each_with_index do |b,i|
- puts " short path #{format("%2d",i+1)}:"
- eval " print ' direction : ',d1,' ',d2,' ',d3,' ',d4,' ',d5,\"\n\"",b
- #eval "travel(d1,d2,d3,d4,d5,true)", b
- end
-
- puts "long path length : #{path.keys.sort.last},path count :#{path[path.keys.sort.last].length}"
-
- path[path.keys.sort.last].each_with_index do |b,i|
- puts " long path #{format("%2d",i+1)}:"
- eval " print ' direction : ',d1,' ',d2,' ',d3,' ',d4,' ',d5,\"\n\"",b
- #eval "travel(d1,d2,d3,d4,d5,true)", b
- end
Related posts for the current post:
RSS feed for comments on this post · TrackBack URI
Leave a reply