0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

問題はこちらのリンクから。
http://qiita.com/items/55641767510c2f9f235f

BOARD = <<-EOD
!!!!!!!!!
!!!!!!!!!
!!!!T!!!!
!!!k!U!!!
!!j!H!V!!
!i!S!I!W!
!!R!B!J!!
!h!G!C!X!
!!Q!A!K!!
!g!F!D!Y!
!!P!E!L!!
!f!O!M!Z!
!!e!N!a!!
!!!d!b!!!
!!!!c!!!!
!!!!!!!!!
!!!!!!!!!
EOD

def make_map
  board = BOARD.split("\n").map{|_|_.split('')}
  map = {}
  board.each_with_index{|line, y|
    line.each_with_index{|val, x|
      if val =~/[A-Za-z]/
        map[val] = []
        map[val] << board[y-2][x]
        map[val] << board[y-1][x+1]
        map[val] << board[y+1][x+1]
        map[val] << board[y+2][x]
        map[val] << board[y+1][x-1]
        map[val] << board[y-1][x-1]
      end
    }
  }
  map
end

def solve(q)
  map = make_map
  moves = q.split('').map(&:to_i)
  moves.inject('A'){|ans,move|
    last_char = ans.split('').reverse.find{|_|_!='!'}
    ans += map[last_char][move]
  }
end

DATA.readlines.each do |line|
  no,q,a = line.split(/\s+/)
  ans = solve(q)
  print no + "\t" + ans.to_s
  puts ans == a ? ' o' : ' x'
end

__END__
0	135004	ACDABHS
1	1	AC
2	33333120	AENc!!b!M
3	0	AB
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?