examples/nonogram.rb
# define constraint type 3: correspondence between cell variables and rule ones
define constraint type 3: correspondence between cell variables and rule ones
define constraint type 3 : correspondence between cell variables and rule ones
def define_constraint_type_3(solver, l, n, r)
  t = l.map { [] }
  r.each_with_index do |r2, i|
    off = n[0, i].inject(0) {|z, x| z + x + 1 }
    n[i].times do |j|
      r2.each_with_index do |v, k|
        t[off + j + k] |= [v]
      end
    end
  end; t
  l.zip(t) do |v, c|
    c.each {|v2| solver << [v, -v2] }
    solver << ([-v] + c)
  end
end
def define_constraint_type_3 ( solver , l , n , r ) t = l . map { [ ] } r . each_with_index do | r2 , i | off = n [ 0 , i ] . inject ( 0 ) { | z , x | z + x + 1 } n [ i ] . times do | j | r2 . each_with_index do | v , k | t [ off + j + k ] |= [ v ] end end end ; t l . zip ( t ) do | v , c | c . each { | v2 | solver << [ v , - v2 ] } solver << ( [ - v ] + c ) end end
define constraint type 3: correspondence between cell variables and rule ones
define constraint type 3 : correspondence between cell variables and rule ones
# l : a,b,c,d,e,f # n : [1, 2] # r : [[p,q,r], [s,t,u]] # t : [[p], [q], [r,s], [s,t], [t,u], [u]]
solver
l
n
r