LoginSignup
1
1

More than 5 years have passed since last update.

オフラインリアルタイムどう書く第5回の参考問題をRubyで解く

Posted at

問題はこちら
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
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1