1
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.

第8回オフラインリアルタイムどう書く。ruby による実装例。

Last updated at Posted at 2013-03-01

問題 : http://nabetani.sakura.ne.jp/hena/ord8biboma/
解答リンク集 : http://qiita.com/items/709d61dff282cff7a890

最初に書いた実装。

# coding:utf-8

def solve( src )
  w,b = src.split( "/" ).map{ |i| i.to_i(16) }
  r=b;
  field = (0xffff_ffff)&~3
  [
    [1, ("111110"*5+"00").to_i(2) ],
    [-1, ("011111"*5+"00").to_i(2) ],
    [6, -1],
    [-6, -1 ]
  ].each do |shi, mask|
    fire=b
    while fire!=0 do
      r |= (fire & field )
      fire = ( ( fire & mask ) >> shi ) & ~w & field
    end
  end
  "%08x" % r
end

DATA.each do |line|
  num, src, expected = line.split( /\s+/ )
  actual = solve( src )
  puts "%s->%s ( %s ) : %s" % [
    src,
    actual,
    expected,
    actual==expected ? "ok" : "***NG***"
  ]
  raise line unless actual == expected
end

__END__
0 802b1200/01400c20 53c40cfc  
29  0c901d38/72602200 f36da280  

例によってテストデータの大半は省略。

ビット演算で解決するとこんな感じ、という例。
("111110"*5+"00").to_i(2) の辺りは一見不要に見えるかもしれないが、これらがないと、左端に達した爆風が右端から現れたりする。

ruby のバージョンは 1.9.3 と 2.0.0。どちらでも動く。1.8.x は試していない。

1
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
1
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?