Skip to content

A blazingly-fast, bit-shifting, backtracking, n-queens algorithm in javascript.

Notifications You must be signed in to change notification settings

reem/n-queens.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Javascript N-Queens

The 30,000 foot overview:

Our inner helper function that does all of the work only takes a few arguments: three integers which represent the spots on the current row that are blocked by previous queens.

The "secret sauce" here is that we can avoid passing around the board or even the locations of the previous queens and instead we use this information to infer the conflicts for the next row.

Once we know the conflicts in our current row we can simply recurse over all of the open spots and profit.

We can then use bit magic to significantly speed up the process over arrays - almost a 50x speedup.

Time for n=12: 60ms.

Please see src/solvers.js for a significantly more detailed explanation of the algorithm including a thorough explanation of all bit magic.

This was part of the curriculum at Hack Reactor and worked on with a pair.

About

A blazingly-fast, bit-shifting, backtracking, n-queens algorithm in javascript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published