def place(board, queens, r, c, d, g):
 if (not queens):
  solutions.append(None)
 if (len(queens) > len(board)):
  return
 if (len(queens) > len(r)):
  return
 if (len(queens) > len(c)):
  return
 if (len(queens) > len(d)):
  return
 if (len(queens) > len(g)):
  return
 for (ip, pos) in enumerate(board):
  (ar, ac, ad, ag) = attackmap[pos]
  attacked = frozenset.union(ar, ac, ad, ag)
  nboard = [b for b in board[(ip + 1):] if (b not in attacked)]
  place(nboard, queens[1:], (r - ar), (c - ac), (d - ad), (g - ag))

def place ( board , queens , r , c , d , g ) : if ( not queens ) : solutions . append ( None ) if ( len ( queens ) > len ( board ) ) : return if ( len ( queens ) > len ( r ) ) : return if ( len ( queens ) > len ( c ) ) : return if ( len ( queens ) > len ( d ) ) : return if ( len ( queens ) > len ( g ) ) : return for ( ip , pos ) in enumerate ( board ) : ( ar , ac , ad , ag ) = attackmap [ pos ] attacked = frozenset . union ( ar , ac , ad , ag ) nboard = [ b for b in board [ ( ip + 1 ) : ] if ( b not in attacked ) ] place ( nboard , queens [ 1 : ] , ( r - ar ) , ( c - ac ) , ( d - ad ) , ( g - ag ) )
remaining unattacked places on the board remaining queens to place remaining rows .