Under construction

Still being built. Things may change

Board Game

Time limit1sMemory limit1024 MB

Summary
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 NN and MM, the game uses 2NM2NM cards in total. Exactly NMNM of these cards have the letter A written on them, and the other NMNM cards have B. Before the game starts, Alice and Bob each take NMNM cards and lay them out in an N×MN\times M 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 MM 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 NN and MM, separated by a space. (1≤N≤101\leq N\leq 10; 1≤M≤1 000 0001\leq M\leq 1\,000\,000; NM≤1 000 000NM\leq 1\,000\,000)

From the second line onward, NN lines give Alice's layout. Line (i+1)(i+1) gives the cards in row ii of Alice's grid.

From line (N+2)(N+2) onward, NN lines give Bob's layout. Line (i+N+1)(i+N+1) gives the cards in row ii of Bob's grid.

Each row is a string of length MM made only of A and B. The character in row ii, column jj is the letter on that player's card at row ii, column jj. The input strings contain NMNM A characters and NMNM B characters in total. Row 1 of each player's layout is the topmost card in each column.

Line (2N+2)(2N+2) contains a positive integer QQ, the number of swaps. (1≤Q≤200 0001\leq Q\leq 200\,000)

From line (2N+3)(2N+3) onward, QQ lines describe the swaps. Line (i+2N+2)(i+2N+2) contains four integers r1r_1, c1c_1, r2r_2, c2c_2, separated by spaces. (1≤r1,r2≤N1\leq r_1,r_2\leq N; 1≤c1,c2≤M1\leq c_1,c_2\leq M) The pair (r1,c1)(r_1,c_1) is the position of the card in Alice's grid to swap, and (r2,c2)(r_2,c_2) 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 QQ lines, print the winner in the same format after applying the first ii swaps, for i=1,…,Qi=1,\dots,Q. Line (i+1)(i+1) gives the result after swap ii.

Examples1

  1. Example 1

    Input
    2 3
    BAA
    ABA
    BAB
    BBA
    2
    2 2 1 2
    2 3 2 1
    
    Expected output
    Alice
    Bob
    Alice