端の二行を与えて解を数える。 def nq2(size, r1, r2) board = concat([r1], r2) return 0 unless board nq(size, board) end def ipsj(size) found = 0 size.times do |r1| size.times do |r2| found += nq2(size, r1, r2) end end found end
IPSJの課題に合うように書き直してみる。課題の都合により次の行に進めるかどうかを試すメソッドをわけた。 class NQueen def concat(board, row) board.each_with_index do |v, col| return nil if v == row return nil if (v - row).abs == board.size - c…