Skip to content

Instantly share code, notes, and snippets.

@rugyoga
Created July 27, 2019 19:13
Show Gist options
  • Save rugyoga/2138bdbd5aff13540315e6054f1f418b to your computer and use it in GitHub Desktop.
Save rugyoga/2138bdbd5aff13540315e6054f1f418b to your computer and use it in GitHub Desktop.
Bitmask based solution to N queens
def unsafe?(file, rank)
get?(@ranks, rank) ||
get?(@northwests, northwest(file, rank)) ||
get?(@northeasts, northeast(file, rank))
end
def move!(file, rank)
super(file, rank)
@ranks = set!(@ranks, rank)
@northwests = set!(@northwests, northwest(file, rank))
@northeasts = set!(@northeasts, northeast(file, rank))
end
def unmove!(file, rank)
super(file, rank)
@ranks = clear!(@ranks, rank)
@northwests = clear!(@northwests, northwest(file, rank))
@northeasts = clear!(@northeasts, northeast(file, rank))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment