Skip to content

Instantly share code, notes, and snippets.

@diogoan
Last active December 22, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diogoan/6491075 to your computer and use it in GitHub Desktop.
Save diogoan/6491075 to your computer and use it in GitHub Desktop.
isTileBusy::[(Int, Int)] -> (Int, Int) -> Bool
isTileBusy [] x = False
isTileBusy (a:as) x = (isLos a x) || (isTileBusy as x)
isLos::(Int, Int) -> (Int, Int) -> Bool
isLos (a, b) (x, y) = or [a == x, b == y, abs (a - x) == abs (b - y)]
-- parametros
-- 1- rainhas ja posicionadas
-- 2- linha aonde se quer encontrar a proxima casa vazia
-- 3- coluna da iteracao atual
-- 4- largura maxima do tabuleiro
-- resultado: coluna aonde deve-se colocar a rainha, baseado na linha informada
queenLinePlacer::[(Int, Int)] -> Int -> Int -> Int -> Int
queenLinePlacer [] line row size = row
queenLinePlacer board line row size =
if (row > size)
then (-1)
else if (isTileBusy board (line, row))
then (queenLinePlacer board line (row + 1) size)
else row
-- parametros
-- 1- configuração atual do tabuleiro
-- 2- tamanho do tabuleiro
-- 3- linha aonde sera colocada uma rainha atualmente
-- 4- a partir de qual coluna deve-se começar a iteração
-- resultado: tabuleiro com rainhas colocadas
queenBoardPlacer::[(Int, Int)] -> Int -> Int -> Int -> [(Int, Int)]
queenBoardPlacer board size line startRow =
if (length board) >= size
then do print board
return board
else if row == (-1)
then do print board
queenBoardPlacer (tail board) size (line - 1) (prevRow + 1)
else do print board
queenBoardPlacer ((line, row):board) size (line + 1) 0
where
row = queenLinePlacer board line startRow size
prevRow = snd $ head board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment