var solutionCount = 0; //fixme
var board = new Board({'n': n});
// recurse on columns
var repeat = function(board,j){
// means we reached the end of the board.
// but what if reached the end of the board, but don't have a solution? will that never happen?
if(j >= n){
return true;
}
for(var i = 0; i < n; i++) // loop on rows
{
board.togglePiece(i, j);
// we only check for row conflict, since we never place two items(rooks/queens) in the same column,
// when we place a piece, we go to the next column straight away.
if(!board.hasRowConflictAt(i))
{
if(repeat(board, j + 1) === true)
{
solutionCount++;
//return true;
}
}
board.togglePiece(i, j);
}
return false;
}
repeat(board,0);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter