#P11878. Chess Check Detection
Chess Check Detection
Chess Check Detection
You are given a chess position described in the FEN (Forsyth–Edwards Notation) format. The board consists of 8 rows and 8 columns. The columns are labeled from a to h from left to right, and the rows are numbered from 1 to 8 from bottom to top.
The FEN string is divided into 8 parts using 7 slashes ('/'). The first part corresponds to the 8th row (top row) of the board, the second part corresponds to the 7th row, and so on. In each part, digits (from 1 to 8) represent that many consecutive empty squares, and letters represent chess pieces. Uppercase letters represent White pieces and lowercase letters represent Black pieces. The pieces are represented as follows:
- K/k: King
- Q/q: Queen
- R/r: Rook
- B/b: Bishop
- N/n: Knight
- P/p: Pawn
A king is considered to be in check if it is within the attack range of any enemy piece. The attack patterns are defined as follows:
- King: Can attack any adjacent square (one step in any direction).
- Queen: Can attack any square in the same row, column, or diagonal, but its path is blocked by any piece encountered along the way.
- Rook: Can attack any square in the same row or column, with blocking rules similar to the queen.
- Bishop: Can attack any square in the diagonals, with blocking rules.
- Knight: Attacks in an "L" shape and can jump over other pieces. The relative moves are \(\{(-2,-1), (-2,1), (-1,-2), (-1,2), (1,-2), (1,2), (2,-1), (2,1)\}\).
- Pawn: Attacks one square diagonally forward. For White pawns (P), the forward direction is upward (i.e. to a lower row index when row 8 is the top). For Black pawns (p), the forward direction is downward.
For pawn attacks, note that a Black pawn at position \((i, j)\) attacks squares \((i+1, j-1)\) and \((i+1, j+1)\), while a White pawn at \((i, j)\) attacks \((i-1, j-1)\) and \((i-1, j+1)\). Here, the board is internally represented so that row 0 corresponds to rank 8 and row 7 corresponds to rank 1.
Your task is to determine whether at least one of the kings is in check in the given FEN-described position. If either the White king or the Black king is in check, output Yes
; otherwise, output No
.
All mathematical formulas have been formatted with \(\LaTeX\). For example, the FEN string is divided as follows: \(s_{1}\,/\,s_{2}\,/\,\cdots\,/\,s_{8}\) and a digit \(i\) in a part means there are \(i\) consecutive empty squares.
inputFormat
The input consists of a single line containing the FEN string representing the chess position.
For example:
r1bq1rk1/pppp1ppp/2n2n2/2b1p3/2B1P3/1P3N2/PBPP1PPP/RN1Q1RK1
outputFormat
Output a single line containing Yes
if at least one king (either white or black) is in check; otherwise, output No
.
sample
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
No