This page is still under construction.

Parts of this page are still being built. What you see may change.

Cannon's Move

Time limit1sMemory limit128 MB

Summary
Move the cannon by jumping over exactly one piece per move to capture the king in the fewest moves.
Level

Medium6 of 10

Topics
BFS, Graph, Simulation
Solved
No attempts yet

Problem

Janggi is a Korean board game similar to chess. Two players face each other on a board of size 9 by 10. Each side has 16 pieces of 7 types: 1 king, 2 chariots, 2 cannons, 2 horses, 2 elephants, 2 guards, and 5 pawns. The cannon is special: it both moves and captures by jumping over exactly one other piece along a straight line.

In this problem we play a one-dimensional janggi on a single row of cells. There are four kinds of pieces:

  • C (cannon) and F (friend) are your pieces.
  • E (enemy) and K (king) are the opponent's pieces.

Empty cells are written as B. Exactly one C and exactly one K appear on the board. Your task is to capture K using a sequence of valid cannon moves.

The rules of one-dimensional janggi are:

  1. You may move only C.
  2. C moves by jumping over exactly one piece, which may be K, E, or F.
  3. C must land either on an empty cell or on a cell holding an opponent's piece (E or K).
  4. When C lands on an opponent's piece, we say it captures that piece, and the captured piece is removed from the board. C can never land on (capture) a friendly piece F.
  5. The game ends the moment C captures K.

For example, on the board above C sits at position 6. From there C can move to 2 (capturing E), 3, 4, 9, or 10 (capturing E). No other cell is reachable in a single move.

After C captures the E at 10, the board becomes the one shown above. Now C can move to 6, 7, 12, or 13 (capturing K).

Input

Input is read from standard input. The first line contains the number of test cases TT. Each of the next TT lines contains one string that describes a one-dimensional janggi board. Occupied cells are written as C, E, F, or K, and empty cells are written as B. Each string has length at least 5 and at most 200.

Output

Write to standard output. For each test case, print on its own line the minimum number of moves C needs to capture K. If capturing K is impossible, print 0.

Examples3

  1. Example 1

    Input
    3
    BEBBFCBEBEFBKB
    BEEBKCE
    BCBEFFEK
    
    Expected output
    2
    3
    0
    
  2. Example 2

    Input
    1
    CEKBB
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    KEBBC
    
    Expected output
    1