オフラインでリアルタイムに「どう書く」をやるイベント。
http://atnd.org/events/32191
の参考問題
http://qiita.com/items/9c514267214d3917edf2
の実装例。
まずは ruby。これは、出題前に書いた最初の実装ほぼそのまま。
# !ruby
# coding:utf-8
def move( state, dir )
y,x=( state[-1].ord-'a'.ord ).divmod(5)
x+=dir[0]
y+=dir[1]
return nil unless (0...5)===x && (0...5)===y
('a'.ord+y*5+x).chr
end
def can_move( nextpos, state, de )
! state.include?( nextpos ) && ! de.include?( state[-1]+nextpos )
end
def count_path( state, dir, de )
nextpos = move( state[-1], dir)
return 0 unless nextpos && can_move( nextpos, state, de )
return 1 if nextpos=='y'
s=state+nextpos
[ [1,0], [-1,0], [0, 1], [0, -1] ].map{ |dir|
count_path( s, dir, de )
}.inject( &:+ )
end
def solve(q)
cur = 'a'
de=q+' '+q.reverse
count_path( cur, [1,0], de) + count_path( cur, [0,1], de)
end
DATA.each{ |line|
if /^"(?<q>.*)"\s*\-\>\s*(?<ex>\d+)/=~line
actual = solve(q)
puts( %Q!"%s" -> %d! % [q,actual] )
puts "**EXPECTED : #{ex} **" unless actual==ex.to_i
else
puts( "skipped : "+ line )
end
}
__END__
"" -> 8512 #そのまま。これが最大値。
"ch hi mn kp mr rs sx" -> 50 #6*3+4*8
本当はもっとたくさんテストデータがある。
行き止まりのリストを単一の文字列にして持っているあたりがやっつけっぽい。