Created
July 27, 2019 19:15
-
-
Save rugyoga/00a551c182ff84a6705b8bd73887be3b to your computer and use it in GitHub Desktop.
Crystal implementation of the bitmask 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
class NQueensBitarrays < NQueens | |
def initialize(size : Int32) | |
super(size) | |
@ranks = Array(Bool).new(n){ false } | |
@northwests = Array(Bool).new(2*size){ false } | |
@northeasts = Array(Bool).new(2*size){ false } | |
end | |
def unsafe?(file, rank) | |
@ranks[rank] || | |
@northwests[northwest(file, rank)] || | |
@northeasts[northeast(file, rank)] | |
end | |
def move!(file, rank) | |
super(file, rank) | |
@ranks[rank] = true | |
@northwests[northwest(file, rank)] = true | |
@northeasts[northeast(file, rank)] = true | |
end | |
def unmove!(file, rank) | |
super(file, rank) | |
@ranks[rank] = false | |
@northwests[northwest(file, rank)] = false | |
@northeasts[northeast(file, rank)] = false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment