問題はこちら
http://qiita.com/items/0ddde0164a745cd09c34
1次元配列で番兵を使った。あまりRubyらしくないかも。
MOVE = {
0 => { 4=> 4,-4=>-4, 1=> 1,-1=>-1},
1 => { 4=> 1,-4=>-1, 1=> 4,-1=>-4},
2 => { 4=>-1,-4=> 1, 1=>-4,-1=> 4},
}
BOARD = '----ABC-DEF-GHI----'.split('')
def solve(q)
pat = ('----%c%c%c-%c%c%c-%c%c%c----'%q.split('')).split('').map{|e| e=='-' ? -1:e.to_i}
pos, mv = [5, 4]
a = ''
until(pat[pos]<0)
a += BOARD[pos]
mv = MOVE[pat[pos]][mv]
pos += mv
end
a
end
DATA.readlines.each do |line|
no,q,a = line.chop.split(/\s+/)
ans = solve(q)
print ans
puts ans == a ? ' o' : ' x'
end
__END__
#0 101221102 BEDGHIFEH
#1 000000000 BEH