#P6194. Safe King Placement on a Chessboard
Safe King Placement on a Chessboard
Safe King Placement on a Chessboard
You are given an 8×8 chessboard on which some black pieces have been placed: black rooks and black bishops. Due to a mysterious force, these are the only pieces left. You want to determine all cells on which a white king can be safely placed. A cell is considered safe if and only if:
- The cell is empty (i.e. it does not already contain a black piece).
- No black piece can move to that cell in one move.
The movement rules are as follows (with all moves written in \(\LaTeX\)
format):
- A rook moves in the four cardinal directions: up, down, left, and right. Formally, from cell \((r,c)\), a rook can move to any cell \((r, c+k)\) or \((r+k, c)\) for any nonzero integer \(k\), provided that there is no piece between the starting cell and the target cell. If a piece is encountered along the way, the rook can attack that cell but cannot move beyond it.
- A bishop moves diagonally. That is, from \((r,c)\), it can move to any \((r+k, c+k)\), \((r+k, c-k)\), \((r-k, c+k)\), or \((r-k, c-k)\), for nonzero integer \(k\), with the same rule about blocking pieces.
You need to list all safe cells (sorted in increasing order of row and then column, where rows and columns are numbered from 1 to 8). The output should start with the number of safe cells, followed by each safe cell in the format "row col" on a separate line.
Note: Even if a black piece could normally be captured by moving into its cell, you are not allowed to place the king on any cell that is already occupied.
If you have trouble understanding the problem, please refer to the sample test cases.
inputFormat
The input consists of exactly 8 lines, each containing 8 characters. Each character is either:
'.'
representing an empty cell,'R'
representing a black rook, or'B'
representing a black bishop.
This represents the board configuration, where the first line corresponds to row 1 and so on.
outputFormat
First, print an integer indicating the total number of safe cells for placing the white king. Then, if there is at least one safe cell, print each safe cell’s coordinates on a new line in the format "row col" (both row and column are 1-indexed and sorted first by row then by column).
sample
........
........
........
...R....
........
........
........
........
49
1 1
1 2
1 3
1 5
1 6
1 7
1 8
2 1
2 2
2 3
2 5
2 6
2 7
2 8
3 1
3 2
3 3
3 5
3 6
3 7
3 8
5 1
5 2
5 3
5 5
5 6
5 7
5 8
6 1
6 2
6 3
6 5
6 6
6 7
6 8
7 1
7 2
7 3
7 5
7 6
7 7
7 8
8 1
8 2
8 3
8 5
8 6
8 7
8 8
</p>