Captures in One Move
Time limit5sMemory limit512 MB
Count ordered capture pairs on an 8x8 board by applying each chess piece's capture rules with blocking for sliders.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
A chess board with pieces on it is given. Count the different ways in which any piece can be captured in one move. Colors do not matter in this problem, so any piece captures any other piece as long as the move is legal.

A square is written as one letter and one digit: A1, A2, ..., A8, B1, ..., H8. The letter runs from A to H and the digit runs from 1 to 8.
The pieces move like this.
- (K) A king moves one square in any of the eight directions.
- (Q) A queen moves any number of squares in any of the eight directions, but cannot jump over another piece.
- (R) A rook moves only horizontally or vertically, and cannot jump over another piece.
- (B) A bishop moves only diagonally, and cannot jump over another piece.
- (N) A knight moves two squares in one direction and then one square at a right angle to it, jumping over anything in between.
- (P) A pawn captures only by moving diagonally toward the higher letter (A to B, B to C, and so on). A pawn on the square with letter and digit attacks exactly the two squares whose letter is the one after and whose digit is or .
A queen, a rook and a bishop stop on the first occupied square along a direction, so each of them captures only the nearest piece in that direction.
Count the ordered pairs (capturing piece, captured piece). With a king on B2, a pawn on A1 and a queen on H8 the answer is 3. The queen on H8 captures the king on B2, the pawn on A1 captures the king on B2, and the king on B2 captures the pawn on A1.
Input
The first line has the number of test cases . Each test case starts with a line holding the number of pieces , followed by lines that each give one piece in the form square-type. For example, A1-K means a king on A1. The type is one of K, Q, R, B, N, P, and no two pieces share a square. Blank lines may appear between test cases.
Limits
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the number of different ways in which a piece can be captured in one move.