Under construction

Still being built. Things may change

Captures in One Move

Time limit5sMemory limit512 MB

Summary
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 cc and digit rr attacks exactly the two squares whose letter is the one after cc and whose digit is r−1r-1 or r+1r+1.

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 TT. Each test case starts with a line holding the number of pieces NN, followed by NN 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

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤641 \le N \le 64

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the number of different ways in which a piece can be captured in one move.

Examples2

  1. Example 1

    Input
    2
    2
    A1-K
    A8-Q
    
    3
    B2-K
    A1-P
    H8-Q
    
    Expected output
    Case #1: 1
    Case #2: 3
    
  2. Example 2

    Input
    1
    5
    B2-P
    A1-K
    A3-K
    C1-K
    C3-K
    
    Expected output
    Case #1: 6