Skip to content

Instantly share code, notes, and snippets.

@rugyoga
Created July 27, 2019 19:02
Show Gist options
  • Save rugyoga/4cee7d33c19e2a4f4749ab916d016ae2 to your computer and use it in GitHub Desktop.
Save rugyoga/4cee7d33c19e2a4f4749ab916d016ae2 to your computer and use it in GitHub Desktop.
Backtracking solution to n queens
def solve(file = 0, &block)
if file == @size
yield self
else
@size.times do |rank|
next if unsafe?(file, rank)
move!(file, rank)
solve(file + 1, &block)
unmove!(file, rank)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment