Problem Statement
Several queens have been placed on a chessboard. A queen can move any number of unoccupied squares in a straight line vertically, horizontally or diagonally. Any square which has no queen on it and cannot be attacked by any queen is considered safe.
You are given the arrangement of the queens on the chessboard as a String[] board. Each element of board represents one row of squares, and each character represents a square. 'Q' represents a square with a queen in it, and '.' represents an empty square. Return the number of safe squares on the board.
Definition
- Class:
- SafeChessboard
- Method:
- safeSquares
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int safeSquares(String[] board)
- (be sure your method is public)
Constraints
- board will contain between 1 and 50 elements, inclusive.
- Each element of board will contain between 1 and 50 characters, inclusive.
- Each element of board will be the same length.
- Each character of each element of board will be either 'Q' or '.'.
Examples
{"Q..", "...", "..."}Returns: 2
The cells that are under attack look like this: QAA AA. A.A That leaves two cells which are "safe".{"...", "Q..", "..."}Returns: 2
Still 2 safe cells, but now it looks like this: AA. QAA AA.{"Q...", "....", "....", "...Q"}Returns: 2
{"...", "...", "..."}Returns: 9
With no queens attacking, all the cells are safe.{"..Q.....", ".....Q..", "........", "Q......."}Returns: 2