Arc Forumnew | comments | leaders | submitlogin
1 point by map 6136 days ago | link | parent

A bit shorter:

  def valid? stack
    q2 = stack.size - 1
    q2.times { |q1|
      return nil if stack[q1] == stack[q2] or (q1-q2).abs == (stack[q1]-stack[q2]).abs
    }
  end

  def queens stack, n
    if n == 8
      p stack
    else
      (1..8).each do |rank|
        stack.push(rank)
        queens(stack, n+1) if valid?(stack)
        stack.pop
      end
    end
  end

  queens [], 0