Skip to content

Instantly share code, notes, and snippets.

@rugyoga
Created July 27, 2019 19:15
Show Gist options
  • Save rugyoga/00a551c182ff84a6705b8bd73887be3b to your computer and use it in GitHub Desktop.
Save rugyoga/00a551c182ff84a6705b8bd73887be3b to your computer and use it in GitHub Desktop.
Crystal implementation of the bitmask solution to n queens
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