SuperCon2006予選課題 part7 / この世で最高の回答!!!
Powered by dKingyo TurboGears | アルゴリズム | シューティング | VB2005 | ゲームプログラミング
ある意味最高の回答です。
Rubyのソースコードもどうぞ・・・
#http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/42453より引用および改造 class MultiArray def initialize *arg @nsize = arg length = 1 arg.each do |i| length * i end @length = length @data = Array.new(@length) end attr_reader :length,:nsize def [] *arg pos = 0 @nsize.zip(arg) do |length,i| pos = pos * length + i end @data[pos] end def []= *arg pos = 0 @nsize.zip(arg[0,@nsize.length]) do |length,i| pos = pos * length + i end @data[pos] = arg[-1] end def get_x return @nsize[0] end def get_y return @nsize[1] end def get_z return @nsize[2] end end NONE = 0 MARK =1 PAINT = 2 REGEX_NUM = /[-+]?\d+/ class Point def initialize(mark) @line_num = 0 @mark = mark @next_point end attr_accessor :line_num,:mark,:next_point def inc_line_num() if @line_num >= 2 then raise "Point line_num overflow." end @line_num = @line_num + 1 end end def inc(counter) return counter + 1 end def create_table $stdin.seek(0, IO::SEEK_SET) #格子の一辺は100以下だがこの際無制限にする。入力も不規則でも良い事にする。 print "lattice num : ",gets,"" #最初の格子数 max_xy = [0,0] while i = gets#格子の最大値を求める c = 0 i.scan(REGEX_NUM){|s| v = c%2 if nil==s then raise "input regex nil" end t = s.to_i() if max_xy[v] < t then max_xy[v] = t end c = inc(c) } end print "max point : [",max_xy[0],",",max_xy[1],"]\n" # +1するのは0から始まるから。私はここをミスってしまった。 t = MultiArray.new(max_xy[0]+1,max_xy[1]+1) x = t.nsize[0] y = t.nsize[1] for i in 0..x-1 do for j in 0..y-1 do t[i,j] = Point.new(NONE) end end return t end def table_seek_set $stdin.seek(0, IO::SEEK_SET) $stdin.gets end def create_mark_table(table) table_seek_set() xy=[0,0] c = 0 v = 0 while i = gets#格子の頂点を格納する c = 0 i.scan(REGEX_NUM){|s| v = c%2 xy[v] = s.to_i if 0 != c && 1==v then x = xy[0] y = xy[1] if(x==-1 || y==-1) then break end print "[",x,",",y,"] " table[x,y] = Point.new(MARK) end c = inc(c) } end print "\n" return table end def print_table(table) x = table.get_x() y = table.get_y() for j in 0..y-1 do for i in 0..x-1 do t = table[i,j] case(t.mark) when NONE print "・" when MARK print "*" when PAINT print "-" end end print "\n" end end def output_xy(foo,x,y) print x," ",y,"\n" foo.print x," ",y,"\n" end def super_idiotical_method_odd(tbl) if tbl[1,1].mark != NONE then print "解けません。\n" return end foo = File.open("output.txt",'w') x = tbl.get_x() y = tbl.get_y() for j in 0..y-1 do output_xy(foo,0,j) end ctr = 0 j = y-1 while j >= 2 if ctr % 2 == 0 for i in 1..x-1 do output_xy(foo,i,j) end else i=x-1 while i >= 1 output_xy(foo,i,j) i = i-1 end end ctr = ctr + 1 j=j-1 end output_xy(foo,i,j) ctr = 0 jc = 0 while i >= 2 if ctr % 2 == 1 i=i-1 else j = (j==0) ? 1 : 0 end ctr = ctr + 1 output_xy(foo,i,j) end output_xy(foo,0,0) output_xy(foo,-1,-1) end def super_idiotical_method_even(tbl) foo = File.open("output.txt",'w') x = tbl.get_x() y = tbl.get_y() for j in 0..y-1 do output_xy(foo,0,j) end ctr = 0 j = y-1 while j >= 0 if ctr % 2 == 0 for i in 1..x-1 do output_xy(foo,i,j) end else i=x-1 while i >= 1 output_xy(foo,i,j) i = i-1 end end ctr = ctr + 1 j=j-1 end output_xy(foo,0,0) end tbl = create_mark_table(create_table()) print_table(tbl) if tbl.get_x() % 2==0 && tbl.get_y() % 2==0 then super_idiotical_method_even(tbl) else super_idiotical_method_odd(tbl) end