Circular Game
Time limit1sMemory limit128 MB
On a circular board, white and black pieces slide over empty runs; decide with optimal play which side wins or whether play can go on forever.
- Level
Medium7 of 10
- Topics
- Game theory, Array, Implementation, Sorting
- Solved
- No attempts yet
Problem
The board of the circular game is a ring of fields numbered from 1 to . It carries white pieces and black pieces, at most one piece on a field. The white player and the black player move alternately, and white starts.
A move takes one piece of the player's own colour forward or backward across any number of free fields. Every field the piece passes must be empty. On the board drawn below, white can move the piece on field 3 to field 4, or the piece on field 8 to field 7, 9 or 1.

A player with no legal move on his turn loses. Both players play optimally. Decide who wins. The game can also go on forever, with neither player winning.
Input
The first line contains the number of boards . Each board is then described by three lines.
The first line of a board holds its length , the number of white pieces and the number of black pieces , separated by single spaces (, , ).
The second line holds the fields taken by white pieces in increasing order, and the third line holds the fields taken by black pieces in increasing order. Every field number is between 1 and .
The pieces on all boards add up to at most .
Output
Print exactly lines, one answer per board, in the order the boards are given. Each answer is a single character: B if the white player wins, C if the black player wins, R if the game never ends.