examples/queens.rb
# board is the first M columns of an NxN board, and is valid so far.
# piece is a proposed piece for the M+1th row of the board.
# returns true if piece is a valid placement, false otherwise
board is the first M columns of an NxN board, and is valid so far.
piece is a proposed piece for the M+1th row of the board.
returns true if piece is a valid placement, false otherwise
board is the first M columns of an NxN board and is valid so far . piece is a proposed piece for the M + 1th row of the board . returns true if piece is a valid placement false otherwise
def safe board, piece
  board.each_with_index do |c, r|
    return false if c == piece  
    rdist = board.size - r
    cdist = (piece - c).abs
    return false if rdist == cdist
  end
  true
end
def safe board , piece board . each_with_index do | c , r | return false if c == piece rdist = board . size - r cdist = ( piece - c ) . abs return false if rdist == cdist end true end
board is the first M columns of an NxN board, and is valid so far.
board is the first M columns of an NxN board and is valid so far .
# same column # they're on the same diagonal if the distance in columns == the distance in rows
board
<not_specific>
piece
<not_specific>