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

コードがごちゃごちゃしてしまった。

offline8.rb
# coding:utf-8

Height = 5
Width = 6

def index x,y
  x + Width*y
end

def bother wall, x1, x2, y1, y2
  b = false
  ([x1,x2].min..[x1,x2].max).each do |x|
    ([y1,y2].min..[y1,y2].max).each do |y|
      b = true unless wall[index x,y].zero?
    end
  end
  b
end

def fire wall, bom, x, y
  f = 0
  Width.times do |xx|
    f = 1 unless bom[index xx,y].zero? || bother(wall,x,xx,y,y)
  end
  Height.times do |yy|
    f = 1 unless bom[index x,yy].zero? || bother(wall,x,x,y,yy)
  end
  f
end

def calc s
  wall, bom = s.split("/").map{|m| ("%032b"%m.to_i(16)).chars.map{|m| m.to_i}}
  r = Array.new(32, 0)
  Width.times do |x|
    Height.times do |y|
      r[index x,y] = fire wall, bom, x, y
    end
  end
  sprintf("%08x", r.map{|m| m.to_s}.join.to_i(2))
end

DATA.each do |a|
  n, s, d = a.split
  r = calc s
  t = r==d ? "ok": "ng  #{r}!=#{d}"
  puts "#{n}:  #{t}"
end

__END__
0 802b1200/01400c20 53c40cfc
1 28301068/84080504 d64fef94
2 100a4010/80010004 e241850c
3 81020400/000000fc 0e3cfbfc
4 80225020/7e082080 7fdd24d0
5 01201200/40102008 fe1861fc
6 00201000/01000200 43c48f08
7 00891220/81020408 ff060c1c
8 410033c0/0c300000 3cf0c000
9 00000000/01400a00 7bf7bf78
10  00000000/20000a00 fca2bf28
11  00000000/00000000 00000000
12  00cafe00/00000000 00000000
13  aaabaaaa/50000000 51441040
14  a95a95a8/56a56a54 56a56a54
15  104fc820/80201010 ea30345c
16  4a940214/05000008 05000008
17  00908000/05000200 ff043f48
18  00c48c00/fe1861fc ff3873fc
19  00000004/81020400 fffffff0
20  111028b0/40021100 e08fd744
21  6808490c/01959000 17f7b650
22  30821004/81014040 c75de5f8
23  0004c810/10003100 fe4937c4
24  12022020/88200000 edf08208
25  2aa92098/01160000 45165964
26  00242940/10010004 fc43c43c
27  483c2120/11004c00 33c3de10
28  10140140/44004a04 eda3fe3c
29  0c901d38/72602200 f36da280
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?