How many squares are guarded by chess queens?

APL, 40 characters

≢⍸a<∨/(⍳8 8)∘.{∨/⊃=/(⊢,+/,-/)¨⍺⍵}⍸a←'Q'=

⍸a←'Q'= transforms the matrix in a nested vector whose elements are the coordinates of the given queens.

(⊢,+/,-/) returns for every cell: the coordinates, their sum, their difference.

{∨/⊃=/(⊢,+/,-/)¨⍺ ⍵} returns 1 if cells ⍺ & ⍵ share one coordinate, the coordinates sum or the coordinates difference; that is gives 1 if a queen can move orizontally, vertically or diagonally from ⍺ to ⍵.

⍵∘.{∨/⊃=/(⊢,+/,-/)¨⍺⍵}⍳8 8 applies the previous function to each queen and each cell in the chessboard.

∨⌿ returns the boolean matrix of cell reachable from some queens ≢⍸a< counts the cell reachable by the queens and not already occupied by queens.


Python - 125 chars

import os
s=os.read(0,99)
print sum(any('*'<s[i]!='Q'in s[i::j].split()[0]for j in(-10,-9,-8,-1,1,8,9,10))for i in range(71))

APL, 37 chars

{≢t~⍨⍸∨/(=/∘|∨0∘∊)¨(⍳8 8)∘.-t←⍸'Q'=⍵}
  • 'Q'=⍵ gives boolean mask of queens
  • gives vector of coordinates (row, column) of queens
  • t← stores the result in var t
  • (⍳8 8)∘.- outer product: gives "distances" (∆row, ∆column) between all grid cells and queen cells
  • 0∘∊ checks if either ∆row or ∆column is 0
  • =/∘| checks if ∆row = +/- ∆column
  • (=/∘|∨0∘∊)¨ checks both conditions for all "distances"
  • ∨/ checks if at least one of those checks return true
  • gives vector of coordinates (row, column) of reachable cells
  • t~⍨ removes original queens' positions
  • count