Skip to content

Instantly share code, notes, and snippets.

@dungpa
Created February 10, 2012 16:15
Show Gist options
  • Save dungpa/1790555 to your computer and use it in GitHub Desktop.
Save dungpa/1790555 to your computer and use it in GitHub Desktop.
Count all solutions on the chessboard
member x.safe(row) =
not (rows.[row] || fwDiagonals.[row + col]
|| bwDiagonals.[row - col + size - 1])
member x.placeQueen(row) =
rows.[row] <- true
fwDiagonals.[row + col] <- true
bwDiagonals.[row - col + size - 1] <- true
col <- col+1
member x.removeQueen(row) =
col <- col-1
rows.[row] <- false
fwDiagonals.[row + col] <- false
bwDiagonals.[row - col + size - 1] <- false
member x.countSolutions() =
if col = size then 1
else
let mutable answer = 0
for row in 0..size-1 do
if x.safe(row) then
x.placeQueen(row)
answer <- answer + x.countSolutions()
x.removeQueen(row)
answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment