We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importControl.Monad(guard)importqualifiedData.IntSetasSetplaceNSuperQueens::Int->IntplaceNSuperQueensn=length(goSet.emptySet.emptySet.empty(100,100)-- y value of last two columns. For first column,-- represents nonsense value that passes knight check.n)wherego____0=return()-- If go had an extra argument that accumulated y-- positions, it could return valid placements easily.-- The tuple of the last two columns would then-- be unnecessary. It currently returns unit as this-- information is not needed.gohorizontalsdiagonalsantidiagonals(y1,y2)left=doy<-[1..n]letx=n-left-- These next two variables are key. To the right, a mapping-- from board position to diagonals (\) and antidiagonals (/).currentDiagonal=y-xcurrentAntidiagonal=y+x-- Perform knight checks first, as they are quicker.guard$abs(y2-y)/=2guard$abs(y1-y)/=1-- Queen operations use sets, and are thus slower.-- We want to discard failing placements quickly.guard$y`Set.notMember`horizontalsguard$currentDiagonal`Set.notMember`diagonalsguard$currentAntidiagonal`Set.notMember`antidiagonalsgo(Set.insertyhorizontals)(Set.insertcurrentDiagonaldiagonals)(Set.insertcurrentAntidiagonalantidiagonals)(y2,y)(left-1)main::IO()main=interact$show.placeNSuperQueens.read
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Super-Queens on a Chessboard
You are viewing a single comment's thread. Return to all comments →
Well-commented code for Haskell beginners: