Created
October 3, 2013 03:49
-
-
Save delikat/6804672 to your computer and use it in GitHub Desktop.
bitwise n-queens solution
This file contains hidden or 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
var bitwiseCountNQueensSolutions = function(n) { | |
var solutions = 0; | |
var allBits = Math.pow(2, n) - 1; | |
var bitwiseQueens = function(majorDiag, col, minorDiag) { | |
var poss = ~(majorDiag | col | minorDiag) & allBits; | |
while (poss) { | |
var queenSpot = -poss & poss; | |
poss = poss ^ queenSpot; | |
bitwiseQueens((majorDiag | queenSpot) >> 1, col | queenSpot, (minorDiag | queenSpot) << 1); | |
} | |
if (col === allBits) { | |
solutions++; | |
} | |
}; | |
bitwiseQueens(0,0,0); | |
return solutions; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment