Sha256: d84b17f2e2aca81a3d8d93d28d85f2d3a7c5fb7b90fb66875fa191dcd5ae430f

Contents?: true

Size: 1.55 KB

Versions: 18

Compression:

Stored size: 1.55 KB

Contents

require File.dirname(__FILE__) + '/example_helper'

# Solves the n-queens problem: http://en.wikipedia.org/wiki/Nqueens . No attempt
# to break the involved symmetries is made.
class NQueens < Gecode::Model
  def initialize(n)
    @size = n
  
    # The row number that the queen in the i:th column has. By using this as
    # our variables we already make sure that no two queens are in the same
    # column.
    @queen_rows = int_var_array(n, 0...n)
    
    # Set up the constraints
    # Queens must not be in the same diagonal (negative slope).
    @queen_rows.with_offsets((0...n).to_a).must_be.distinct
    # Queens must not be in the same diagonal (positive slope).
    @queen_rows.with_offsets((0...n).to_a.reverse).must_be.distinct
    # Queens must not be in the same row.
    @queen_rows.must_be.distinct
    
    # Branching, we use first-fail heuristic.
    branch_on @queen_rows, :variable => :smallest_size, :value => :min
  end
  
  # Displays the assignment as a chessboard with queens denoted by 'x'.
  def to_s
    rows = @queen_rows.values
  
    separator = '+' << '-' * (3 * @size + (@size - 1)) << "+\n"
    res = (0...@size).inject(separator) do |s, i|
      (0...@size).inject(s + '|') do |s, j|
        s << " #{rows[j] == i ? 'x' : ' '} |"
      end << "\n" << separator
    end
  end
end

# Print the first solution. Note that there are 92 solutions, but only 12 
# are rotationally distinct. For any serious use one should place additional
# constraints to eliminate those symmetries.
NQueens.new( (ARGV[0] || 8).to_i ).solution{ |sol| puts sol.to_s }

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
gecoder-with-gecode-0.8.3-mswin32 example/queens.rb
gecoder-with-gecode-0.8.2-mswin32 example/queens.rb
gecoder-with-gecode-0.8.1-mswin32 example/queens.rb
gecoder-with-gecode-0.8.0-mswin32 example/queens.rb
gecoder-with-gecode-0.7.1-mswin32 example/queens.rb
gecoder-0.8.3 example/queens.rb
gecoder-0.6.0 example/queens.rb
gecoder-0.7.0 example/queens.rb
gecoder-0.7.1 example/queens.rb
gecoder-0.8.0 example/queens.rb
gecoder-0.8.2 example/queens.rb
gecoder-0.6.1 example/queens.rb
gecoder-0.8.1 example/queens.rb
gecoder-with-gecode-0.7.1 example/queens.rb
gecoder-with-gecode-0.8.0 example/queens.rb
gecoder-with-gecode-0.8.1 example/queens.rb
gecoder-with-gecode-0.8.2 example/queens.rb
gecoder-with-gecode-0.8.3 example/queens.rb