King (Small)
Time limit5sMemory limit512 MB
On a board of at most 16 squares with burned cells, a king moves to unvisited neighbors; decide who wins under optimal play.
- Level
Medium6 of 10
- Topics
- Game theory, DFS, Bit manipulation
- Solved
- No attempts yet
Problem
Alice and Bob play a game on a chessboard with rows and columns, so squares in total. Some of the squares are burned.
At the start of the game a king stands on one unburned square. Alice and Bob then move the king in turn.
A move takes the king to one of its 8 neighbouring squares and has to satisfy two conditions:
- the destination square is not burned;
- the king has never stood on the destination square before.
A player who cannot move loses the game. Alice moves first. Determine who wins when both players play optimally.
Input
The first line contains the number of test cases .
test cases follow. The first line of a test case contains two integers and . Each of the next lines contains a string of length that describes the squares of one row. Each string uses only the characters '.', '#' and 'K':
- '#' means the square is burned;
- '.' means the square is unburned and empty;
- 'K' means the king stands on that square at the start of the game.
Each test case contains exactly one 'K'.
Limits
Output
For each test case, print one line with "Case #: ", where is the test case number starting from 1, followed by A if Alice wins or B if Bob wins.