#C4465. Valid Chessboard Setup
Valid Chessboard Setup
Valid Chessboard Setup
This problem requires you to determine whether a given chessboard setup is valid. The chessboard is represented as an 8x8 grid where each cell contains a single character representing a chess piece or an empty cell (represented by a dot .
). The valid chess pieces are indicated by letters: uppercase letters represent white pieces and lowercase letters represent black pieces.
The board is valid if and only if:
- The board has exactly 8 rows and each row has exactly 8 characters.
- There is exactly one white king (
K
) and one black king (k
). - There is at most one white queen (
Q
) and at most one black queen (q
). - There are at most two white rooks (
R
) and at most two black rooks (r
). - There are at most two white bishops (
B
) and at most two black bishops (b
). - There are at most two white knights (
N
) and at most two black knights (n
). - There are at most eight white pawns (
P
) and at most eight black pawns (p
).
In mathematical terms, if we denote the number of a particular piece c by count(c), then the above conditions can be summarized as:
\[ \begin{aligned} count(K) &= 1, \quad count(k) &= 1, \\ count(Q) &\leq 1, \quad count(q) &\leq 1, \\ count(R) &\leq 2, \quad count(r) &\leq 2, \\ count(B) &\leq 2, \quad count(b) &\leq 2, \\ count(N) &\leq 2, \quad count(n) &\leq 2, \\ count(P) &\leq 8, \quad count(p) &\leq 8. \end{aligned} \]
Your task is to read an 8x8 chessboard configuration from the standard input and output "Valid" if the board meets the criteria, otherwise output "Invalid".
inputFormat
The input consists of exactly 8 lines, each containing 8 characters. Each character is one of the following: 'K', 'Q', 'R', 'B', 'N', 'P' for white pieces, 'k', 'q', 'r', 'b', 'n', 'p' for black pieces, or '.' for an empty square.
outputFormat
Output a single line containing either "Valid" or "Invalid" depending on whether the chessboard configuration is valid according to the given criteria.
## samplernbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR
Valid