Created
April 16, 2010 16:34
-
-
Save eregon/368643 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implement 'go to higher neighbour' algorithm | |
class Coord < Struct.new(:x, :y) | |
@@coords = [] | |
def to_s | |
"#{x} #{y}" | |
end | |
alias :inspect :to_s | |
def valid? | |
(1..@@w).include?(x) and (1..@@h).include?(y) | |
end | |
def altitude | |
@altitude ||= begin | |
puts "M #{self}" | |
$stdout.flush | |
gets.to_i | |
end | |
end | |
def neighbours | |
neighbours_with_self.tap { |n| n.delete(self) } | |
end | |
def neighbours_with_self | |
(-1..1).map { |dx| | |
(-1..1).map { |dy| | |
Coord.new(x+dx,y+dy) | |
} | |
}.flatten.select(&:valid?) | |
end | |
class << self | |
alias :_new :new | |
def new(x, y) | |
@@coords.find { |c| c.x == x and c.y == y } || _new(x, y).tap { |o| @@coords << o } | |
end | |
def set_size(w, h) | |
@@w, @@h = w, h | |
end | |
end | |
end | |
class Map | |
attr_accessor :altitudes | |
def initialize(n, m) | |
@w, @h = n, m | |
Coord.set_size(@w, @h) | |
end | |
def colline?(c) | |
c.neighbours.all? { |n| n.altitude < c.altitude } | |
end | |
def find_colline | |
last_coord = coord = Coord.new(@w/2, @h/2) | |
loop do | |
coord = coord.neighbours_with_self.max_by(&:altitude) | |
return coord if last_coord == coord | |
last_coord = coord | |
end | |
# Or | |
# coord = Coord.new(@w/2, @h/2) | |
# coord = coord.neighbours.max_by(&:altitude) until colline?(coord) | |
# coord | |
end | |
end | |
def pmain | |
map = Map.new(*gets.split.map(&:to_i)) | |
puts "A #{map.find_colline}" | |
$stdout.flush | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment