問題はこちらのリンクから
http://qiita.com/Nabetani/items/936e7885f4c607472060
def solve(q)
walls = q.chars.map(&:to_i)
y_sz = walls.max
x_sz = walls.size
pool = walls.map{|sz| sz==0 ? [:H]*y_sz : [:B]*sz + [:W]*(y_sz-sz)}
pool.each_with_index{|v, i|
v.each_with_index{|w, j|
next if w == :B
break if j == 0
left_ok = right_ok = false
if i > 0
(i-1).downto(0){|ii|
case pool[ii][j]
when :H
break
when :B
left_ok = true
break
end
}
end
if i < x_sz - 1
(i+1).upto(x_sz-1){|ii|
case pool[ii][j]
when :H
break
when :B
right_ok = true
break
end
}
end
unless left_ok and right_ok
pool[i] = pool[i][0, j]
break
end
}
}
pool.flatten.count{|v| v == :W}
end
DATA.readlines.each do |line|
no,q,a,d = line.chop.split(/\s+/)
ans = solve(q)
print no + "\t" + ans.to_s
puts ans == a.to_i ? ' o' : ' x'
end
__END__
0 83141310145169154671122 24
1 923111128 45
2 923101128 1