Inquisitor
http://www.unfindable.net/~yabuki/blog/このサイトに刺激されてRubyでSUDOKUに挑戦
いい線まで行くのだけれど行き詰まる
どうやら非決定性計算というのが必要のようだ
つまり仮の答えを選んで進んでダメだったら戻る
SICPに乗ってるアパートの住人問題も
非決定性計算で解けるようだ
Rubyで非決定性計算 - 趣味的にっき
ここを参考にリファクタしてみた
class Range
def each_combination
ary = self.to_a
ary.product(*[ary]*(ary.length-1)).each { |i| yield i }
end
end
solve = []
floor = (1..5)
floor.each_combination do |baker, cooper, fletcher, miller, smith|
next if [baker, cooper, fletcher, miller, smith].uniq!
next if baker.eql? 5
next if cooper.eql? 1
next if fletcher.eql? 1 or fletcher.eql? 5
next if miller < cooper
next if (smith - fletcher).abs.eql? 1
next if (fletcher - cooper).abs.eql? 1
solve << [baker, cooper, fletcher, miller, smith]
end
p solve
いい感じだけどこれって非決定性計算とやらを使ってないぞ?
each_combinationがチョンボか
No comments:
Post a Comment