Board Game
Time limit1sMemory limit1024 MB
Given two N by M grids of A and B cards, find who wins the alternating column-removal game at the start and after each of Q card swaps.
- Level
Hard8 of 10
- Topics
- Game theory, Segment tree, Greedy, Array
- Solved
- No attempts yet
Problem
Alice and Bob play a board game together. For some positive integers and , the game uses cards in total. Exactly of these cards have the letter A written on them, and the other cards have B. Before the game starts, Alice and Bob each take cards and lay them out in an grid in front of themselves. Each player knows the other's layout.
Alice takes the first turn. On her turn, a player chooses one of the columns in front of them and removes the topmost card in that column. If the removed card has A on it, Alice takes the next turn. If it has B, Bob takes the next turn. A player must remove a card on every turn, so a column with no cards left cannot be chosen. If a player's removal empties all the cards in front of them, that player wins immediately.
Alice and Bob each try their best to win. Given a starting layout, they want to know who wins. They also want to know how the winner changes each time Alice and Bob each pick one card in front of themselves and swap the two chosen cards. Swaps are cumulative, so each swap applies to the layout left by the previous swaps.
Input
The first line contains two positive integers and , separated by a space. (; ; )
From the second line onward, lines give Alice's layout. Line gives the cards in row of Alice's grid.
From line onward, lines give Bob's layout. Line gives the cards in row of Bob's grid.
Each row is a string of length made only of A and B. The character in row , column is the letter on that player's card at row , column . The input strings contain A characters and B characters in total. Row 1 of each player's layout is the topmost card in each column.
Line contains a positive integer , the number of swaps. ()
From line onward, lines describe the swaps. Line contains four integers , , , , separated by spaces. (; ) The pair is the position of the card in Alice's grid to swap, and is the position of the card in Bob's grid to swap.
Output
On the first line, print the winner of the starting layout, either Alice or Bob.
On each of the next lines, print the winner in the same format after applying the first swaps, for . Line gives the result after swap .