Saturday, January 16, 2010

アルゴリズム問題

http://www.itmedia.co.jp/enterprise/articles/0908/01/news001.html

class CircleCountry
  attr_reader :x, :y, :r
  def initialize(x, y, r)
    @x, @y, @r = x, y, r
  end

  def inside?(other)
    (self.x - other.x).abs <= other.r && (self.y - other.y).abs <= other.r
  end
end

def least_borders(circles, start, goal)
  count = 0
  circles.each { |c| count += 1 if (start.inside?(c) ^ goal.inside?(c)) }
  count
end

start = CircleCountry.new(0.8, 0.8, 0)
goal = CircleCountry.new(2, 2, 0)
circles = [CircleCountry.new(1, 1, 0.5),
           CircleCountry.new(2, 2, 0.4),
           CircleCountry.new(0.8, 0.8, 0.3)]

puts least_borders(circles, start, goal)

No comments: