Under construction

Still being built. Things may change

One move captures (small)

Time limit5sMemory limit512 MB

Summary
Count all ordered pairs of chess pieces where one can capture the other in a single legal move with blocking.
Level

Easy3 of 10

Topics
Simulation, Brute force
Solved
No attempts yet

Problem

You are given a chess board with pieces on it. Count the total number of different ways in which some piece can be captured in one move. Colors do not matter in this problem, so any piece may capture any other piece.

A square is written as A1, A2, ..., A8, B1, ..., H8, where the letter A to H gives the column and the digit 1 to 8 gives the row.

The pieces move like this.

  • (K) A king moves one square in any of the 8 directions.
  • (Q) A queen moves any number of squares in any of the 8 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 to a square that is two squares horizontally and one square vertically away, or one square horizontally and two squares vertically away. Pieces in between do not block it.
  • (P) A pawn captures only by moving diagonally toward the higher letter (A to B, B to C, and so on). It therefore captures a piece that stands one letter higher and one row above or one row below.

What you count is ordered pairs (capturing piece, captured piece). If two pieces can capture each other, that counts as two ways.

For example, 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. The queen on H8 cannot capture the pawn on A1, because the king on B2 blocks the diagonal.

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. Each of those lines holds the square of a piece, then -, then the piece type.

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤101 \le N \le 10
  • Each piece type is one of K, Q, R, B, N, P.
  • At most one piece stands on a square.

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 total number of different ways in which some piece can be captured in one move.

Examples1

  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