type Point = (Int,Int)
type Board = Point -> Bool
data Color = Red | Black

neighbors                :: Point -> [Point]
neighbors (x,y)          = [(x-1,y),(x+1,y),(x,y-1),(x,y+1)]

numOpenNeighbors         :: Point -> Board -> Int
numOpenNeighbors p b     = sum (map (\ p -> if (b p) then 1 else 0) (neighbors p))

colorForPoint            :: Point -> Board -> Color 
colorForPoint p b        = if even (numOpenNeighbors p b) then Red else Black

place                    :: Point -> Board -> Board
place p b                = (\ p' -> if p' == p then False else b p')

solve                    :: [Point] -> Board -> [(Color,Point)]
solve  []    b           = []
solve (p:ps) b           = ((colorForPoint p b),p):(solve ps (place p b))

initialBoard             :: Int -> Int -> Board
initialBoard h w (x,y)   = x > 0 && y > 0 && x <= h && y <= w

toStr  []                = ""
toStr ((Red,  (x,y)):ms) = "R "++(show x)++","++(show y)++"\n"++(toStr ms)
toStr ((Black,(x,y)):ms) = "B "++(show x)++","++(show y)++"\n"++(toStr ms)



main = putStr (toStr (solve [(x,y) | x <- [1..5], y <- [1..5]] (initialBoard 5 5)))
