ECOO '14 R1 P4 - Fischer Random Chess

View as PDF

Points: 5 (partial)
Time limit: 2.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Fischer Random Chess is just like regular chess except that the starting positions of the pieces are randomized. The game was proposed by grand master Bobby Fischer to make it virtually impossible for players to memorize a set of opening moves.

In Fischer Random Chess, the back row of pieces (two Bishops, two Knights, two Rooks, a King and a Queen) can be placed in any order as long as the King is between the two Rooks (to enable the castling move) and the Bishops are on opposite colors.

In the boards below, K=King, Q=Queen, R=Rook, B=Bishop and N=Knight. The first arrangement is legal. The second two are not.

R N Q B K R B N
R N K B B R N Q
B K B Q R N R N

Of course any arrangement that contains the wrong mix of pieces (e.g. three bishops and one knight) is also illegal.

The input will contain test cases. Each test case consists of a single line of 8 characters, representing a partially constructed back row. Pieces that have been filled in are represented with one of the capital letters K, Q, R, B or N (see the key above) and spaces that have not been filled are represented with the underscore character _ (ASCII code ).

Your job is to output a single number representing the number of legal Fischer Random Chess arrangements that can be created by filling in the blanks with the remaining pieces.

Sample Input

NR____RN
___BB___
_R____R_
___KQ___
___QK___
NBRK____
__Q___B_
__Q___NN
_RKR____
RKRBN__Q

Sample Output

8
60
72
32
32
12
40
6
18
1

Question Development Team

President of ECOO-CS Communications Sam Scott (Sheridan College) Kevin Forest (Sheridan College) David Stermole Greg Reid (St. Francis Xavier Secondary School, Mississauga) Dino Baron (University of Waterloo) Nathan Schucher (University of King’s College) John Ketelaars

Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org