Created
July 27, 2019 19:02
-
-
Save rugyoga/4cee7d33c19e2a4f4749ab916d016ae2 to your computer and use it in GitHub Desktop.
Backtracking solution to n queens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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